Skip to content

Instantly share code, notes, and snippets.

@olh
Created October 24, 2013 02:03
Show Gist options
  • Save olh/7130105 to your computer and use it in GitHub Desktop.
Save olh/7130105 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
typedef struct Elemento{
int num;
struct Elemento *prox;
struct Elemento *ant;
};
//variáveis globais
struct Elemento *inicio,*novo,*ultimo,*i;
int nroElementos;
//prototipos
void criaLista(void);
struct Elemento* inserir(int);
void imprimir(void);
struct Elemento* busca(int);
//implementação
void criaLista(void){
inicio=NULL;
}//fim da função criaLista
struct Elemento* inserir(int n){
nroElementos++;
novo=(struct Elemento*) malloc(sizeof(struct Elemento));
if(!novo) return NULL;
else{
novo->num=n;
novo->prox=NULL;
if(inicio==NULL)
inicio=novo;
else
ultimo->prox=novo;
novo->ant=ultimo;
ultimo=novo;
}
return novo;
}//fim da função inserir
void imprimir(void){
if(inicio!=NULL){
i=inicio;
do{
printf("[%i]->[%p]\n",i->num,i->prox);
i=i->prox;
}while(i!=NULL);
}
else{
printf("Lista vazia!!!\n");
}
}//fim da função imprimir
struct Elemento* busca(int n){
i=inicio;
if(i==NULL){
return inicio;
}
else{
do{
if(n==i->num){
return i;
break;
}
i=i->prox;
}while(i!=NULL);
return NULL;
}
}
struct Elemento* pegaMenor(struct Elemento* e){
struct Elemento* aux;
i=e;
if(i==NULL){
return e;
}
else{
aux=e;
do{
if(i->num<e->num){
e=i;
}
i=i->prox;
}while(i!=NULL);
if(e->ant!=NULL){
e->ant->prox=e->prox;
e->prox=NULL;
}
else{
inicio=e->prox;
}
return e;
}
}
void ordenar(void) {
struct Elemento* auxiliar;
struct Elemento* novoI;
struct Elemento* novoInicio;
int x=0;
printf("a");
for (x=0; x<nroElementos;x++){
i=inicio;
printf("b");
auxiliar = pegaMenor(inicio);
if (novoI!=NULL){
novoI->prox=auxiliar;
novoI=novoI->prox;
}
else{
novoI=auxiliar;
novoInicio=auxiliar;
}
i=i->prox;
}
inicio=novoInicio;
}
int main(void){
criaLista();
printf("Lista Criada\n");
int opt,num;
do{
//system("clear");
printf("1-Inserir\n2-Listar\n3-Buscar\n4-Ordenar\n5-Sair");
scanf("%i",&opt);
switch(opt){
case 1:
if(!inserir(rand()%100))
printf("Valor não inserido\n");
else
printf("Valor inserido\n");
break;
case 2:
imprimir();
break;
case 3:
printf("Numero :");
scanf("%i",&num);
struct Elemento *aux=busca(num);
if(aux==inicio)
printf("Lista vazia!\n");
else if(aux==NULL)
printf("Não encontrado\n");
else
printf("Valor encontrado\n");
break;
case 4:
printf("aa");
ordenar();
break;
}
}while(opt!=5);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment