Nerviie

Listas Simplesmente encadeadas

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