Advertisement
Guest User

Untitled

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