Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. struct Nodes
  6. {
  7.     int value;
  8.     Nodes *prev, *next;
  9. }*head, *tail, *curr;
  10.  
  11. void pushHead()
  12. {
  13.     curr->next = head;
  14.     head->prev = curr;
  15.     head = curr;
  16. }
  17.  
  18. void pushTail()
  19. {
  20.     curr->prev = tail;
  21.     tail->next = curr;
  22.     tail = curr;
  23. }
  24.  
  25.  
  26. void push(int value)
  27. {
  28.     curr = (Nodes*)malloc(sizeof(Nodes));
  29.     curr->value = value;
  30.     curr->prev = curr->next = NULL;
  31.     if(head == NULL)
  32.     {
  33.         head = tail = curr;
  34.     }
  35.     else if(curr->value<head->value)
  36.     {
  37.         pushHead();
  38.     }
  39.     else if(curr->value>tail->value)
  40.     {
  41.         pushTail();
  42.     }
  43.     else
  44.     {
  45.         Nodes *temp = head;
  46.         while(temp->next->value<curr->value)
  47.         {
  48.             temp = temp->next;
  49.         }
  50.         curr->prev = temp;
  51.         curr->next = temp->next;
  52.         temp->next = curr;
  53.         curr->prev->next = curr;
  54.     }
  55. }
  56.  
  57. void popHead()
  58. {
  59.     if(head == NULL)
  60.     {
  61.         printf("No data");
  62.         return;
  63.     }
  64.     else if(head == tail)
  65.     {
  66.         curr = head;
  67.         head = tail = NULL;
  68.         free(curr);
  69.     }
  70.     else
  71.     {
  72.         curr = head;
  73.         head = head->next;
  74.         head->prev = NULL;
  75.         free(curr);
  76.     }
  77. }
  78.  
  79. void popTail()
  80. {
  81.     if(head == NULL)
  82.     {
  83.         printf("No data");
  84.         return;
  85.     }
  86.     else if(head == tail)
  87.     {
  88.         curr = head;
  89.         head = tail = NULL;
  90.         free(curr);
  91.     }
  92.     else
  93.     {
  94.         curr = tail;
  95.         tail = tail->prev;
  96.         tail->next = NULL;
  97.         free(curr);
  98.     }
  99. }
  100.  
  101. void popMid()
  102. {
  103. }
  104.  
  105. void print()
  106. {
  107.     curr = head;
  108.     while(curr)
  109.     {
  110.         printf("%d ",curr->value);
  111.         curr = curr->next;
  112.     }
  113.     printf("End Of Data\n");
  114. }
  115.  
  116. int main()
  117. {
  118.     /*
  119.     printf("Integer : %d\n",sizeof(int));
  120.     printf("Node Pointer : %d\n",sizeof(Nodes*));
  121.     printf("Nodes : %d\n",sizeof(Nodes));
  122.     */
  123.     push(3);
  124.     push(5);
  125.     push(9);
  126.     push(7);
  127.     push(2);
  128.     push(1);
  129.     push(6);
  130.     printf("InitialData : ");
  131.     print();
  132.     popHead();
  133.     printf("\n After popHead : ");
  134.     print();
  135.     printf("\n After popTail : ");
  136.     popTail();
  137.     print();
  138.     getchar();
  139.     return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement