-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion_02.cpp
More file actions
73 lines (70 loc) · 1.82 KB
/
Copy pathQuestion_02.cpp
File metadata and controls
73 lines (70 loc) · 1.82 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
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
};
class Stack {
private:
Node* top;
public:
Stack() {
top = nullptr;
}
bool isEmpty() {
return (top == nullptr);
}
void push(int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->next = top;
top = newNode;
}
int pop() {
if (isEmpty()) {
cout << "Stack Underflow" << endl;
return -1;
} else {
int value = top->data;
Node* temp = top;
top = top->next;
delete temp;
return value;
}
}
int stackTop() {
if (isEmpty()) {
cout << "Stack is empty" << endl;
return -1;
} else {
return top->data;
}
}
void display() {
if (isEmpty()) {
cout << "Stack is empty" << endl;
} else {
cout << "Stack elements are: ";
Node* temp = top;
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
}
};
int main() {
Stack stac;
stac.push(10);
stac.push(20);
stac.push(30);
stac.display();
cout << "Stack top is: " << stac.stackTop() << endl;
stac.pop();
cout << "Stack top after pop is: " << stac.stackTop() << endl;
stac.display();
cout << "Is the stack empty? " << (stac.isEmpty() ? "Yes" : "No") << endl;
return 0;
}