Advertisement
Guest User

Untitled

a guest
Nov 12th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.94 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. // 044 - Exibir na tela
  4.  
  5. /*** ESTRUTURAS ***/
  6. typedef struct
  7. {
  8.     int matricula;
  9.     char nome[30];
  10.     float n1,n2,n3;
  11. }ALUNO;
  12.  
  13. typedef struct elemento
  14. {
  15.     ALUNO dados;
  16.     struct elemento *prox; // estou criando uma escrutura que aponta para outro nó
  17. }NO;
  18.  
  19. /*** PROTOTIPO E FUNÇAO ***/
  20. NO* cria_lista();
  21. int exibir (NO **li);
  22. int inserir_inicio(NO **li, ALUNO al);
  23. int inserir_final(NO **li, ALUNO al);
  24. int inserir_ordenado(NO **li, ALUNO al);
  25. int lista_vazia(NO **li);
  26. int remover_final(NO **li);
  27. int remover_inicio(NO **li);
  28. int remover_matricula(NO **li, int matricula);
  29. int pesquisar_matricula(NO **li, int matricula);
  30. void excluir(NO **li);
  31. void iniciar();
  32.  
  33. /*** MAIN ***/////////////////////////////////////////////////////////////////////////////////////////////
  34. int main ()
  35. {
  36.     iniciar();
  37.  
  38.     int check;
  39.     NO *raiz = NULL;
  40.     raiz = cria_lista();
  41.  
  42.     ALUNO al  = {180, "00joao", 1.2,2.2,3.3};
  43.     ALUNO al2 = {141, "01gabriel", 1.2,2.2,3.3};
  44.     ALUNO al3 = {13, "02rafael", 1.2,2.2,3.3};
  45.     ALUNO al4 = {912, "03marcos", 1.2,2.2,3.3};
  46.     ALUNO al5 = {384, "04otavio", 1.2,2.2,3.3};
  47.     ALUNO al6 = {9, "teste ordenado", 1.2,2.2,3.3};
  48.     // INSERIR NO INICIO
  49.     inserir_ordenado(&raiz, al); // estou passando o endereço do ponteiro raiz, e não o endereço aonde ela aponta
  50.     inserir_ordenado(&raiz, al2);
  51.     inserir_ordenado(&raiz, al3);
  52.     inserir_ordenado(&raiz, al4);
  53.     inserir_ordenado(&raiz, al5);
  54.     inserir_ordenado(&raiz, al6);
  55.    
  56.     exibir(&raiz);
  57.     pesquisar_matricula(&raiz,13);
  58.     remover_matricula(&raiz,13);
  59.     pesquisar_matricula(&raiz,13);
  60.    
  61.     exibir(&raiz);
  62.    
  63. }
  64. ///////////////////////////////////////////////////////////////////////////////////////////////////////////
  65.  
  66. void iniciar(){
  67.     int opcao;
  68.     int check;
  69.     int mat;
  70.     float n1, n2, n3;
  71.     char nome[20];
  72.     ALUNO reg;
  73.     NO *raiz = NULL;
  74.    
  75.     do{
  76.         system("cls");
  77.         printf("Escolha uma opcao: \n\n");
  78.         printf("1 - Criar lista\n");
  79.         printf("2 - Inserir registro no comeco da lista\n");
  80.         printf("3 - Inserir registro no final da lista\n");
  81.         printf("4 - Inserir registro em ordem de matricula\n");
  82.         printf("5 - Exibir todos os registros da lista\n");
  83.         printf("6 - Menu de exclusao\n");
  84.         printf("7 - Tamanho da lista\n");
  85.         printf("0 - Sair\n");
  86.         printf("Opcao: ");
  87.         scanf(" %d",&opcao);
  88.        
  89.         // não deixa acessar as opções caso a lista nao exista
  90.         if(opcao != 1 && raiz == NULL){
  91.             printf("Por favor crie uma lista para acessar essa opcao\n");
  92.             system("Pause");
  93.             continue;
  94.         }
  95.        
  96.         if(opcao == 2 || opcao == 3 || opcao == 4){
  97.             printf("\nDigite o numero da matricula: ");
  98.             scanf(" %d",&reg.matricula);
  99.            
  100.             printf("Digite o nome: ");
  101.             scanf(" %s",&reg.nome);        
  102.            
  103.         }
  104.        
  105.         switch (opcao){
  106.             case 1:
  107.                 if(raiz == NULL){
  108.                     raiz = cria_lista();
  109.                     if (raiz != NULL) printf("\nLista Criada!\n\n");
  110.                     else printf("\nErro: nao foi possivel criar a lista");
  111.                     system("pause");
  112.                 }else{
  113.                     printf("\nLista ja foi criada\n");
  114.                     system("pause");
  115.                 }              
  116.                            
  117.                 break;
  118.             case 2:
  119.                 check = inserir_inicio(&raiz,reg);
  120.  
  121.                 if (check == 1) printf("\nSucesso: Adicionado no inicio da lista\n\n");
  122.                 else printf("\n\nErro: lista cheia, ou nao existe\n\n");
  123.                
  124.                 system("pause");               
  125.                 break;
  126.             case 3:
  127.                 check = inserir_final(&raiz,reg);
  128.  
  129.                 if (check == 1) printf("\nSucesso! Adicionado no final da lista\n\n");
  130.                 else printf("\n\nErro: lista cheia, ou nao existe\n\n");
  131.                
  132.                 system("pause");               
  133.                 break;
  134.             case 4:
  135.                 check = inserir_ordenado(&raiz,reg);           
  136.  
  137.                 if (check == 1) printf("\nSucesso! Adicionado ordenamente na lista\n\n");
  138.                 else printf("\n\nErro: lista cheia, ou nao existe\n\n");
  139.  
  140.                 system("pause");
  141.                 break;
  142.             case 5:
  143.                 check = exibir(&raiz);
  144.                 if(check == 0){
  145.                     printf("\nLista vazia, ou nao foi criada\n\n");
  146.                 }
  147.                 system("pause");
  148.                 break;
  149.             case 6:
  150.                 excluir(&raiz);
  151.  
  152.                 system("pause");
  153.                 break;
  154.             case 7:
  155.                 /*check = tamanho_lista(&raiz);
  156.                 if (check < 0)
  157.                     printf("\nLista vazia\n\n");
  158.                 else
  159.                     printf("\nTamanho da lista %d\n\n", tamanho_lista(&raiz));
  160.                 system("pause");*/
  161.                 break;
  162.             case 0:
  163.                 printf("\nSaindo...\n");
  164.                 break;
  165.             default:
  166.                 system("cls");
  167.                 printf("Opcao invalida, tente novamente\n\n");
  168.         }
  169.     }while(opcao != 0 );
  170. }
  171.  
  172. /*** Exibir na tela ***/
  173. int exibir(NO **li)
  174. {
  175.     if (lista_vazia(li) == (-1)) {
  176.         return 0;
  177.     }else {
  178.         NO *temp;
  179.         temp = (*li)->prox;
  180.         while (temp != NULL ){
  181.             printf("Matricula: %d, Nome: %s \n",temp->dados.matricula,temp->dados.nome);
  182.             temp = temp->prox;
  183.         }
  184.         return 1;
  185.     }
  186. }
  187.  
  188. /*** CRIA LISTA ***/
  189. NO* cria_lista()
  190. {
  191.  
  192.     NO *ptr;
  193.     ptr = (NO*) malloc(sizeof(NO));
  194.     ptr->prox = NULL;
  195.     return ptr;
  196. }
  197. /*** LISTA VAZIA ***/
  198. int lista_vazia(NO **li){
  199.     return ((*li)->prox == NULL);
  200. }
  201.  
  202. /*** INSERIR NO INICIO ***/
  203. int inserir_inicio(NO **li, ALUNO al)
  204. {
  205.     if(*li == NULL) {
  206.         printf("Lista nao criada\n");
  207.         return -1;
  208.     }
  209.    
  210.     NO *novo = (NO*) malloc(sizeof(NO));
  211.    
  212.     if(novo != NULL)
  213.     {
  214.         novo->dados = al;
  215.         novo->prox = (*li)->prox;
  216.         (*li)->prox = novo;
  217.         return 1;
  218.     }
  219.    
  220. }
  221. /*** REMOVER INICIO ***/
  222. int remover_inicio(NO **li){
  223.     if (lista_vazia(li)) {
  224.         printf("entrou");
  225.         return 0;
  226.     }
  227.  
  228.     NO *temp;
  229.     temp = (*li)->prox;
  230.     (*li)->prox = temp->prox;
  231.     free(temp);
  232.     return 1;
  233.    
  234. }
  235. /*** INSERIR NO FINAL ***/
  236. int inserir_final(NO **li, ALUNO al)
  237. {
  238.     if(*li == NULL) return -1;
  239.    
  240.     NO *temp;
  241.     temp = (*li);
  242.    
  243.     while (temp->prox != NULL){
  244.         temp = temp->prox;
  245.     }
  246.    
  247.     if(temp->prox == NULL){
  248.         NO *novo = (NO*) malloc(sizeof(NO));
  249.         if (novo != NULL){
  250.             novo->dados = al;
  251.             temp->prox = novo;
  252.             novo->prox = NULL;
  253.            
  254.             return 1;
  255.         }  
  256.     }
  257. }
  258.  
  259. /*** REMOVER FINAL ***/
  260. int remover_final(NO **li){
  261.     if (lista_vazia(li) == (-1)) {
  262.         return -1;
  263.     }
  264.    
  265.     NO *temp;
  266.     temp = (*li)->prox;
  267.    
  268.     while(temp->prox->prox != NULL){
  269.         temp = temp->prox;
  270.     }
  271.     if(temp->prox->prox == NULL){
  272.         free(temp->prox);
  273.         temp->prox = NULL;
  274.        
  275.         return 1;
  276.     }
  277. }
  278.  
  279. /*** Pesquisar por matricula ***/
  280. int pesquisar_matricula(NO **li, int matricula){
  281.     if (lista_vazia(li)){
  282.         return 1;
  283.     }
  284.     NO *temp = (*li)->prox;
  285.  
  286.     while(temp->dados.matricula != matricula && temp->prox != NULL){
  287.         temp = temp->prox;
  288.  
  289.     }
  290.    
  291.     if (matricula == temp->dados.matricula){
  292.         printf("\nEncontrado!\n");
  293.         printf("Nome da pessoa (%d): %s\n", temp->dados.matricula, temp->dados.nome);
  294.         return 1;
  295.     }else {
  296.         printf("Matricula %d nao encontrada \n\n",matricula);
  297.         return 0;
  298.     }
  299.    
  300. }
  301.  
  302. /*** remover por matricula ***/
  303. int remover_matricula(NO **li, int matricula){
  304.     if (lista_vazia(li)){
  305.         printf("\nLista vazia\n");
  306.         return 1;
  307.     }
  308.     NO *temp = (*li);
  309.     NO *temp2;
  310.    
  311.     while(temp->prox->dados.matricula != matricula && temp != NULL){
  312.         temp=temp->prox;
  313.     }
  314.    
  315.     if (temp->prox->dados.matricula == matricula){
  316.         printf("\nEncontrado! \nRemovendo matricula %d\n", temp->prox->dados.matricula);
  317.         temp2 = temp->prox->prox;
  318.         temp->prox = temp2;
  319.         return 1;
  320.     }
  321.     else {
  322.         printf("\nNao encontrado\n");
  323.     }
  324. }
  325. /*** inserir ordenado ***/
  326. int inserir_ordenado(NO **li, ALUNO al){
  327.     if(*li == NULL) return -1;
  328.        
  329.     // Chama a funçao inserir inicio se a lista estiver vazia
  330.     if((*li)->prox == NULL){       
  331.         return inserir_inicio(li, al);
  332.     }  
  333.    
  334.     NO *novo = (NO*) malloc(sizeof(NO));
  335.  
  336.     NO *temp = (*li);
  337.     printf("entrou %d, valor de temp: %d\n",al.matricula,temp->prox->dados.matricula);
  338.     while (al.matricula >= temp->prox->dados.matricula){
  339.  
  340.         temp = temp->prox;
  341.        
  342.         if(temp->prox == NULL){
  343.             novo->dados = al;
  344.             novo->prox = NULL   ;
  345.             temp->prox = novo;
  346.             return 1;
  347.         }
  348.     }
  349.    
  350.     if (al.matricula <= temp->prox->dados.matricula){
  351.         novo->dados = al;
  352.         novo->prox = temp->prox;
  353.         temp->prox = novo;
  354.         return 1;
  355.     }
  356.    
  357. }
  358. /*** EXCLUIR DA LISTA ***/
  359. void excluir(NO **li){
  360.  
  361.     int opcao, check, matricula;
  362.  
  363.         system("CLS");
  364.         printf("1 - Remover inicio da lista \n2 - Remover final da lista \n3 - Remover por identificador \n0 - Voltar \nOpcao: ");
  365.         scanf(" %d",&opcao);
  366.         switch(opcao){
  367.             case 1:
  368.                
  369.                 check = remover_inicio(li);        
  370.                 if (check == 1) printf("\n\nSucesso! Removido do inicio da lista\n\n");
  371.                 else printf("\n\nErro: lista vazia, ou nao foi criada\n\n");
  372.                 system("pause");
  373.                 break;
  374.             case 2:
  375.                 check = remover_final(li);         
  376.  
  377.                 if (check == 1) printf("\n\nSucesso! Removido do final da lista\n\n");
  378.                 else printf("\n\nErro: lista vazia, ou nao foi criada\n\n");
  379.                 system("pause");
  380.                 break;
  381.             case 3:
  382.                 printf("\n\nDigite o numero da matricula para remover: ");
  383.                 scanf(" %d", &matricula);
  384.                 check = remover_matricula(li, matricula);
  385.  
  386.                 if (check == 1) printf("\nSucesso! Removido com sucesso\n\n");
  387.                 else printf("\n\nErro: Erro: lista vazia, ou nao foi criada\n\n");
  388.                 system("pause");
  389.                 break;
  390.             case 0:
  391.                 break;
  392.             default:
  393.                 printf("\n\nOpcao invalida, tente novamente...");
  394.                 system("pause");
  395.                 break;
  396.         }
  397. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement