Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5. #include <locale.h>
  6.  
  7. typedef struct no{
  8. char nome[30];
  9. int matricula, turma;
  10. float coef;
  11. struct no *prox;
  12. struct no *ant;
  13. }noAluno;
  14. noAluno *prim=NULL;
  15. noAluno *ult=NULL;
  16.  
  17. bool insereAluno (noAluno *insere){
  18.     noAluno *atual = prim;
  19.     noAluno *ant = NULL;
  20.     noAluno *novo = (noAluno*) malloc(sizeof(noAluno));
  21.  
  22.             novo = insere;
  23.  
  24.  
  25.     if (prim == NULL){
  26.         novo->prox = novo;
  27.         novo->ant = novo;
  28.         prim = novo;
  29.         ult = novo;
  30.         }
  31.         else{
  32.             while (strcmp(atual->nome, insere->nome) > 0){
  33.                 ant = atual;
  34.                 atual = atual->prox;
  35.  
  36.                 if (atual   == prim)
  37.                     break;
  38.             }
  39.             if (ant == NULL){
  40.                 prim = novo;
  41.                 ult->prox = prim;
  42.             }
  43.             else
  44.                 ant->prox = novo;
  45.  
  46.  
  47.             novo->prox = atual;
  48.             novo->ant = ant;
  49.  
  50.             if (atual == prim)
  51.                 ult = novo;
  52.         }
  53.         return true;
  54.     }
  55.  
  56. void imprimeLista (){
  57.     system("cls");
  58. noAluno *atual = prim;
  59. while (atual != NULL){
  60.             printf("Nome: %sMatrícula: %d\nTurma: %d\nCoeficiente: %.2f\n\n", atual->nome, atual->matricula, atual->turma, atual->coef);
  61.             atual = atual ->prox;
  62.  
  63.     if(atual == prim)
  64.         break;
  65.         }
  66. }
  67.  
  68. bool removeAluno (int mat){
  69. noAluno *atual = prim;
  70. noAluno *ant = NULL;
  71.  
  72. if (atual != NULL){
  73. while ((atual->matricula != mat) && (atual != NULL)){
  74. ant = atual;
  75. atual = atual->prox;
  76.  
  77.         if (atual == prim)
  78.             break;
  79.         }
  80.         if (ant == NULL)
  81.             prim = atual->prox;
  82.         else
  83.             ant->prox = atual->prox;
  84.  
  85.         free(atual);
  86.         return true;
  87.     }
  88.     else{
  89.         printf("Não existe essa matrícula cadastrada\n");
  90.         return false;
  91.         }
  92. }
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99. int main (int argc, char *argv[]){
  100. setlocale(LC_ALL, "Portuguese");
  101. int a = 1;
  102. printf ("Digite os dados do aluno\n");
  103. while (a != 0){
  104.         noAluno *aux = (noAluno*) malloc (sizeof(noAluno));
  105.  
  106.         printf ("Nome: ");
  107.         fflush(stdin);
  108.         fgets(aux->nome, 30, stdin);
  109.  
  110.         printf ("Matricula: ");
  111.         scanf ("%d", &aux->matricula);
  112.  
  113.         printf ("Turma: ");
  114.         scanf ("%d", &aux->turma);
  115.  
  116.         printf ("Coeficiente: ");
  117.         scanf ("%f", &aux->coef);
  118.  
  119.  
  120.         insereAluno(aux);
  121.  
  122.  
  123.         printf("Digite 0 para encerrar a inserção de aluno\n       1 para inserir outro aluno: ");
  124.         scanf ("%d", &a);
  125. }
  126. imprimeLista();
  127.  
  128. int matri;
  129. printf("Digite o número de matrícula à ser removido: ");
  130. scanf("%d", &matri);
  131. removeAluno(matri);
  132.  
  133. imprimeLista();
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement