-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack As Linked List.cpp
65 lines (60 loc) · 1.04 KB
/
Stack As Linked List.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
using namespace std;
class Stack
{
private:
struct node
{
int data;
node *next = NULL;
};
node *top;
public:
Stack() : top(NULL) {}
void push(int val)
{
node *n = new node{val};
n->next = top;
top = n;
cout << val << " pushed to Stack!\n";
}
void pop()
{
if (top)
{
cout << top->data << " popped from Stack!\n";
top = top->next;
}
else
{
cout << "Stack is Empty!\n";
}
}
void display()
{
node *temp = top;
while (temp != NULL)
{
cout << temp->data << '\t';
temp = temp->next;
}
cout << endl;
}
};
int main()
{
system("cls");
Stack s;
s.push(10);
s.push(20);
s.push(30);
s.push(12);
s.push(43);
s.push(32);
s.display();
s.pop();
s.pop();
s.pop();
s.display();
return 0;
}