Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- struct arqv {
- struct arqv *prev,*prox;
- int estado_civil,idade;
- char nome[30],telefone[15];
- };
- typedef struct arqv New;
- int menu(int i);
- int listar_todos(New **data,New **ini,New **fim);
- void inserir(New **data,New **ini,New **fim);
- void primeiro_cadastro (New **data,New **ini,New **fim);
- void deletar(New **data,New **ini,New **fim);
- void editar(New **data,New **ini,New **fim);
- int main()
- {
- New *data=NULL,*fim=NULL,*ini=NULL;
- int i;
- do{
- i=menu(i);
- switch (i)
- {
- case 1: inserir(&data,&ini,&fim); break;
- case 2: editar(&data,&ini,&fim); break;
- case 3: deletar(&data,&ini,&fim); break;
- case 4: listar_todos(&data,&ini,&fim); break;
- case 5: printf(" Finalizando...\n"); break;
- default: printf(" \t\t OPCAO INVALIDA !!!\n");
- }
- }while (i!=5);
- return 1;
- }// fim de main
- //funcao exibe o menu para o usuario
- int menu(int i)
- {
- printf("\n\t\t|--------------------|\n");
- printf(" \t \tAgenda \n");
- printf(" \t\t|--------------------|\n\n");
- printf(" 1- Inserir \n");
- printf(" 2- Editar \n");
- printf(" 3- Deletar \n");
- printf(" 4- Listar todos \n");
- printf(" 5- Sair \n");
- fflush(stdin);
- scanf("%d",&i);
- return i;
- }
- void inserir(New **data,New **ini,New **fim)
- {
- New *aux=NULL;
- int flag=0;
- if((*ini)==NULL)
- {
- primeiro_cadastro(&*data,&*ini,&*fim); (*data)=*ini; return;
- }
- else if(*ini!=NULL)
- {
- aux=(New*)malloc(sizeof(New));
- aux->prox=NULL;
- printf(" --- \t Nome \n\t\t");
- fflush(stdin);
- gets((aux)->nome);
- printf(" --- \t Idade\n\t\t");
- fflush(stdin);
- scanf("%d",&(aux)->idade);
- printf(" --- \t Estado Civil 1- Casado, 2- Solteiro, 3- Viuvo\n\t\t");
- fflush(stdin);
- scanf("%d",&(aux)->estado_civil);
- printf(" --- \t Telefone\n\t\t");
- fflush(stdin);
- gets((aux)->telefone);
- (*data)=*ini;
- if(strcmp((*ini)->nome,aux->nome)>=0)
- {
- aux->prox=(*data);
- aux->prev=NULL;
- (*data)->prev=aux;
- (*ini)=aux;
- (*fim)=(*data);
- return;
- }
- flag=1;
- while((*data)->prox!=NULL)
- {
- if(strcmp((*data)->nome,aux->nome)>=0)
- {
- aux->prox=(*data);
- aux->prev=(*data)->prev;
- (*data)->prev->prox=aux;
- (*data)->prev=aux;
- flag=0;
- break;
- }
- (*data)=(*data)->prox;
- }
- if(flag==1&&(*data)->prox==NULL)
- {
- if(strcmp((*data)->nome,aux->nome)>=0)
- {
- aux->prox=(*data);
- aux->prev=(*data)->prev;
- aux->prev->prox=aux;
- (*data)->prev=aux;
- }
- else if(strcmp((*data)->nome,aux->nome)<=0)
- {
- (*data)->prox=aux;
- aux->prox=NULL;
- aux->prev=(*data);
- (*fim)=aux;
- }
- }
- }
- }
- void primeiro_cadastro (New **data,New **ini,New **fim)
- {
- *data=(New*)malloc(sizeof(New));
- (*ini)=(*data);
- (*ini)->prev=NULL;
- printf(" --- \t nome \n\t\t");
- fflush(stdin);
- gets((*data)->nome);
- printf(" --- \t idade\n\t\t");
- fflush(stdin);
- scanf("%d",&(*data)->idade);
- printf(" --- \t Estado Civil 1- Casado, 2- Solteiro, 3- Viuvo\n\t\t");
- fflush(stdin);
- scanf("%d",&(*data)->estado_civil);
- printf(" --- \t Telefone\n\t\t");
- fflush(stdin);
- gets((*data)->telefone);
- (*ini)->prox=NULL;
- (*fim)=(*ini);
- }
- int listar_todos(New **data,New **ini,New **fim)
- {
- int temp=0;
- New *temp1=NULL;
- //condicao verifica se o ini == NULL, se for = a NULL a lista estah vazia;
- if((*ini)==NULL)
- {
- printf(" \t\t\t ------- Nenhum Dado cadastrado --------- \n");
- printf(" \t\t\t Se quiser cadastrar digite 1, se nao digite 0\n\n");
- scanf("%d",&temp);
- if(temp==1) {primeiro_cadastro(&(*data),&(*ini),&(*fim)); return 0;} else {return 0;}
- }
- for(temp1=*ini;temp1!=NULL;temp1=temp1->prox)
- {
- printf(" \n\n\t\t\t nome: %s \n",temp1->nome);
- printf(" \t\t\tidade: %d\n",temp1->idade);
- if(temp1->estado_civil==1)
- printf(" \t\t\testado civil: Casado \n");
- else if(temp1->estado_civil==2)
- printf(" \t\t\t estado civil: Solteiro\n");
- else if(temp1->estado_civil==3)
- printf(" \t\t\testado civil: Viuvo \n");
- printf(" \t\t\t Telefone: %s",temp1->telefone);
- }
- return 0;
- }
- void deletar(New **data,New **ini,New **fim)
- {
- char nome[30];
- New *temp=NULL;
- if(*ini==NULL)
- {
- printf(" \t\t\t Lista vazia \n");
- printf(" \t\t\t Insira algum dado antes de deletar \n");
- return;
- }
- fflush(stdin);
- printf(" Digite o nome do cadastro a ser deletado \n");
- printf(" \n\n\t\t\t*** Nomes Disponiveis *** \n");
- for(*data=*ini;*data!=NULL;*data=(*data)->prox) printf("%s \n",(*data)->nome); gets(nome);
- for(*data=*ini;*data!=NULL;*data=(*data)->prox)
- {
- if(strcmp((*data)->nome,nome)==0) break;
- }
- if((*data)!=NULL)
- {
- if(*ini==*fim)
- {
- (*ini)=NULL;
- (*fim)=NULL;
- return;
- }
- else if(*data==*ini&&*ini!=*fim)
- {
- (*ini)=(*data)->prox;
- (*ini)->prev=NULL;
- free(*data);
- printf(" \t\t\t Cadastro %s deletado com sucesso !!! \n\n",nome);
- }
- else if(*data==*fim&&*ini!=*fim&&*data!=*ini)
- {
- (*fim)=(*data)->prev;
- (*fim)->prox=NULL;
- free(*data);
- printf(" \t\t\t Cadastro %s deletado com sucesso !!! \n\n",nome);
- }
- else if(*ini!=*fim&&*data!=*ini&&*data!=*fim)
- {
- (*data)->prev->prox=(*data)->prox;
- (*data)->prox->prev=(*data)->prev;
- free((*data));
- printf(" \t\t\t Cadastro %s deletado com sucesso !!! \n\n",nome);
- }
- }
- else printf("\t\t\t--------- Numero nao encontrado \n\n\n -----------");
- for(temp=*ini;temp!=NULL;temp=temp->prox)*fim=temp;
- }
- void editar(New **data,New **ini,New **fim)
- {
- char nome[30];
- New *temp=NULL;
- if(*ini==NULL)
- {
- printf(" \t\t\t Lista vazia \n");
- printf(" \t\t\t Insira algum dado antes de editar \n");
- return;
- }
- fflush(stdin);
- printf(" Digite o nome do cadastro a ser editado \n");
- printf(" \n\n\t\t\t*** Nomes Disponiveis *** \n\n");
- for(*data=*ini;*data!=NULL;*data=(*data)->prox) printf("\t\t\t %s \n",(*data)->nome); gets(nome);
- for(*data=*ini;*data!=NULL;*data=(*data)->prox)
- {
- if(strcmp((*data)->nome,nome)==0) break;
- }
- if((*data)!=NULL)
- {
- if(*ini==*fim)
- {
- (*ini)=NULL;
- (*fim)=NULL;
- }
- else if(*data==*ini&&*ini!=*fim)
- {
- (*ini)=(*data)->prox;
- (*ini)->prev=NULL;
- free(*data);
- }
- else if(*data==*fim&&*ini!=*fim&&*data!=*ini)
- {
- (*fim)=(*data)->prev;
- (*fim)->prox=NULL;
- free(*data);
- }
- else if(*ini!=*fim&&*data!=*ini&&*data!=*fim)
- {
- (*data)->prev->prox=(*data)->prox;
- (*data)->prox->prev=(*data)->prev;
- free((*data));
- }
- }
- else
- {printf("\t\t\t ------- Nome nao encontrado ------- \n"); return; }
- printf("\nEdite: \n\n"); inserir(&(*data),&(*ini),&(*fim));
- for(temp=*ini;temp!=NULL;temp=temp->prox)*fim=temp;
- printf(" O *fim->nome eh %s \t e o *ini->nome eh %s",(*fim)->nome,(*ini)->nome);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement