lordasif

doubly link list

Apr 1st, 2019
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. struct Node
  4. {
  5. int val;
  6. Node * prev;
  7. Node * next;
  8.  
  9. Node(int v)
  10. {
  11. val = v;
  12. prev = NULL;
  13. next = NULL;
  14. }
  15. };
  16.  
  17. class DoubleLinkedList
  18. {
  19. Node * head;
  20. Node * tail;
  21. public:
  22. DoubleLinkedList()
  23. {
  24. head = NULL;
  25. tail = NULL;
  26. }
  27. void push_front(int val_)
  28. {
  29. Node * temp = new Node(val_);
  30.  
  31. temp->val = val_;
  32. temp->prev = NULL;
  33. temp->next = NULL;
  34.  
  35. if (head == NULL)
  36. {
  37. head = temp;
  38. tail = temp;
  39. }
  40. else
  41. {
  42. temp->prev = NULL;
  43. temp->next = head;
  44. head->prev = temp;
  45. head = head->prev;
  46. }
  47. }
  48.  
  49. void push_back(int v)
  50. {
  51. if (head == NULL)
  52. push_front(v);
  53. else
  54. {
  55. Node * temp = new Node(v);
  56. temp->prev = tail;
  57. tail->next = temp;
  58. tail = temp;
  59. }
  60. }
  61.  
  62. void display()
  63. {
  64. if (head == NULL)
  65. printf("No element!\n");
  66. else
  67. {
  68. printf("LinkedList Elements: ");
  69. Node * temp = head;
  70. while (temp != NULL)
  71. {
  72. printf("%d ", temp->val);
  73. temp = temp->next;
  74. }
  75. printf("\n");
  76. }
  77. }
  78.  
  79. void revDisplay()
  80. {
  81. if (tail == NULL)
  82. printf("No element!\n");
  83. else
  84. {
  85. printf("LinkedList Reversed Elements: ");
  86. Node * temp = tail;
  87. while (temp != NULL)
  88. {
  89. printf("%d ", temp->val);
  90. temp = temp->prev;
  91. }
  92. printf("\n");
  93. }
  94. }
  95. int pop_front()
  96. {
  97. if (head == NULL)
  98. {
  99. printf("List is empty\n");
  100. return -1;
  101. }
  102. else if (head == tail)
  103. {
  104. Node * temp;
  105. temp = head;
  106. head = NULL;
  107. tail = NULL;
  108. int x = temp->val;
  109. delete temp;
  110. return x;
  111. }
  112. else
  113. {
  114. Node * temp;
  115. temp = head;
  116. head = head->next;
  117. ///head = NULL;
  118. ///tail = NULL;
  119. int x = temp->val;
  120. delete temp;
  121. head->prev = NULL;
  122. return x;
  123. }
  124. }
  125.  
  126. int pop_back()
  127. {
  128. if ((head == NULL)|| (head == tail))
  129. return pop_front();
  130. else
  131. {
  132. Node *temp;
  133. temp = tail;
  134. tail = tail->prev;
  135. int x = temp->val;
  136. delete temp;
  137. tail->next = NULL;
  138. return x;
  139. }
  140. }
  141.  
  142. void dispose()
  143. {
  144. tail = NULL;
  145. while (head != NULL)
  146. {
  147. Node * temp = head;
  148. head = head->next;
  149. delete temp;
  150. }
  151. printf("All elements are disposed!\n");
  152. }
  153.  
  154. void delete_node(Node * node)
  155. {
  156. //Write your code here
  157. }
  158.  
  159. void delete_odd_elements()
  160. {
  161. //Write your code here
  162. }
  163. };
  164.  
  165.  
  166.  
  167. int main()
  168. {
  169. DoubleLinkedList l1;
  170.  
  171. l1.push_back(100);
  172. l1.push_back(105);
  173. l1.push_back(110);
  174. l1.push_back(115);
  175.  
  176. l1.display();
  177. l1.revDisplay();
  178.  
  179. l1.delete_odd_elements();
  180.  
  181. l1.display();
  182. l1.revDisplay();
  183.  
  184. l1.dispose();
  185.  
  186. }
Add Comment
Please, Sign In to add comment