loulett

HW2_listDanila

Dec 8th, 2014
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #define _CRT_SECURE_NO_WARNINGS
  4.  
  5. struct Node
  6. {
  7.     Node * next;
  8.     Node * prev;
  9.     int key;
  10. };
  11.  
  12. struct List
  13. {
  14.     Node * first;
  15.     Node * last;
  16. };
  17.  
  18. void swap(Node * a)
  19. {
  20.     Node * b = a->next;
  21.     a->next = a->prev;
  22.     a->prev = b;
  23. }
  24.  
  25. int push(List * l, int a)
  26. {
  27.     Node * q = (Node *)malloc(sizeof(Node));
  28.     if (!q)
  29.         return 0;
  30.     q->key = a;
  31.     l->last->prev->next = q;
  32.     q->prev = l->last->prev;
  33.     q->next = l->last;
  34.     l->last->prev = q;
  35.     return 1;
  36. }
  37.  
  38. int unshift(List* l, int a)
  39. {
  40.     Node * q = (Node *)malloc(sizeof(Node));
  41.     if (!q)
  42.         return 0;
  43.     q->key = a;
  44.     l->first->next->prev = q;
  45.     q->next = l->first->next;
  46.     q->prev = l->first;
  47.     l->first->next = q;
  48.     return 1;
  49. }
  50.  
  51. int shift(List* l, int* q)
  52. {
  53.     Node* a = l->first->next;
  54.     *q = l->first->next->key;
  55.     l->first->next->next->prev = l->first;
  56.     l->first->next = l->first->next->next;
  57.     free(a);
  58.     return 1;
  59. }
  60.  
  61. int pop(List* l, int* q)
  62. {
  63.     Node* a = l->last->prev;
  64.     *q = l->last->prev->key;
  65.     l->last->prev->prev->next = l->last;
  66.     l->last->prev = l->last->prev->prev;
  67.     free(a);
  68.     return 1;
  69. }
  70.  
  71. int reverse(List* l)
  72. {
  73.     Node* a = l->first;
  74.     Node* b;
  75.     while (a != NULL)
  76.     {
  77.         swap(a);
  78.         a = a->prev;
  79.     }
  80.     b = l->first;
  81.     l->first = l->last;
  82.     l->last = b;
  83.     return 1;
  84. }
  85.  
  86. List* list_new()
  87. {
  88.     List* a = (List*)malloc(sizeof(List));
  89.     Node* q = (Node*)malloc(sizeof(Node));
  90.     Node* w = (Node*)malloc(sizeof(Node));
  91.     q->key = 0;
  92.     w->key = 0;
  93.     q->prev = NULL;
  94.     w->next = NULL;
  95.     q->next = w;
  96.     w->prev = q;
  97.     a->first = q;
  98.     a->last = w;
  99.     return a;
  100. }
  101.  
  102. void list_delete(List* l)
  103. {
  104.     Node* a = l->first;
  105.     Node* b;
  106.     while (a != NULL)
  107.     {
  108.         b = a->next;
  109.         free(a);
  110.         a = b;
  111.     }
  112.     free(l);
  113. }
  114.  
  115. int main()
  116. {
  117.     List* l = list_new();
  118.     int q = 3, w = 5;
  119.     push(l, q);
  120.     for (Node* i = l->first; i; i = i->next)
  121.         printf("%i ", i->key);
  122.     printf("\n");
  123.     unshift(l, w);
  124.     for (Node* i = l->first; i; i = i->next)
  125.         printf("%i ", i->key);
  126.     printf("\n");
  127.     reverse(l);
  128.     for (Node* i = l->first; i; i = i->next)
  129.         printf("%i ", i->key);
  130.     printf("\n");
  131.     pop(l, &q);
  132.     for (Node* i = l->first; i; i = i->next)
  133.         printf("%i ", i->key);
  134.     printf("\n");
  135.     shift(l, &w);
  136.     for (Node* i = l->first; i; i = i->next)
  137.         printf("%i ", i->key);
  138.     printf("\n");
  139.     list_delete(l);
  140.     return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment