Advertisement
KirilGeorgiev

doubly LinkedList

Oct 28th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.86 KB | None | 0 0
  1. // ConsoleApplication3.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <ctype.h>
  8. #include <conio.h>
  9. typedef struct student {
  10.     struct student* next;
  11.     struct student* prev;
  12.     int fnum;
  13.     char firstname[20];
  14.     char lastname[20];
  15.     double grade;
  16. }student;
  17. student input(void);
  18. struct student* Add(student *root);
  19. void output(student *root);
  20.  
  21. int main()
  22. {
  23.     student* head=NULL;
  24.     int n;
  25.     printf("How many elements do you want to create?");
  26.     scanf_s("%d", &n);
  27.     for (int i = 0; i < n; i++) {
  28.         head=Add(head);
  29.     }
  30.     output(head);
  31.     return 0;
  32. }
  33.  
  34. student input(void) {
  35.     student a = { 0 };
  36.     printf("Firstname:");
  37.     fgets(a.firstname, sizeof a.firstname, stdin);
  38.     printf("Lastname:");
  39.     fgets(a.lastname, sizeof a.lastname, stdin);
  40.     printf("Firstname:");
  41.     fgets(a.firstname, sizeof a.firstname, stdin);
  42.     printf("fnum");
  43.     scanf_s("%d", &(a.fnum));
  44.     printf("grade");
  45.     scanf_s("%lf", &(a.grade));
  46.     a.next = NULL;
  47.     a.prev = NULL;
  48.     return a;
  49. }
  50.  
  51. student* Add(student* root) {
  52.     student* p = malloc(sizeof(student));
  53.     student b = input();
  54.     p = &b;
  55.     student* l;
  56.     if (root == NULL) {
  57.         root = p;
  58.         return root;
  59.     }
  60.     for (;;) {
  61.         if (root->next == NULL) {
  62.             if (root->fnum < p->fnum) {
  63.                 root->next = p;
  64.                 p->prev = root;
  65.                 return root;
  66.             }
  67.             else {
  68.                 l = root;
  69.                 root = p;
  70.                 root->next = l;
  71.                 l->prev = p;
  72.                 return root;
  73.             }
  74.         }
  75.         if (root->fnum < p->fnum) {
  76.             root = root->next;
  77.             continue;
  78.         }
  79.         else {
  80.             l = root;
  81.             root = p;
  82.             root->next = l;
  83.             l->prev = p;
  84.             return root;
  85.         }
  86.     }
  87.     return root;
  88. }
  89.  
  90. void output(student* root) {
  91.     if (root != NULL) {
  92.         printf("fname %s", root->firstname);
  93.         printf("lname %s",root->lastname);
  94.         printf("fnum %d", root->fnum);
  95.         printf("grade %13.6lf", root->grade);
  96.         output(root->next);
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement