-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstack_llcpp.cpp
75 lines (67 loc) · 966 Bytes
/
stack_llcpp.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
66
67
68
69
70
71
72
73
74
75
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node *next;
};
class Stack
{
private:
Node *top;
public:
Stack()
{
top = NULL;
}
void Push(int x);
int Pop();
void Display();
};
void Stack ::Push(int x)
{
Node *t = new Node;
if (t == NULL)
cout << "Stack is Full\n";
else
{
t->data = x;
t->next = top;
top = t;
}
}
int Stack ::Pop()
{
int x = -1;
if (top == NULL)
cout << "Stack is Empty\n";
else
{
x = top->data;
Node *t = top;
top = top->next;
delete t;
}
return x;
}
void Stack ::Display()
{
Node *p = top;
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
cout << endl;
}
int main()
{
Stack stk;
stk.Push(10);
stk.Push(20);
stk.Push(30);
stk.Display();
cout << "popped : " << stk.Pop();
return 0;
}