Guest User

Untitled

a guest
Feb 15th, 2020
71
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ConsoleApplication1.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. struct node
  9. {
  10. int data;
  11. node *next;
  12. node *prev;
  13. };
  14.  
  15. class linked_list
  16. {
  17.  
  18. public:
  19.  
  20. node *head, *tail;
  21.  
  22. linked_list()
  23. {
  24. head = NULL;
  25. tail = NULL;
  26. }
  27.  
  28. void push(int n)
  29. {
  30. node *tmp = new node;
  31. (*tmp).data = n;
  32. tmp->next = NULL;
  33. tmp->prev = tail;
  34.  
  35. if (head == NULL)
  36. {
  37. head = tmp;
  38. tail = tmp;
  39. }
  40. else
  41. {
  42. tail->next = tmp;
  43. tail = tmp;
  44. }
  45. }
  46.  
  47. void enqueue(int n)
  48. {
  49. node *tmp = new node;
  50. (*tmp).data = n;
  51. tmp->next = head;
  52. tmp->prev = NULL;
  53.  
  54. if (head == NULL)
  55. {
  56. head = tmp;
  57. tail = tmp;
  58. }
  59. else
  60. {
  61. head->prev = tmp;
  62. head = tmp;
  63. }
  64. }
  65.  
  66. int pop()
  67. {
  68. if (tail == NULL)
  69. {
  70. return -1;
  71. }
  72. node *ptr = tail;
  73. int val = tail->data;
  74. tail = tail->prev;
  75.  
  76. delete ptr;
  77. return val;
  78. }
  79.  
  80. int dequeue()
  81. {
  82. if (head == NULL)
  83. {
  84. return -1;
  85. }
  86. node *ptr = head;
  87.  
  88. int val = head->data;
  89.  
  90. head = head->next;
  91. delete ptr;
  92. return val;
  93. }
  94. };
  95.  
  96. int main()
  97. {
  98. int b = 0;
  99. linked_list a;
  100. a.push(1);
  101. a.push(2);
  102. a.push(3);
  103.  
  104. cout << a.pop();
  105. cout << a.dequeue();
  106. a.push(1);
  107. a.push(2);
  108. a.enqueue(3);
  109. cout << a.dequeue();
  110. cin >> b;
  111. return 0;
  112. }
RAW Paste Data