loulett

HW2_stud

Dec 24th, 2014
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.76 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cmath>
  4.  
  5. struct stud
  6. {
  7.     int id;
  8.     char name[33];
  9.     int mark;
  10. };
  11.  
  12. struct Node
  13. {
  14.     Node * next;
  15.     Node * prev;
  16.     stud * key;
  17. };
  18.  
  19. struct List
  20. {
  21.     Node * first;
  22.     Node * last;
  23. };
  24.  
  25. void swap(Node * a)
  26. {
  27.     Node * b = a->next;
  28.     a->next = a->prev;
  29.     a->prev = b;
  30. }
  31.  
  32. int push(List* l, stud* a)
  33. {
  34.     Node * q = (Node *)malloc(sizeof(Node));
  35.     if (!q)
  36.         return 0;
  37.     q->key = a;
  38.     l->last->prev->next = q;
  39.     q->prev = l->last->prev;
  40.     q->next = l->last;
  41.     l->last->prev = q;
  42.     return 1;
  43. }
  44.  
  45.  
  46. List* list_new()
  47. {
  48.     List* a = (List*)malloc(sizeof(List));
  49.     Node* q = (Node*)malloc(sizeof(Node));
  50.     Node* w = (Node*)malloc(sizeof(Node));
  51.     q->prev = NULL;
  52.     w->next = NULL;
  53.     q->next = w;
  54.     w->prev = q;
  55.     a->first = q;
  56.     a->last = w;
  57.     return a;
  58. }
  59.  
  60. void list_delete(List* l)
  61. {
  62.     Node* a = l->first;
  63.     Node* b;
  64.     while (a != NULL)
  65.     {
  66.         b = a->next;
  67.         free(a);
  68.         a = b;
  69.     }
  70.     free(l);
  71. }
  72.  
  73.  
  74. int main()
  75. {
  76.     int n = 0, k = 0;
  77.     scanf("%i", &n);
  78.     stud* arr = (stud*)malloc(n*sizeof(stud));
  79.     List** mas = (List**)malloc(4 * sizeof(List*));
  80.     for (int i = 0; i < 4; i++)
  81.         mas[i] = list_new();
  82.     int from = 0, to = 0;
  83.     for (int i = 0; i < n; ++i)
  84.     {
  85.         scanf("%i%n%s%n", &arr[i].id, &from, arr[i].name, &to);
  86.         scanf("%s%i", arr[i].name + to - from, &arr[i].mark);
  87.         arr[i].name[to - from - 1] = ' ';
  88.         push(mas[arr[i].mark - 2], &arr[i]);
  89.     }
  90.     for (int j = 0; j < 4; j++)
  91.     {
  92.         for (Node* i = mas[j]->first->next; i->next; i = i->next)
  93.             printf("%i %s %i\n", i->key->id, i->key->name, i->key->mark);
  94.     }
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment