Baixe o app para aproveitar ainda mais
Prévia do material em texto
/* Lista duplamente encadeada com comportamento de pilha. * Aluno: Thiago Mendes da Silva. * Estruturas de Dados I *Ano: 2011 * Acadêmico de Ciência da Computação. * Universidade Federal da Fronteira Sul. */ #include <stdio.h> #include <stdlib.h> struct lista { int info; struct lista *next, *prev; }; typedef struct lista Lista; Lista* insere(){ return NULL; } Lista* push(Lista* l, int e){ // Inserir elemento, sempre inserindo no 'topo' da lista. Lista* new =(Lista *)malloc(sizeof(Lista)); new->info=e; new->next=l; new->prev=NULL; return new; } Lista* pop(Lista* l){ // Exclusão: Sempre no 'topo' da lista. Lista *p; if(l==NULL){ puts("Pilha vazia"); return l; } if(l->next == NULL){ l = NULL; return l; } l=l->next; if(l==NULL){ puts("Pilha Vazia"); } for(p=l; p!=NULL; p = p->next){ printf("Info: %d\n", p->info); } return l; } void imprime(Lista* l){ Lista* p; if(l==NULL){ puts("Pilha Vazia"); } for(p=l; p!=NULL; p = p->next){ printf("Info: %d\n", p->info); } } int main () { Lista *l; l = insere(); int e, OP, sair=0; while(sair!=4){ puts("\tMenu"); puts("1 - Insere"); puts("2 - Exclui"); puts("3 - Imprime"); puts("4 - Sair"); scanf("%d",&OP); switch(OP){ case 1: puts("Informe o numero a ser inserido:"); scanf("%d",&e); l = push(l,e); break; case 2: l = pop(l); break; case 3: imprime(l); break; case 4: sair=4; } } return 0; }
Compartilhar