Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int COD=1;
- typedef struct estrutura
- {
- int cod;
- char nome[20];
- int idade;
- struct estrutura *prox;
- }estrutura;
- estrutura *alocaNo(char *nome, int idade)
- {
- estrutura *no=(estrutura*)malloc(sizeof(estrutura));
- no->cod=COD;
- COD++;
- strcpy(no->nome, nome);
- no->idade=idade;
- return no;
- }
- void insereInicio(estrutura **lista, char *nome, int idade)
- {
- estrutura *no=alocaNo(nome, idade);
- if((*lista)==NULL){
- (*lista)=no;
- no->prox=NULL;
- return;
- }
- no->prox=(*lista);
- (*lista)=no;
- return;
- }
- void insereFim(estrutura **lista, char *nome, int idade)
- {
- estrutura *no=alocaNo(nome, idade);
- no->prox=NULL;
- if((*lista)==NULL)
- {
- (*lista)=no;
- return;
- }
- estrutura *aux=(*lista);
- while(aux->prox!=NULL)
- {
- aux=aux->prox;
- }
- aux->prox=no;
- return;
- }
- void removeInicio(estrutura **lista)
- {
- estrutura *aux=(*lista);
- (*lista)=aux->prox;
- free(aux);
- return;
- }
- void removeFim(estrutura **lista)
- {
- estrutura *aux=(*lista);
- while(aux->prox->prox!=NULL)
- {
- aux=aux->prox;
- }
- free(aux->prox);
- aux->prox=NULL;
- return;
- }
- void removeNomeIdade(estrutura **lista, char *nome, int idade)
- {
- estrutura *aux=(*lista);
- estrutura *aux2=(*lista);
- if(strcmp(aux->nome, nome)==0 && aux->idade==idade)
- {
- (*lista)=aux->prox;
- free(aux);
- return;
- }
- aux=aux->prox;
- while(aux!=NULL)
- {
- if(strcmp(aux->nome, nome)==0 && aux->idade==idade)
- {
- aux2->prox=aux->prox;
- free(aux);
- return;
- }
- aux2=aux;
- aux=aux->prox;
- }
- return;
- }
- estrutura *buscaNome(estrutura **lista, char *nome)
- {
- estrutura *aux=(*lista);
- while(aux!=NULL && strcmp(aux->nome, nome)!=0)
- {
- aux=aux->prox;
- }
- if(aux==NULL)
- {
- return NULL;
- }
- return aux;
- }
- void imprimeLista(estrutura *lista)
- {
- estrutura *aux=lista;
- while(aux!=NULL)
- {
- printf(" Nome: %s\n Idade: %d\n\n", aux->nome, aux->idade);
- aux=aux->prox;
- }
- }
- int main(){
- estrutura *lista=NULL;
- int esc=1;
- while(esc!=0)
- {
- printf("-------------------------------------------------------------------------------------\n");
- printf(" Menu:\n\n 1- Inserir Inicio\n 2- Inserir Fim"
- "\n 3- Remover Inicio\n 4- Remover Fim\n 5- Remover Busca"
- "\n 6- Buscar pelo Nome\n 7- Imprime Lista\n\n Digite a sua escolha: ");
- scanf("%d", &esc);
- switch(esc)
- {
- case 1:
- {
- char nome[20];
- int idade;
- printf("\n Digite o nome: ");
- scanf("%s", nome);
- printf("\n Digite a idade: ");
- scanf("%d", &idade);
- insereInicio(&lista, nome, idade);
- printf("\n Inserido com sucesso!\n\n");
- break;
- }
- case 2:
- {
- char nome[20];
- int idade;
- printf("\n Digite o nome: ");
- scanf("%s", nome);
- printf("\n Digite a idade: ");
- scanf("%d", &idade);
- insereFim(&lista, nome, idade);
- printf("\n Inserido com sucesso!\n\n");
- break;
- }
- case 3:
- {
- removeInicio(&lista);
- printf("\n Removido com sucesso!\n\n");
- break;
- }
- case 4:
- {
- removeFim(&lista);
- printf("\n Removido com sucesso!\n\n");
- break;
- }
- case 5:
- {
- char nome[20];
- int idade;
- printf("\n Digite o nome: ");
- scanf("%s", nome);
- printf("\n Digite a idade: ");
- scanf("%d", &idade);
- removeNomeIdade(&lista, nome, idade);
- printf("\n Removido com sucesso!\n\n");
- break;
- }
- case 6:
- {
- char nome[20];
- estrutura *no;
- printf("\n Digite o nome: ");
- scanf("%s", nome);
- no=buscaNome(&lista, nome);
- printf("\n Nome: %s\n Idade: %d\n\n", no->nome, no->idade);
- break;
- }
- case 7:
- {
- printf("\n");
- imprimeLista(lista);
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement