-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack_list.cpp
More file actions
111 lines (98 loc) · 1.99 KB
/
Stack_list.cpp
File metadata and controls
111 lines (98 loc) · 1.99 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include "Stack_list.h"
bool isEmpty(NPTR &stack)
{
if(stack = nullptr)
return true;
return false;
}
NPTR createNode(const int &value)
{
NPTR pStack = new NODE;
pStack->info = value;
pStack->next = nullptr;
return pStack;
}
void push(NPTR &stack, const int &value, NPTR &lastNode)
{
NPTR newNode = createNode(value);
if(isEmpty(stack))
{
stack = newNode;
lastNode = newNode;
return;
}
newNode->next = stack;
stack = newNode;
}
void pop(NPTR &stack)
{
NPTR delNode = nullptr;
if(isEmpty(stack))
{
std::cout << "Empty Stack! Nothing to delete" << std::endl;
return;
}
/*if(countNode(stack) == 1)
{
delete stack;
stack = nullptr;
return;
}*/
delNode = stack;
stack = stack->next;
delete delNode;
delNode = nullptr;
}
int peek(NPTR &stack)
{
return stack->info;
}
void display(NPTR stack)
{
if(isEmpty(stack))
{
std::cout << "Stack is empty" << std::endl;
return;
}
while(stack != nullptr)
{
std::cout << stack->info << " ";
stack = stack->next;
}
std::cout << "\n";
}
int main()
{
int value, ch, peekEl = 0;
NPTR stack = nullptr;
NPTR lastNode = nullptr;
do
{
std::cout << "~ M E N U ~" << std::endl;
std::cout << "1. Push an element" << std::endl;
std::cout << "2. Pop an element" << std::endl;
std::cout << "3. Display the peek element" << std::endl;
std::cout << "4. Display the Stack" << std::endl;
std::cout << "5. Exit" << std::endl;
std::cout << "Enter your choice" << std::endl;
std::cin >> ch;
switch(ch)
{
case 1 : std::cout << "Enter the element you want to push" << std::endl;
std::cin >> value;
push(stack, value, lastNode);
break;
case 2 : pop(stack);
break;
case 3 : peekEl = peek(stack);
std::cout << "The peek element is " << peekEl << std::endl;
break;
case 4 : display(stack);
break;
case 5 : break;
default : std::cout << "Invalid Choice" << std::endl;
}
}while(ch != 5);
std::cout << "Thank You !" << std::endl;
}