fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define MAX 3
  5.  
  6. typedef struct alunos {
  7. int matricula;
  8. char nome[30];
  9. float nota1, nota2;
  10. } Alunos;
  11.  
  12. typedef struct lista {
  13. int quant;
  14. Alunos dados[MAX];
  15.  
  16. } Lista;
  17.  
  18. Lista* cria_Lista() {
  19. Lista* l = malloc(sizeof(Lista));
  20. if (l != NULL) l->quant = 0;
  21. return l;
  22. }
  23.  
  24. void libera_Lista(Lista* l) {
  25. free(l);
  26. }
  27.  
  28. int insere_final(Lista* l, Alunos al) {
  29. if (l == NULL) { // || lista_Cheia(l)) { --- tirei porque a função não existe
  30. return 0;
  31. }
  32. l->dados[l->quant] = al;
  33. l->quant++;
  34. return 1;
  35. }
  36.  
  37. int insere_inicio(Lista *l, Alunos al) {
  38. if (l == NULL) { // || lista_Cheia(l)) { --- tirei porque a função não existe
  39. return 0;
  40. }
  41. for (int i = l->quant - 1; i >= 0; i--) {
  42. printf("ok");
  43. l->dados[i + 1] = l->dados[i];
  44. l->dados[0] = al;
  45. l->quant++;
  46. }
  47. l->dados[0] = al;
  48. l->quant++;
  49. return 1;
  50. }
  51.  
  52. void imprime_lista(Lista* l) {
  53. if (l == NULL) return;
  54. for(int i = 0; i < l->quant; i++) {
  55. printf("Matricula: %d\n", l->dados[i].matricula);
  56. printf("Nome: %s\n", l->dados[i].nome);
  57. printf("Notas: %f %f\n", l->dados[i].nota1, l->dados[i].nota2);
  58. }
  59. }
  60.  
  61. int main() {
  62. Alunos al[2] = {{3, "João", 8.45, 9.98}, {1, "Maria", 6.75, 8.54}};
  63. Lista *l = cria_Lista();
  64. for (int i = 0; i < 2; i++) {
  65. insere_inicio(l, al[i]);
  66. imprime_lista(l);
  67. }
  68. libera_Lista(l);
  69. }
  70.  
  71. //https://pt.stackoverflow.com/q/157781/101
Success #stdin #stdout 0s 4448KB
stdin
Standard input is empty
stdout
Matricula: 3
Nome: João
Notas: 8.450000 9.980000
okMatricula: 1
Nome: Maria
Notas: 6.750000 8.540000
Matricula: 3
Nome: João
Notas: 8.450000 9.980000
Matricula: 0
Nome: 
Notas: 0.000000 0.000000