TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
stack_using_linked_list.cpp
1#include <iostream>
2
3struct node {
4 int val;
5 node *next;
6};
7
8node *top_var;
9
10void push(int x) {
11 node *n = new node;
12 n->val = x;
13 n->next = top_var;
14 top_var = n;
15}
16
17void pop() {
18 if (top_var == nullptr) {
19 std::cout << "\nUnderflow";
20 } else {
21 node *t = top_var;
22 std::cout << "\n" << t->val << " deleted";
23 top_var = top_var->next;
24 delete t;
25 }
26}
27
28void show() {
29 node *t = top_var;
30 while (t != nullptr) {
31 std::cout << t->val << "\n";
32 t = t->next;
33 }
34}
35
36int main() {
37 int ch = 0, x = 0;
38 do {
39 std::cout << "\n0. Exit or Ctrl+C";
40 std::cout << "\n1. Push";
41 std::cout << "\n2. Pop";
42 std::cout << "\n3. Print";
43 std::cout << "\nEnter Your Choice: ";
44 std::cin >> ch;
45 switch (ch) {
46 case 0:
47 break;
48 case 1:
49 std::cout << "\nInsert : ";
50 std::cin >> x;
51 push(x);
52 break;
53 case 2:
54 pop();
55 break;
56 case 3:
57 show();
58 break;
59 default:
60 std::cout << "Invalid option!\n";
61 break;
62 }
63 } while (ch != 0);
64
65 return 0;
66}
struct node { int data; int height; struct node *left; struct node *right;} node
for std::queue
Definition avltree.cpp:13
int main()
Main function.