Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct Node{
- int n;
- struct Node *prox;
- }typedef node;
- int vazia(node *lista){
- if(lista->prox==NULL) return 1;
- return 0;
- }
- node *aloca(){
- node *novo = (node*) malloc(sizeof(node));
- if(!novo){
- printf("Sem memoria disponivel!\n");
- exit(1);
- }
- else{
- printf("Novo elemento: ");
- scanf("%d",&novo->n);
- return novo;
- }
- }
- void insereFim(node *lista){
- node *novo=aloca();
- novo->prox = NULL;
- if(vazia(lista))
- lista->prox=novo;
- else{
- node *tmp = lista->prox;
- while(tmp->prox != NULL)
- tmp = tmp->prox;
- tmp->prox = novo;
- }
- }
- void exibe(node *lista){
- if(vazia(lista)){
- printf("Lista vazia!\n\n");
- return;
- }
- node *tmp;
- tmp = lista->prox;
- printf("Lista:");
- while(tmp){
- printf("%5d", tmp->n);
- tmp = tmp->prox;
- }
- printf("\n");
- }
- void libera(node *lista){
- if(!vazia(lista)){
- node *proxNode,*atual;
- atual = lista->prox;
- while(atual != NULL){
- proxNode = atual->prox;
- free(atual);
- atual = proxNode;
- }
- }
- }
- node *retiraInicio(node *lista){
- if(lista->prox == NULL){
- printf("Lista ja esta vazia\n");
- return NULL;
- }
- else{
- node *tmp = lista->prox;
- lista->prox = tmp->prox;
- return tmp;
- }
- }
- void insereInicio(node *lista){
- node *novo=aloca();
- node *oldHead = lista->prox;
- lista->prox = novo;
- novo->prox = oldHead;
- }
- node *retiraFim(node *lista){
- if(lista->prox == NULL){
- printf("Lista ja vazia\n\n");
- return NULL;
- }
- else{
- node *ultimo = lista->prox,
- *penultimo = lista;
- while(ultimo->prox != NULL){
- penultimo = ultimo;
- ultimo = ultimo->prox;
- }
- penultimo->prox = NULL;
- return ultimo;
- }
- }
- int menu(){
- int opc;
- printf(" _________________________\n");
- printf("|__________|Menu|_________|\n");
- printf("|0-Sair. |\n");
- printf("|1-Inserir inicio. |\n");
- printf("|2-Inserir fim. |\n");
- printf("|3-Remover inicio. |\n");
- printf("|4-Remover fim. |\n");
- printf("|5-Mostrar. |\n");
- printf("|_________________________|\n");
- printf("Digite uma opcao: ");
- scanf("%d",&opc);
- return opc;
- }
- int main(){
- int opc;
- node l,*tmp;
- l.prox=NULL;
- do{
- opc=menu();
- switch(opc){
- case 0:
- printf("Obrigado por usar o programa.\n");
- break;
- case 1:
- insereFim(&l);
- break;
- case 5:
- exibe(&l);
- break;
- case 3:
- tmp=retiraInicio(&l);
- printf("Retirado: %3d\n\n", tmp->n);
- break;
- case 2:
- insereInicio(&l);
- break;
- case 4:
- tmp=retiraFim(&l);
- printf("Retirado: %3d\n\n", tmp->n);
- break;
- default:
- printf("Obrigado por usar o programa.\n");
- break;
- }
- system("pause");
- system("cls");
- }while(opc!=0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement