Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdbool.h>
- struct node
- {
- char name[100];
- int id;
- struct node *next;
- };
- typedef struct node no;
- bool vazio(no * reader)
- {
- if(reader = NULL)
- {
- return 1;
- }else
- {
- return 0;
- }
- }
- void inicia (no * reader)
- {
- reader->next = NULL;
- }
- void liberaEspaco(no * reader)
- {
- if(!vazio(reader))
- {
- no *proxEl, *atual;
- atual = reader->next;
- while(atual != NULL)
- {
- proxEl = atual->next;
- free(atual);
- atual = proxEl;
- }
- }
- }
- void showList(no * reader)
- {
- no * tmp;
- tmp = reader->next;
- while(tmp !=NULL)
- {
- printf("Nome:%s\n",tmp->name);
- printf("Idade:%d\n",tmp->id);
- printf("=========================\n");
- tmp = tmp->next;
- }
- }
- void adicionaInicio (no * reader)
- {
- no * novoEl;
- int cont;
- char **tempName;
- tempName = malloc(1 * sizeof(int));
- novoEl =(no*)malloc(sizeof(no));
- if(vazio(novoEl))
- {
- printf("Sem espaco man!\n");
- exit(1);
- }
- fflush(stdin);
- for(cont = 0;cont < 1; cont++)
- {
- printf("Adicione um Elemento\n");
- tempName[cont] = malloc(100);
- fgets(tempName[cont],100,stdin);
- strcpy(&novoEl->name[cont],tempName[cont]);
- }
- printf("De a idade ao elemento\n");
- scanf("%d",&novoEl->id);
- no *velhoTopo = reader->next;
- reader -> next = novoEl;
- novoEl ->next = velhoTopo;
- }
- void adicionaFim(no * reader)
- {
- no * novoEl;
- int cont;
- char **tempName;
- tempName = malloc(1 * sizeof(int));
- novoEl = (no*)malloc(sizeof(no));
- if(vazio(novoEl))
- {
- printf("Sem espaco man\n");
- exit(1);
- }
- fflush(stdin);
- for(cont = 0;cont < 1;cont++)
- {
- printf("Digite o nome do elemento\n");
- tempName[cont] = malloc(100);
- fgets(tempName[cont],100,stdin);
- strcpy(&novoEl->name[cont],tempName[cont]);
- }
- printf("De uma idade ao elemento\n");
- scanf("%d",&novoEl->id);
- novoEl -> next = NULL;
- if(vazio(reader))
- {
- reader->next = novoEl;
- }else
- {
- no* tmp = reader->next;
- while(tmp->next != NULL)
- {
- tmp = tmp->next;
- }
- tmp->next = novoEl;
- }
- }
- int ui()
- {
- system("CLS");
- int opt;
- printf("-----------|Menu|-----------------\n");
- printf("===================================\n");
- printf("[1] - Adicione um elemento no inicio\n");
- printf("[2] - Adicione um elemento no fim \n");
- printf("[3] - Mostre a lista de elementos \n");
- printf("[4] - Finalizar o programa \n");
- printf("----------------------------------------\n");
- printf("A sua opcao:");
- scanf("%d",&opt);
- return opt;
- }
- void opcao(no * reader,int opt)
- {
- switch(opt)
- {
- case 1:
- adicionaInicio(reader);
- break;
- case 2:
- adicionaFim(reader);
- break;
- case 3:
- showList(reader);
- system("pause");
- break;
- case 4:
- printf("Programa sendo encerrado!\n");
- break;
- default:
- printf("Opcao inválida! Escolha uma opcao válida\n");
- }
- }
- int main ()
- {
- int option;
- no * reader =(no*)malloc(sizeof(no));
- if(!reader)
- {
- printf("Sem memoria man!\n");
- exit(1);
- }
- inicia(reader);
- do
- {
- option = ui();
- opcao(reader,option);
- }while(option != 4);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement