Advertisement
renix1

Exemplo, Ana

Apr 6th, 2019
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.28 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. typedef struct aluno {
  6.     char *nome;
  7.     int idade;
  8.     struct aluno *proxAluno;
  9. } estudante;
  10.  
  11.  
  12. void inserir(estudante **inicio, char *nome, int idade) {
  13.     estudante *novoEstudante;
  14.     novoEstudante = malloc(sizeof(estudante));
  15.     novoEstudante->nome = nome;
  16.     novoEstudante->idade = idade;
  17.     if (*inicio == NULL) {
  18.         novoEstudante->proxAluno = NULL;
  19.         *inicio = novoEstudante;
  20.     } else {
  21.         novoEstudante->proxAluno = *inicio;
  22.         *inicio = novoEstudante;
  23.     }
  24. }
  25.  
  26. void remover(estudante **inicio) {
  27.     estudante *estudanteAtual;
  28.     if (*inicio != NULL) {
  29.         estudanteAtual = *inicio;
  30.         printf("Aluno %s removido!\n", estudanteAtual->nome);
  31.         *inicio = (*inicio)->proxAluno;
  32.         free(estudanteAtual);
  33.     }
  34. }
  35.  
  36. void listar(estudante **inicio) {
  37.     estudante *elementoAtual;
  38.     elementoAtual = *inicio;
  39.     while (elementoAtual != NULL)
  40.     {
  41.         printf("Estudante %s com %d anos\n", elementoAtual->nome, elementoAtual->idade);
  42.         elementoAtual = elementoAtual->proxAluno;
  43.     }
  44. }
  45.  
  46. int main (void) {
  47.     estudante *inicio = NULL;
  48.     char *nome = "Reni";
  49.     int idade = 18;
  50.     inserir(&inicio, nome, idade);
  51.     listar(&inicio);
  52.  
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement