Advertisement
Guest User

Untitled

a guest
Nov 12th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct aluno{
  4.     int matricula;
  5.     char nome[15];
  6.     char curso[20];
  7.     int idade;
  8.     struct aluno *prox;
  9. }No;
  10.  
  11. No*criar(){
  12.     return NULL;
  13. }
  14.  
  15. No *inserir(No *inicio)
  16. {
  17.     No *aux;
  18.  
  19.     No *novo = (No *)malloc(sizeof(No));
  20.     novo->prox = NULL;
  21.     setbuf(stdin, NULL);
  22.     printf("Forneca a matricula: ");
  23.     scanf("%d", &novo->matricula);
  24.     printf("Forneca o nome: ");
  25.     scanf("%s", novo->nome);
  26.     printf("Forneca o curso: ");
  27.     scanf("%s", novo->curso);
  28.     printf("Forneca a idade: ");
  29.     scanf("%d", &novo->idade);
  30.     if(inicio == NULL)
  31.     {
  32.         inicio = novo;
  33.         inicio->prox = NULL;
  34.     }else
  35.     {
  36.         aux = inicio;
  37.         while(aux->prox != NULL)
  38.         {
  39.             aux = aux->prox;
  40.         }
  41.         aux->prox = novo;
  42.         novo->prox = NULL;
  43.     }
  44.     return inicio;
  45. }
  46.  
  47. void imprimir(No *inicio)
  48. {
  49.     No *aux;
  50.     aux = inicio;
  51.     if(aux == NULL)
  52.     {
  53.         printf("Lista vazia maninho");
  54.     }
  55.     while(aux != NULL)
  56.     {
  57.         printf("Nome: %s", aux->nome);
  58.         printf("\ncurso: %s", aux->curso);
  59.         printf("\nIdade: %d", aux->idade);
  60.         printf("\nmatricula: %d\n\n", aux->matricula);
  61.  
  62.         aux = aux->prox;
  63.     }
  64. }
  65.  
  66. No *buscar(No *inicio, int mat)
  67. {
  68.     No *aux;
  69.     aux = inicio;
  70.  
  71.     while(aux != NULL)
  72.     {
  73.         if(aux->matricula == mat)
  74.         {
  75.             printf("Elemento encontrado\n");
  76.             printf("Nome: %s", aux->nome);
  77.             printf("\ncurso: %s", aux->curso);
  78.             printf("\nIdade: %d", aux->idade);
  79.             printf("\nmatricula: %d\n\n", aux->matricula);
  80.             return aux;
  81.         }
  82.         aux = aux->prox;
  83.     }
  84.     return NULL;
  85. }
  86. No *excluir(No *inicio, int mat)
  87. {
  88.     No *aux, *aux_ant;
  89.     aux = inicio;
  90.     aux_ant = NULL;
  91.  
  92.     while(aux != NULL && aux->matricula != mat)
  93.     {
  94.         aux_ant = aux;
  95.         aux = aux->prox;
  96.     }
  97.     if(aux == NULL)
  98.     {
  99.         printf("Matricula nao encontrada\n");
  100.         return inicio;
  101.     }
  102.     if(aux_ant == NULL)
  103.     {
  104.         aux = aux->prox;
  105.         return aux;
  106.     }else
  107.     {
  108.         aux_ant->prox = aux->prox;
  109.     }
  110.  
  111.     free(aux);
  112.     return inicio;
  113. }
  114.  
  115.  
  116. void finalizar(No *inicio)
  117. {
  118.     No *aux;
  119.     aux = inicio;
  120.     while(aux != NULL)
  121.     {
  122.         inicio = inicio->prox;
  123.         free(aux);
  124.         aux = inicio;
  125.     }
  126. }
  127.  
  128. int main()
  129. {
  130.     No *inicio = criar();
  131.     int y, x;
  132.     x = 0;
  133.     while(x != 3)
  134.     {
  135.         printf("Escolha a opcao desejada:\n");
  136.         printf("\n1 - Inserir novo aluno");
  137.         printf("\n2 - Imprimir lista de alunos");
  138.         printf("\n3 - Buscar");
  139.         printf("\n4 - Excluir");
  140.         printf("\n5 - Sair\n\n");
  141.  
  142.         scanf("%d", &y);
  143.  
  144.         if(y == 1)
  145.         {
  146.             inicio = inserir(inicio);
  147.         }
  148.         if(y == 2)
  149.         {
  150.             imprimir(inicio);
  151.         }
  152.  
  153.         if(y == 3)
  154.         {
  155.             int mat;
  156.             printf("Digite o numero da matricula do Aluno: ");
  157.             scanf("%d", &mat);
  158.             buscar(inicio, mat);
  159.         }
  160.         if(y == 4)
  161.         {
  162.             int mat;
  163.             printf("Digite o numero da matricula do Aluno: ");
  164.             scanf("%d", &mat);
  165.             excluir(inicio, mat);
  166.         }
  167.         if(y == 5)
  168.         {
  169.             x = 3;
  170.         }
  171.     }
  172.  
  173.     finalizar(inicio);
  174.     return 0;
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement