Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- struct st_next
- {
- int id;
- char name[100];
- struct st_next *nextI;
- struct st_next *nextS;
- };
- typedef struct st_next *no;
- int vazio (no * reader)
- {
- if((reader->nextI == NULL)&&(reader->nextS == NULL))
- {
- return 1;
- }else
- {
- return 0;
- }
- }
- void initial (no * reader)
- {
- reader->nextI = NULL;
- reader->nextS = NULL;
- }
- void freeUsage(no * reader)
- {
- if(!vazio(reader))
- {
- no *proxEl,*proxN, *atualI,*atualS;
- atualI = reader->nextI;
- atualS = reader->nextS;
- while((atualI != NULL)&&(atualS != NULL))
- {
- proxEl = atualI->nextI;
- free(atualI);
- atualI = proxEl;
- proxN = atualS->nextS;
- free(atualS);
- atualS = proxN;
- }
- }
- }
- void showList (no * reader)
- {
- if(vazio(reader))
- {
- printf("Primeiro adicione algum elemento na lista\n");
- exit(1);
- }else
- {
- no *tmpI,tmpS;
- tmpS = malloc(100 * sizeof(char*));
- tmpI = reader->next;
- while((tmpI != NULL)&&(tmpS != NULL))
- {
- printf("%d",tmpI->id);
- tmpI = tmpI->nextI;
- printf("%s",tmpS->name);
- tmpS = tmpS->nextS;
- }
- }
- }
- void adicionaInicio (no * reader)
- {
- no *novoElI = (no*)malloc(sizeof(no));
- no *novoElS = (no*)malloc(sizeof(no));
- if((!novoElI)&&(!novoElS))
- {
- printf("Sem espaço! Libere um pouco antes de executar\n");
- exit(1);
- }else
- {
- printf("Adicione o elemento\n");
- novoElS->name= malloc(100);
- fgets(novoElS->name,100,stdin);
- scanf("%d",&novoElI->id);
- no * velhoTopI = reader->nextI;
- reader->nextI = novoElI;
- novoElI->nextI =velhoTopI;
- novoElS->name[reader];
- no *velhoTopS = reader ->nextS;
- reader->nextS = novoElS;
- novoElS->nextS = velhoTopS;
- }
- }
- void adicionaFim (no * reader)
- {
- no *novoElI = (no*)malloc(sizeof(no));
- no *novoElS = (no*)malloc(sizeof(no));
- if((!novoElI)&&(!novoElS))
- {
- printf("Sem memoria disponivel\n");
- exit(1);
- }
- printf("Adicione o elemento do fim\n");
- novoElS->name = malloc(100);
- fgets(novoElS->name,100,stdin);
- scanf("%d",&novoElS->id);
- novoElI->nextI = NULL;
- novoElS->nextS = NULL;
- if(vazio(reader))
- {
- reader->nextI = novoElI;
- reader->nextS = novoElS;
- }else
- {
- no *tmpI = reader->nextI;
- no *tmpS = reader->nextS;
- while((tmpI->nextI != NULL)&&(tmpS->nextS != NULL))
- {
- tmpI = tmpI->nextI;
- tmpS = tmpS->nextS;
- }
- tmpI->nextI = novoElI;
- tmpS->nextS = novoElS;
- }
- }
- int userInterface()
- {
- system("Cls");
- int opt;
- printf("----------|Menu|--------------\n");
- printf("[1] - Adicionar um elemento no inicio da lista\n");
- printf("[2] - Adicionar um elemento no fim da lista\n");
- printf("[3] - Imprimir elementos na tela\n");
- printf("[4] - Terminar programa\n");
- printf("Sua opcao: ");
- scanf("%d",&opt);
- return opt;
- }
- void choosenOne (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("Fechando o programa!\n");
- freeUsage(reader);
- break;
- default:
- printf("Opcao invalida,por favor escolha uma opcao valida\n");
- break;
- }
- }
- int main ()
- {
- int opcao;
- no * reader= (no*) malloc(sizeof(no));
- if(!reader)
- {
- printf("Sem memoria man!\n");
- exit(1);
- }
- initial(reader);
- do
- {
- opcao = userInterface();
- choosenOne(reader,opcao);
- }while(opcao != 4);
- free(reader);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement