Advertisement
lordasif

link list stack

Mar 19th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. using namespace std;
  6.  
  7. struct Node{
  8. int val;
  9. Node *next;
  10. };
  11.  
  12. class Stack{
  13. private:
  14. Node *head = NULL;
  15. int size = 0;
  16. public:
  17. Stack(){
  18. size = 0;
  19. }
  20. void push_back(int in){
  21. Node *t1, *t2;
  22. t1 = head;
  23. while(t1 -> next != NULL)
  24. t1 = t1 -> next;
  25. t2 = new Node;
  26. t2 -> val = in;
  27. t2->next = NULL;
  28. t1->next = t2;
  29. size++;
  30. }
  31. void push_front(int in){
  32. Node *t;
  33. t = new Node;
  34. t -> val = in;
  35. if(head == NULL){
  36. t -> next = NULL;
  37. head = t;
  38. }
  39. else{
  40. t -> next = head;
  41. head = t;
  42. }
  43. size++;
  44. }
  45. int pop_back(){
  46. int val;
  47. if(head == NULL){
  48. cout << "Stack is Empty\n";
  49. return -1;
  50. }
  51. else{
  52.  
  53. Node *t1;
  54. t1 = head;
  55. if(head -> next == NULL){
  56. val = head -> val;
  57. delete head;
  58. return val;
  59. }
  60. else{
  61. Node *chk = t1 -> next;
  62. while(chk -> next != NULL){
  63. t1 = chk;
  64. chk = chk -> next;
  65. }
  66. val = t1 -> val;
  67. delete chk;
  68. t1 -> next = NULL;
  69. }
  70. }
  71. return val;
  72. }
  73. int pop_front(){
  74. int val;
  75. if (head == NULL){
  76. cout << "Stack is Empty\n";
  77. return -1;
  78. }
  79. else{
  80. Node *t1;
  81. t1 = head;
  82. if (head->next == NULL){
  83. val = head->val;
  84. delete head;
  85. return val;
  86. }
  87. else{
  88. head = head -> next;
  89. val = head -> val;
  90. delete t1;
  91. }
  92. }
  93. return val;
  94. }
  95. int top(){
  96. if(head == NULL) return -1;
  97. return head -> val;
  98. }
  99. void show(){
  100. Node *t;
  101. t = head;
  102. while(t != NULL){
  103. cout << t -> val << ' ';
  104. t = t -> next;
  105. }
  106. cout << endl;
  107. }
  108. int find(int in){
  109. Node *t;
  110. t = head;
  111. int index = 0;
  112. if(head == NULL) return -1;
  113. else{
  114. while(t != NULL){
  115. index++;
  116. if(t -> val == in) return index;
  117. t = t -> next;
  118. }
  119. }
  120. return -1;
  121. }
  122. void del(){
  123. Node *t;
  124. while(head -> next != NULL)
  125. {
  126. t = head;
  127. head = head -> next;
  128. delete t;
  129. }
  130. }
  131. int length(){
  132. return size;
  133. }
  134. };
  135.  
  136.  
  137. int main(){
  138. Stack s;
  139. cout << "1. Push Front.\n";
  140. cout << "2. Push Back.\n";
  141. cout << "3. Pop Front.\n";
  142. cout << "4. Pop Back.\n";
  143. cout << "5. Print.\n";
  144. cout << "6. Top.\n";
  145. cout << "7. Search.\n";
  146. cout << "8. Exit.\n";
  147. while(true){
  148. int input, iin, delv;
  149. cin >> input;
  150. if(input == 1){
  151. cin >> iin;
  152. s.push_front(iin);
  153. }
  154. else if (input == 2)
  155. {
  156. cin >> iin;
  157. s.push_back(iin);
  158. }
  159. else if (input == 3)
  160. {
  161. delv = s.pop_front();
  162. }
  163. else if (input == 4)
  164. {
  165. delv = s.pop_back();
  166. }
  167. else if (input == 5)
  168. {
  169. s.show();
  170. }
  171. else if (input == 6)
  172. {
  173. cout << s.top() << endl;
  174. }
  175. else if (input == 7)
  176. {
  177. cin >> iin;
  178. if(s.find(iin) != -1) cout << "Found at " << s.find(iin) << " position\n";
  179. else cout << "Not Found\n";
  180. }
  181. else if (input == 8)
  182. {
  183. break;
  184. }
  185. }
  186. return 0;
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement