Advertisement
Guest User

Untitled

a guest
Jun 20th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. struct arqv {
  6.     struct arqv *prev,*prox;
  7.     int estado_civil,idade;
  8.     char nome[30],telefone[15];
  9.         };
  10.  
  11.             typedef struct arqv New;
  12.  
  13.  
  14.  
  15. int menu(int i);
  16. int listar_todos(New **data,New **ini,New **fim);
  17. void inserir(New **data,New **ini,New **fim);
  18. void primeiro_cadastro (New **data,New **ini,New **fim);
  19. void deletar(New **data,New **ini,New **fim);
  20. void editar(New **data,New **ini,New **fim);
  21.  
  22.  
  23.  
  24. int main()
  25. {
  26.  
  27. New *data=NULL,*fim=NULL,*ini=NULL;
  28.  
  29. int i;
  30.  
  31.  
  32.    do{     
  33.         i=menu(i);
  34.     switch (i)
  35.     {
  36.     case 1: inserir(&data,&ini,&fim);       break;
  37.     case 2: editar(&data,&ini,&fim);        break; 
  38.     case 3: deletar(&data,&ini,&fim);       break; 
  39.     case 4: listar_todos(&data,&ini,&fim);  break;
  40.     case 5: printf(" Finalizando...\n");    break;
  41.     default: printf(" \t\t     OPCAO INVALIDA !!!\n");
  42.     }
  43.  
  44.      }while (i!=5);
  45.  
  46. return 1;
  47.  
  48.  
  49. }// fim de main
  50.  
  51.  
  52. //funcao exibe o menu para o usuario
  53. int menu(int i)
  54. {
  55. printf("\n\t\t|--------------------|\n");
  56. printf(" \t           \tAgenda      \n");
  57. printf("  \t\t|--------------------|\n\n");
  58. printf(" 1- Inserir \n");
  59. printf(" 2- Editar \n");
  60. printf(" 3- Deletar \n");
  61. printf(" 4- Listar todos \n");
  62. printf(" 5- Sair \n");
  63. fflush(stdin);
  64. scanf("%d",&i);
  65. return i;
  66. }
  67.  
  68.  
  69. void inserir(New **data,New **ini,New **fim)
  70. {
  71.      New *aux=NULL;
  72.      int flag=0;
  73.  
  74.    
  75.    
  76.     if((*ini)==NULL)
  77.    {
  78.        primeiro_cadastro(&*data,&*ini,&*fim); (*data)=*ini; return;  
  79.    }
  80.        
  81.    
  82.    
  83.     else if(*ini!=NULL)
  84.     {        
  85.      
  86.                    
  87.                     aux=(New*)malloc(sizeof(New));
  88.                     aux->prox=NULL;
  89.                    
  90.          
  91.                     printf(" --- \t Nome \n\t\t");
  92.                     fflush(stdin);
  93.                     gets((aux)->nome);
  94.                     printf(" --- \t Idade\n\t\t");
  95.                     fflush(stdin);
  96.                     scanf("%d",&(aux)->idade);
  97.                     printf(" --- \t Estado Civil 1- Casado, 2- Solteiro, 3- Viuvo\n\t\t");
  98.                     fflush(stdin);
  99.                     scanf("%d",&(aux)->estado_civil);
  100.                     printf(" --- \t Telefone\n\t\t");  
  101.                     fflush(stdin);
  102.                     gets((aux)->telefone);  
  103.    
  104.  
  105.        (*data)=*ini;
  106.  
  107.     if(strcmp((*ini)->nome,aux->nome)>=0)
  108.         {
  109.          
  110.           aux->prox=(*data);
  111.           aux->prev=NULL;
  112.          (*data)->prev=aux;
  113.          (*ini)=aux;
  114.          (*fim)=(*data);
  115.          return;
  116.        }
  117.        
  118.      
  119.    
  120.      flag=1;
  121.    
  122.  
  123.  
  124.  
  125. while((*data)->prox!=NULL)
  126. {
  127.      
  128.                    
  129.        
  130.        if(strcmp((*data)->nome,aux->nome)>=0)
  131.       {
  132.    
  133.           aux->prox=(*data);
  134.           aux->prev=(*data)->prev;
  135.           (*data)->prev->prox=aux;
  136.           (*data)->prev=aux;
  137.          
  138.           flag=0;
  139.           break;
  140.      
  141.       }
  142.      
  143.        
  144.        
  145.       (*data)=(*data)->prox;
  146.    
  147. }
  148.      
  149. if(flag==1&&(*data)->prox==NULL)
  150. {
  151.  
  152.    if(strcmp((*data)->nome,aux->nome)>=0)
  153.        {
  154.         aux->prox=(*data);
  155.          aux->prev=(*data)->prev;
  156.          aux->prev->prox=aux;
  157.          (*data)->prev=aux;
  158.          
  159.         }
  160.     else  if(strcmp((*data)->nome,aux->nome)<=0)
  161.        {
  162.        
  163.        (*data)->prox=aux;
  164.          aux->prox=NULL;
  165.          aux->prev=(*data);
  166.          (*fim)=aux;
  167.         }  
  168. }
  169.  
  170. }
  171.  
  172. }  
  173.  
  174.    
  175.  
  176.    
  177.  
  178.  
  179.  
  180. void primeiro_cadastro (New **data,New **ini,New **fim)
  181. {
  182.        *data=(New*)malloc(sizeof(New));
  183.    
  184.    (*ini)=(*data);
  185.    (*ini)->prev=NULL;
  186.      
  187.      
  188.    
  189.    printf(" --- \t  nome \n\t\t");
  190.    fflush(stdin);
  191.     gets((*data)->nome);
  192.    printf(" --- \t idade\n\t\t");
  193.    fflush(stdin);
  194.    scanf("%d",&(*data)->idade);
  195.    printf(" --- \t  Estado Civil 1- Casado, 2- Solteiro, 3- Viuvo\n\t\t");
  196.    fflush(stdin);
  197.    scanf("%d",&(*data)->estado_civil);
  198.    printf(" --- \t  Telefone\n\t\t");
  199.    fflush(stdin);
  200.    gets((*data)->telefone);
  201.    (*ini)->prox=NULL;  
  202.     (*fim)=(*ini);
  203. }
  204.  
  205. int listar_todos(New **data,New **ini,New **fim)
  206. {
  207.   int temp=0;
  208.   New *temp1=NULL;
  209.   //condicao verifica se o  ini == NULL, se for = a NULL a lista estah vazia;
  210.   if((*ini)==NULL)
  211.   {
  212.       printf(" \t\t\t -------   Nenhum Dado cadastrado --------- \n");
  213.       printf(" \t\t\t Se quiser cadastrar digite 1, se nao digite 0\n\n");
  214.       scanf("%d",&temp);
  215.       if(temp==1)   {primeiro_cadastro(&(*data),&(*ini),&(*fim)); return 0;}    else {return 0;}
  216.   }
  217.  
  218. for(temp1=*ini;temp1!=NULL;temp1=temp1->prox)
  219.     {
  220.      printf(" \n\n\t\t\t nome: %s \n",temp1->nome);
  221.      printf("      \t\t\tidade: %d\n",temp1->idade);
  222.      if(temp1->estado_civil==1)
  223.      printf("      \t\t\testado civil: Casado \n");
  224.      else if(temp1->estado_civil==2)
  225.      printf("      \t\t\t estado civil: Solteiro\n");
  226.      else if(temp1->estado_civil==3)
  227.      printf("        \t\t\testado civil: Viuvo \n");
  228.      printf("      \t\t\t Telefone: %s",temp1->telefone);
  229.     }
  230.  
  231.  return 0;
  232. }
  233.  
  234. void deletar(New **data,New **ini,New **fim)
  235. {
  236. char nome[30];
  237.      New *temp=NULL;
  238.      
  239.      
  240.         if(*ini==NULL)
  241.          {
  242.              printf(" \t\t\t Lista vazia \n");
  243.              printf(" \t\t\t Insira algum dado antes de deletar \n");
  244.              return;
  245.          }
  246.  
  247.  
  248.       fflush(stdin);
  249.       printf(" Digite o nome do cadastro a ser deletado \n");
  250.       printf(" \n\n\t\t\t*** Nomes Disponiveis *** \n");
  251.    
  252.       for(*data=*ini;*data!=NULL;*data=(*data)->prox) printf("%s \n",(*data)->nome); gets(nome);
  253.       for(*data=*ini;*data!=NULL;*data=(*data)->prox)
  254.       {
  255.           if(strcmp((*data)->nome,nome)==0) break;
  256.       }
  257.      
  258.      if((*data)!=NULL)
  259.      {
  260.        
  261.          if(*ini==*fim)
  262.           {
  263.               (*ini)=NULL;
  264.               (*fim)=NULL;
  265.               return;
  266.           }
  267.      
  268.          else if(*data==*ini&&*ini!=*fim)
  269.          {
  270.              (*ini)=(*data)->prox;
  271.              (*ini)->prev=NULL;
  272.              free(*data);
  273.              
  274.              printf(" \t\t\t Cadastro %s deletado com sucesso !!! \n\n",nome);
  275.          }
  276.          else if(*data==*fim&&*ini!=*fim&&*data!=*ini)
  277.          {
  278.              (*fim)=(*data)->prev;
  279.              (*fim)->prox=NULL;
  280.              free(*data);
  281.              printf(" \t\t\t Cadastro %s deletado com sucesso !!! \n\n",nome);
  282.              }
  283.         else if(*ini!=*fim&&*data!=*ini&&*data!=*fim)
  284.        {
  285.           (*data)->prev->prox=(*data)->prox;
  286.           (*data)->prox->prev=(*data)->prev;
  287.           free((*data));
  288.            
  289.           printf(" \t\t\t Cadastro %s deletado com sucesso !!! \n\n",nome);
  290.          }
  291.        
  292.   }
  293.        else printf("\t\t\t--------- Numero nao encontrado \n\n\n -----------");
  294.   for(temp=*ini;temp!=NULL;temp=temp->prox)*fim=temp;
  295.  
  296.    }
  297.  
  298.  
  299.  
  300. void editar(New **data,New **ini,New **fim)
  301. {
  302.     char nome[30];  
  303.      New *temp=NULL;
  304.    
  305.      
  306.      if(*ini==NULL)
  307.          {
  308.              printf(" \t\t\t Lista vazia \n");
  309.              printf(" \t\t\t Insira algum dado antes de editar \n");
  310.              return;
  311.          }
  312.  
  313.  
  314.        fflush(stdin);
  315.        printf(" Digite o nome do cadastro a ser editado \n");
  316.        printf(" \n\n\t\t\t*** Nomes Disponiveis *** \n\n");
  317.       for(*data=*ini;*data!=NULL;*data=(*data)->prox) printf("\t\t\t     %s \n",(*data)->nome); gets(nome);
  318.       for(*data=*ini;*data!=NULL;*data=(*data)->prox)
  319.       {
  320.           if(strcmp((*data)->nome,nome)==0) break;
  321.       }
  322.      
  323.       if((*data)!=NULL)
  324.       {
  325.           if(*ini==*fim)
  326.           {
  327.               (*ini)=NULL;
  328.               (*fim)=NULL;
  329.           }
  330.      
  331.          else  if(*data==*ini&&*ini!=*fim)
  332.          {
  333.              (*ini)=(*data)->prox;
  334.              (*ini)->prev=NULL;
  335.              free(*data);
  336.          }
  337.          else if(*data==*fim&&*ini!=*fim&&*data!=*ini)
  338.          {
  339.              (*fim)=(*data)->prev;
  340.              (*fim)->prox=NULL;
  341.              free(*data);
  342.              }
  343.         else if(*ini!=*fim&&*data!=*ini&&*data!=*fim)
  344.        {
  345.           (*data)->prev->prox=(*data)->prox;
  346.           (*data)->prox->prev=(*data)->prev;
  347.           free((*data));
  348.          }
  349.          
  350.          
  351.       }
  352.      else
  353.      {printf("\t\t\t ------- Nome nao encontrado ------- \n"); return; }
  354.    
  355.    printf("\nEdite: \n\n"); inserir(&(*data),&(*ini),&(*fim));
  356.   for(temp=*ini;temp!=NULL;temp=temp->prox)*fim=temp;
  357.  printf(" O *fim->nome eh %s \t e o *ini->nome eh %s",(*fim)->nome,(*ini)->nome);
  358. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement