Advertisement
Guest User

3-2

a guest
Apr 21st, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.56 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct node {
  5.     int value;
  6.     struct node *next;
  7.     struct node *prev;
  8. } node;
  9.  
  10. struct list {
  11.     struct node* head;
  12.     struct node* tail;
  13. } list;
  14.  
  15. void init(struct list* l, int value) {
  16.     struct node* cur;
  17.     cur = (struct node*)malloc(sizeof(struct node));
  18.     cur -> value = value;
  19.     cur -> prev = NULL;
  20.     cur -> next = NULL;
  21.     l -> head = cur;
  22.     l -> tail = cur;
  23. }
  24.  
  25. void clear(struct list* l) {
  26.     struct node* cur = l -> head;
  27.     struct node* vsp = NULL;
  28.     while (cur != NULL) {
  29.         vsp = cur -> next;
  30.         free(cur);
  31.         cur = vsp;
  32.     }
  33.     l -> head = NULL;
  34.     l -> tail = NULL;
  35. }
  36.  
  37. int isEmpty(struct list* l) {
  38.     return l -> head == NULL && l -> tail == NULL;
  39. }
  40.  
  41. struct node* find(struct list* l, int value) {
  42.     struct node *cur = l -> head;
  43.     while (cur -> value != value) {
  44.         if (cur -> next != NULL) {
  45.             cur = cur -> next;
  46.         }
  47.         else {
  48.             return 0;
  49.         }
  50.     }
  51.     return cur;
  52. }
  53.  
  54. int push_back(struct list* l, int value) {
  55.     if (isEmpty(l) == 1) {
  56.         init(l, value);
  57.     }
  58.     else {
  59.         struct node *cur = (struct node*)malloc(sizeof(struct node));
  60.         cur -> value = value;
  61.         cur -> prev = l -> tail;
  62.         cur -> next = NULL;
  63.         l -> tail = cur;
  64.         cur -> prev -> next = cur;
  65.     }
  66.     return 0;
  67. }
  68.  
  69. int push_front(struct list* l, int value) {
  70.     if (isEmpty(l) == 1) {
  71.         init(l, value);
  72.     }
  73.     else {
  74.         struct node *cur = (struct node*)malloc(sizeof(struct node));
  75.         cur -> value = value;
  76.         cur -> next = l -> head;
  77.         cur -> prev = NULL;
  78.         l -> head = cur;
  79.         cur -> next -> prev = cur;
  80.     }
  81.     return 0;
  82. }
  83.  
  84. int insertAfter(struct list* l, int pos, int value) {
  85.     struct node* cur = l -> head;
  86.     for (int i = 1; i < pos; i++) {
  87.         cur = cur -> next;
  88.         if (cur == NULL) {
  89.             return 1;
  90.         }
  91.     }
  92.     struct node *vsp = malloc(sizeof(node));
  93.     vsp -> value = value;
  94.     vsp -> next = cur -> next;
  95.     vsp -> prev = cur;
  96.     cur -> next -> prev = vsp;
  97.     cur -> next = vsp;
  98.     return 0;
  99. }
  100.  
  101. int insertBefore(struct list* l, int pos, int value) {
  102.     struct node* cur = l -> head;
  103.     for (int i = 1; i < pos; i++) {
  104.         cur = cur -> next;
  105.         if (cur == NULL) {
  106.             return 1;
  107.         }
  108.     }
  109.     struct node *vsp = malloc(sizeof(node));
  110.     vsp -> value = value;
  111.     vsp -> prev = cur -> prev;
  112.     vsp -> next = cur;
  113.     cur -> prev = vsp;
  114.     vsp -> prev -> next = vsp;
  115.     return 0;
  116. }
  117.  
  118. int removeFirst(struct list* l, int value) {
  119.     struct node* cur = find(l, value);
  120.     if (cur != NULL) {
  121.         if (cur == l -> head && cur == l -> tail) {
  122.             clear(l);
  123.             return 1;
  124.         }
  125.         if (cur == l -> head)
  126.         {
  127.             l -> head = cur -> next;
  128.             cur -> next -> prev = NULL;
  129.         }
  130.         else if(cur == l -> tail)
  131.         {
  132.             l -> tail = cur -> prev;
  133.             cur -> prev -> next = NULL;
  134.         }
  135.         else if (cur != l -> head && cur != l -> tail)
  136.         {
  137.             cur -> next -> prev = cur -> prev;
  138.             cur -> prev -> next = cur -> next;
  139.         }
  140.         free(cur);
  141.         return 0;
  142.     }
  143.     return 1;
  144. }
  145.  
  146. int removeLast(struct list* l, int value) {
  147.     struct node* cur = l -> tail;
  148.     while (cur -> value != value) {
  149.         cur = cur -> prev;
  150.         if (cur -> next == NULL) {
  151.             return 1;
  152.         }
  153.     }
  154.     if (cur != NULL) {
  155.         if (cur == l -> head && cur == l -> tail) {
  156.             clear(l);
  157.             return 1;
  158.         }
  159.         if (cur == l -> head)
  160.         {
  161.             l -> head = cur -> next;
  162.             cur -> next -> prev = NULL;
  163.         }
  164.         else if(cur == l -> tail)
  165.         {
  166.             l -> tail = cur -> prev;
  167.             cur -> prev -> next = NULL;
  168.         }
  169.         else if (cur != l -> head && cur != l -> tail)
  170.         {
  171.             cur -> next -> prev = cur -> prev;
  172.             cur -> prev -> next = cur -> next;
  173.         }
  174.         free(cur);
  175.         return 0;
  176.     }
  177. }
  178.  
  179. void print(struct list* l) {
  180.     struct node* cur = l -> head;
  181.     while (cur -> next != NULL)
  182.     {
  183.         printf("%d ", cur -> value);
  184.         cur = cur -> next;
  185.     }
  186.     printf("%d\n", cur -> value);
  187. }
  188.  
  189. void print_invers(struct list* l) {
  190.     struct node* cur = l -> tail;
  191.     while (cur -> prev != NULL)
  192.     {
  193.         printf("%d ", cur -> value);
  194.         cur = cur -> prev;
  195.     }
  196.     printf("%d\n", cur -> value);
  197. }
  198.  
  199. int main() {
  200.     int n, value;
  201.     struct list* l=(struct list*)malloc(sizeof(struct list));
  202.     scanf("%d", &n);
  203.     for (int i = 0; i < n; i++) {
  204.         scanf("%d",&value);
  205.         push_back(l, value);
  206.     }
  207.     print(l);
  208.  
  209.     int k[3];
  210.     scanf("%d%d%d", &k[0], &k[1], &k[2]);
  211.     for (int i = 0; i < 3; i++) {
  212.         if (find(l, k[i]) != NULL) {
  213.             printf("1");
  214.         }
  215.         else {
  216.             printf("0");
  217.         }
  218.     }
  219.     printf("\n");
  220.  
  221.     int m;
  222.     scanf("%d", &m);
  223.     push_back(l, m);
  224.     print_invers(l);
  225.  
  226.     int t;
  227.     scanf("%d", &t);
  228.     push_front(l, t);
  229.     print(l);
  230.  
  231.     int j, x;
  232.     scanf("%d%d", &j, &x);
  233.     insertAfter(l, j, x);
  234.     print_invers(l);
  235.  
  236.     int u, y;
  237.     scanf("%d%d", &u, &y);
  238.     insertBefore(l, u, y);
  239.     print(l);
  240.  
  241.     int z;
  242.     scanf("%d", &z);
  243.     removeFirst(l, z);
  244.     print_invers(l);
  245.  
  246.     int r;
  247.     scanf("%d", &r);
  248.     removeLast(l, r);
  249.     print(l);
  250.  
  251.     clear(l);
  252.  
  253.     return 0;
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement