uni

University stuff
git clone git://git.christosmarg.xyz/uni-assignments.git
Log | Files | Refs | README | LICENSE

queue.c (2207B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 
      5 struct Student {
      6         char name[50];
      7         float grade;
      8         int id;
      9         struct Student *next;
     10 };
     11 
     12 struct Student *head, *tail;
     13 
     14 static void
     15 enqueue(struct Student *s)
     16 {
     17         struct Student *p;
     18 
     19         if ((p = malloc(sizeof(struct Student))) == NULL) {
     20                 fputs("malloc failed\n", stderr);
     21                 exit(EXIT_FAILURE);
     22         }
     23 
     24         *p = *s;
     25         p->next = NULL;
     26         if (tail == NULL)
     27                 head = p;
     28         else
     29                 tail->next = p;
     30         tail = p;
     31 }
     32 
     33 static void
     34 dequeue(void)
     35 {
     36         struct Student *p;
     37 
     38         if (head != NULL) {
     39                 p = head;
     40                 head = head->next;
     41                 free(p);
     42                 if (head == NULL)
     43                         tail = NULL;
     44         }
     45 }
     46 
     47 static void
     48 print(void)
     49 {
     50         struct Student *p;
     51 
     52         for (p = head; p != NULL; p = p->next)
     53                 printf("Name: %s | ID: %d | Grade: %.2f\n", p->name, p->id, p->grade);
     54 }
     55 
     56 int
     57 main(int argc, char *argv[])
     58 {
     59         struct Student stud;
     60         int ch = -1;
     61 
     62         head = NULL;
     63         tail = NULL;
     64 
     65         while (ch != 0) {
     66                 puts("1. Enqueue");
     67                 puts("2. Dequeue");
     68                 puts("3. Print");
     69                 puts("0. Exit");
     70                 printf("\nYour choice: ");
     71                 scanf("%d", &ch);
     72                 getchar();
     73 
     74                 switch (ch) {
     75                 case 1:
     76                         printf("Name: ");
     77                         fgets(stud.name, sizeof(stud.name), stdin);
     78                         stud.name[strlen(stud.name) - 1] = '\0';
     79                         printf("ID: ");
     80                         scanf("%d", &stud.id);
     81                         getchar();
     82                         printf("Grade: ");
     83                         scanf("%f", &stud.grade);
     84                         getchar();
     85                         enqueue(&stud);
     86                         break;
     87                 case 2:
     88                         dequeue();
     89                         break;
     90                 case 3:
     91                         print();
     92                         break;
     93                 }
     94         }
     95 
     96         return 0;
     97 }