-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp11StackOperation.cpp
More file actions
110 lines (100 loc) · 2.13 KB
/
p11StackOperation.cpp
File metadata and controls
110 lines (100 loc) · 2.13 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
/* 11) Design, Develop and Implement a menu driven Program for the following operations on STACK of Integers (Linked Implementation)
a. Push an Element on to Stack
b. Pop an Element from Stack
c. To check Overflow and Underflow situations on Stack
d. Display the elements of Stack
e. Exit*/
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node *next;
};
class Stack
{
private:
Node *top;
public:
Stack()
{
top = nullptr;
}
void push(int value)
{
Node *newNode = new Node();
if (newNode == nullptr)
{
cout << "Overflow! Cannot push.\n";
return;
}
newNode->data = value;
newNode->next = top;
top = newNode;
cout << value << " pushed.\n";
}
void pop()
{
if (top == nullptr)
{
cout << "Underflow! Stack is empty.\n";
return;
}
Node *temp = top;
cout << temp->data << " popped.\n";
top = top->next;
delete temp;
}
void display()
{
if (top == nullptr)
{
cout << "Stack is empty.\n";
return;
}
Node *temp = top;
cout << "Stack: ";
while (temp != nullptr)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
};
int main()
{
Stack s;
int choice, value;
do
{
cout << "ABHISHEK SINGH 2315272/2435222" << endl;
cout << "\n1. Push" << endl;
cout << "2. Pop" << endl;
cout << "3. Display" << endl;
cout << "4. Exit" << endl;
cout << "Enter choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter value: ";
cin >> value;
s.push(value);
break;
case 2:
s.pop();
break;
case 3:
s.display();
break;
case 4:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice.\n";
}
} while (choice != 4);
return 0;
}