-
Notifications
You must be signed in to change notification settings - Fork 111
/
Stack Using Linked List.txt
82 lines (81 loc) · 1.35 KB
/
Stack Using Linked List.txt
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
76
77
78
79
80
81
82
#include<iostream>
using namespace std;
template<class t>
class Stack {
private:
struct StackNode
{
t item;
StackNode*next;
};
StackNode *topPtr, *curPtr;
public:
Stack() {
topPtr = NULL;
}
bool isEmpty()
{
return topPtr == NULL;
}
void push(t newItem)
{
StackNode *newPtr = new StackNode;
if (newPtr == NULL)
cout << "Stack push cannot allocate memory";
else
{
newPtr->item = newItem;
newPtr->next = topPtr;
topPtr = newPtr;
}
}
void pop() {
if (isEmpty())
cout << "Stack empty on pop";
else {
StackNode *temp = topPtr;
topPtr = topPtr->next;
temp->next = NULL;
delete temp;
}
}
void pop(t stackTop)
{
if (isEmpty())
cout << "Stack empty on pop";
else {
stackTop = topPtr->item;
StackNode *temp = topPtr;
topPtr = topPtr->next;
temp->next = NULL;
delete temp;
}
}
void getTop(t stackTop)
{
if (isEmpty())
cout << "stack empty on getTop";
else
stackTop = topPtr->item;
cout << "\nTop Element of the stack is " << stackTop;
cout << endl;
}
void display()
{
curPtr = topPtr;
cout << "\nItems in the stack : ";
cout << "[ ";
while (curPtr != NULL)
{
cout <<curPtr->item;
curPtr = curPtr->next;
}
cout << " ]\n";
}
};
int main()
{
Stack<int>s;
s.push(10);
s.display();
}