Nerviie

Listas Duplamente encadeadas

May 19th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.41 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. /*Listas Duplamente Encadeadas*/
  5.  
  6. typedef struct lista{
  7.     char nome[30];
  8.     int num;
  9.     struct lista *prox,*ant;
  10. }LISTA;
  11.  
  12. LISTA *inicio,*fim,*temp,*aux,*ant;
  13.  
  14. /*Inserir Inicio*/
  15.  
  16. void InserirInicio(){
  17.     int x;
  18.     do{
  19.         temp=(LISTA*)malloc(sizeof(LISTA));
  20.         printf("Numero: ");
  21.         scanf("%d",&temp->num);
  22.         printf("Nome: ");
  23.         do{
  24.             gets(temp->nome);
  25.         }while(strlen(temp->nome)==0);
  26.         if(inicio==NULL){
  27.             inicio=temp;
  28.             inicio->prox=inicio->ant=NULL;
  29.         }
  30.         else{
  31.             temp->prox=inicio;
  32.             inicio->ant=temp;
  33.             temp->ant=NULL;
  34.             inicio=temp;
  35.         }
  36.         printf("Continuar? : ");
  37.         scanf("%d",&x);
  38.     }while(x==1);
  39. }
  40.  
  41. /*Inserção no fim*/
  42. void InserirFim(){
  43.     int c;
  44.     do{
  45.         temp=(LISTA*)malloc(sizeof(LISTA));
  46.         printf("Numero: ");
  47.         scanf("%d",&temp->num);
  48.         printf("Nome: ");
  49.         do{
  50.             gets(temp->nome);
  51.         }while(strlen(temp->nome)==0);
  52.         if(inicio==NULL){
  53.             inicio=temp;
  54.             fim=temp;
  55.             inicio->prox=inicio->ant=NULL;
  56.         }
  57.         else{
  58.             fim->prox=temp;
  59.             temp->ant=fim;
  60.             temp->prox=NULL;
  61.             fim=temp;
  62.         }
  63.         printf("Continuar?: ");
  64.         scanf("%d",&c);
  65.     }while(c==1);
  66. }
  67.  
  68. /*Consulta Global*/
  69. void consulta(){
  70.     temp=inicio;
  71.     while(temp!=NULL){
  72.         printf("Numero:%d\nNome :%s\n\n",temp->num,temp->nome);
  73.         temp=temp->prox;
  74.     }
  75. }
  76.  
  77. /*Inserir ordenada ascendentemente*/
  78.  
  79. void InserirAscendentemente(){
  80.     int x;
  81.     do{
  82.         temp=(LISTA*)malloc(sizeof(LISTA));
  83.         printf("Numero: ");
  84.         scanf("%d",&temp->num);
  85.         printf("Nome: ");
  86.         do{
  87.             gets(temp->nome);
  88.         }while(strlen(temp->nome)==0);
  89.         if(inicio==NULL){
  90.             inicio=temp;
  91.             inicio->prox=inicio->ant=NULL;
  92.         }
  93.         else{
  94.             if(temp->num < inicio->num){
  95.                 temp->prox=inicio;
  96.                 inicio->ant=temp;
  97.                 temp->ant=NULL;
  98.                 inicio=temp;
  99.             }
  100.             else{
  101.                 aux=inicio;
  102.                 while(aux->prox!=NULL && temp->num > aux->num){
  103.                     aux=aux->prox;
  104.                 }
  105.                 if(temp->num < aux->num){
  106.                     aux->prox=temp;
  107.                     temp->ant=aux;
  108.                     temp->prox=NULL;
  109.                 }
  110.                 else{
  111.                     temp->prox=aux;
  112.                     temp->ant=aux;
  113.                     aux->ant->prox=temp;
  114.                     aux->ant=temp;
  115.                 }
  116.             }
  117.         }
  118.         printf("Continuar?: ");
  119.         scanf("%d",&x);
  120.     }while(x==1);
  121. }
  122.  
  123. void InserirDescendentemente(){
  124.     int x;
  125.     do{
  126.         temp=(LISTA*)malloc(sizeof(LISTA));
  127.         printf("Numero: ");
  128.         scanf("%d",&temp->num);
  129.         printf("Nome: ");
  130.         do{
  131.             gets(temp->nome);
  132.         }while(strlen(temp->nome)==0);
  133.         if(inicio==NULL){
  134.             inicio=temp;
  135.             inicio->prox=inicio->ant=NULL;
  136.         }
  137.         else{
  138.             if(temp->num > inicio->num){
  139.                 temp->prox=inicio;
  140.                 inicio->ant=temp;
  141.                 temp->ant=NULL;
  142.                 inicio=temp;
  143.             }
  144.             else{
  145.                 aux=inicio;
  146.                 while(aux->prox!=NULL && temp->num < aux->num){
  147.                     aux=aux->prox;
  148.                 }
  149.                 if(temp->num > aux->num){
  150.                     aux->prox=temp;
  151.                     temp->ant=aux;
  152.                     temp->prox=NULL;
  153.                 }
  154.                 else{
  155.                     temp->prox=aux;
  156.                     temp->ant=aux;
  157.                     aux->ant->prox=temp;
  158.                     aux->ant=temp;
  159.                 }
  160.             }
  161.         }
  162.         printf("Continuar?: ");
  163.         scanf("%d",&x);
  164.     }while(x==1);
  165. }
  166. void eliminar(){
  167.     int x;
  168.     printf("Numero a eliminar:");
  169.     scanf("%d",&x);
  170.     aux=inicio;
  171.     while(x!=temp->num && aux->prox!=NULL){
  172.         aux=aux->prox;
  173.     }
  174.     if(x==temp->num){
  175.         if(aux==inicio){
  176.             inicio=inicio->prox;
  177.             if(inicio!=NULL){
  178.                 inicio->ant=NULL;
  179.             }
  180.         }
  181.         else{
  182.             aux->ant->prox=aux->prox;
  183.             if(aux->prox!=NULL){
  184.                 aux->prox->ant=aux->ant;
  185.             }
  186.         }
  187.         free(aux);
  188.         printf("Foi eliminado um nodo!\n");
  189.     }
  190.     else{
  191.         printf("Não existe\n");
  192.     }
  193. }
  194.  
  195. /*Main*/
  196. int main(){
  197.     int op;
  198.     do{
  199.         printf("Listas Simplesmente Encadeadas\n");
  200.         printf("1-Inserir no inicio\n");
  201.         printf("2-Inserir no fim\n");
  202.         printf("3-CONSULTA GLOBAL\n");
  203.         printf("4-Inserir ordenada ascendentemente\n");
  204.         printf("5-Inserir ordenada descendentemente\n");
  205.         printf("6-Eliminacao\n");
  206.         printf("7-Sair\n");
  207.         printf("Escolha: ");
  208.         scanf("%d",&op);
  209.         system("cls");
  210.         switch(op){
  211.             case 1:InserirInicio();
  212.                 break;
  213.             case 2:InserirFim();
  214.                 break;
  215.             case 3:consulta();
  216.                 break;
  217.             case 4:InserirAscendentemente();
  218.                 break;
  219.             case 5:InserirDescendentemente();
  220.                 break;
  221.             case 6:eliminar();
  222.                 break; 
  223.         }
  224.     }while (op!=7);
  225. }
Add Comment
Please, Sign In to add comment