Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <conio.h>
- #include <malloc.h>
- typedef struct estrutura
- {
- int chave;
- estrutura *prox;
- } NO;
- typedef struct
- {
- NO* inicio;
- } LISTA;
- void inicializar(LISTA *l)
- {
- l->inicio = NULL;
- }
- void exibir(LISTA *l)
- {
- NO* p = l->inicio;
- while(p)
- {
- printf("%i\t", p->chave);
- p = p->prox;
- }
- printf("\n");
- }
- NO* buscar(LISTA *l, int chave, NO* *ant)
- {
- NO* p = l->inicio;
- *ant = NULL;
- while(p)
- {
- if(p->chave == chave) return p;
- *ant = p;
- p = p->prox;
- }
- return NULL;
- }
- bool anexar(LISTA *l, int chave)
- {
- NO* ant;
- NO* novo = buscar(l, chave, &ant);
- if(novo) return false;
- novo = (NO*) malloc(sizeof(NO));
- novo->chave = chave;
- if(!l->inicio)
- {
- l->inicio = novo;
- novo->prox = NULL;
- }
- else
- {
- if(!ant)
- {
- novo->prox = l->inicio;
- l->inicio = novo;
- }
- else
- {
- novo->prox = ant->prox;
- ant->prox = novo;
- }
- }
- return true;
- }
- int size(LISTA *l)
- {
- NO* p = l->inicio;
- int cont = 0;
- while(p)
- {
- cont++;
- p = p->prox;
- }
- return cont;
- }
- bool inserir(LISTA *l, int chave, int pos)
- {
- if(pos <0) return false;
- NO* ant;
- NO* novo = buscar(l, chave, &ant);
- if(novo) return false;
- novo = (NO*) malloc(sizeof(NO));
- novo->chave =chave;
- if(pos == 0)
- {
- novo->prox = l->inicio;
- l->inicio = novo;
- }
- if(pos >= size(l))
- {
- ant->prox = novo;
- novo->prox = NULL;
- }
- else
- {
- int cont=0;
- NO* p = l->inicio;
- while(p)
- {
- if(cont == pos -1)
- {
- novo->prox = p->prox;
- p->prox =novo;
- return true;
- }
- else
- {
- cont ++;
- p =p->prox;
- }
- }
- }
- return true;
- }
- bool excluir(LISTA *l, int chave)
- {
- NO* ant;
- NO* p = buscar(l, chave, &ant);
- if(!p) return false;
- if(!ant) l->inicio = p->prox;
- else ant->prox = p->prox;
- free(p);
- return true;
- }
- void destruir(LISTA *l)
- {
- NO* p =l->inicio;
- NO* aux;
- while(p)
- {
- aux = p->prox;
- free(p);
- p = aux;
- }
- l->inicio = NULL;
- }
- //Comparar 2 listas, se forem identicas return true : return false;
- bool compararListas(LISTA *l1, LISTA *l2)
- {
- if(size(l1) != size(l2)) return false;
- NO* p1 = l1->inicio;
- NO* p2 = l2->inicio;
- while(p1)
- {
- if(p1->chave != p2->chave) return false;
- p1 = p1->prox;
- p2 = p2->prox;
- }
- printf("Sao inguais\n");
- return true;
- }
- //Comparar 2 listas, se todos os elementos de uma existirem na outra, return true : return false;
- bool compararElementos(LISTA *l1, LISTA *l2)
- {
- if(size(l1) != size(l2)) return false;
- NO* p1 = l1->inicio;
- NO* p2 = l2->inicio;
- int cont = 0;
- while(p1)
- {
- while(p2)
- {
- if(p1->chave != p2->chave)
- {
- p2 = p2->prox;
- } else {
- cont++;
- p2 = l2->inicio;
- break;
- }
- }
- p1 = p1->prox;
- }
- if(cont == size(l1)) {printf("Sao inguais\n");return true;}
- return false;
- }
- //Concatenar 2 listas
- NO* getUltimo(LISTA *l)
- {
- NO* p = l->inicio;
- while(p->prox) {
- p = p->prox;
- }
- return p;
- }
- bool concatenar(LISTA *l1, LISTA *l2)
- {
- NO* p = l1->inicio;
- while(p->prox) {// pode subistiruir pelo get ultimo
- p = p->prox;
- }
- p->prox = l2->inicio;
- return true;
- }
- //Inverter uma lista
- bool inverterLista(LISTA *l)
- {
- NO* atual = l->inicio;
- NO* proximo = l->inicio;
- NO* temp = l->inicio;
- while( proximo != NULL ){
- // preserva ponteiro para caminhar no encadeamento
- temp = temp->prox;
- // inverte o caminho do elemento posterior ao atual
- proximo->prox = atual;
- // determina o proximo elemento a ser processado
- atual = proximo;
- // determina o elemento posterior
- proximo = temp;
- }
- // remove link do primeiro elemento com o segundo elemento
- l->inicio->prox = NULL;
- // aponta para a nova cabeca da lista
- l->inicio = atual;
- return true;
- }
- main()
- {
- LISTA l;
- LISTA l2;
- inicializar(&l);
- anexar(&l, 1);
- anexar(&l, 2);
- anexar(&l, 3);
- exibir(&l);
- getch();
- inicializar(&l2);
- anexar(&l2, 1);
- anexar(&l2, 4);
- anexar(&l2, 3);
- exibir(&l2);
- getch();
- inverterLista(&l);
- exibir(&l);
- getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment