-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackdynamicarray.cpp
More file actions
148 lines (131 loc) · 3.64 KB
/
stackdynamicarray.cpp
File metadata and controls
148 lines (131 loc) · 3.64 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <iostream>
#include <stdexcept>
class Stack {
private:
int size;
int capacity;
int* arr;
int top;
public:
Stack() {
size = 0;
capacity = 2;
arr = new int[capacity];
top = -1;
}
~Stack() {
delete[] arr;
}
void push(int value) {
if (size == capacity) {
capacity *= 2;
int* old_arr = arr;
arr = new int[capacity];
for (int i = 0; i < size; i++) {
arr[i] = old_arr[i];
}
delete[] old_arr;
}
top++;
size++;
arr[top] = value;
}
int pop() {
if (top == -1) {
throw std::runtime_error("Cannot pop from an empty stack.");
}
int top_element = arr[top];
top--;
size--;
return top_element;
}
int peek() {
if (top == -1) {
throw std::runtime_error("Cannot peek from an empty stack.");
}
return arr[top];
}
int get_index(int value) {
for (int i = 0; i <= top; i++) {
if (arr[i] == value) {
return i;
}
}
return -1;
}
void print() {
std::cout << "[";
if (top != -1) {
std::cout << arr[0];
}
for (int i = 1; i <= top; i++) {
std::cout << " " << arr[i];
}
std::cout << "]" << std::endl;
}
int getLargest() {
if (top == -1) {
throw std::runtime_error("Stack is empty.");
}
int largest = arr[0];
for (int i = 1; i <= top; i++) {
if (arr[i] > largest) {
largest = arr[i];
}
}
return largest;
}
};
int main() {
Stack s;
int choice;
int value;
do {
std::cout << "Stack Menu:\n";
std::cout << "1. Push\n";
std::cout << "2. Pop\n";
std::cout << "3. Display\n";
std::cout << "4. Find Largest\n";
std::cout << "5. Exit\n";
std::cout << "Enter your choice: ";
std::cin >> choice;
switch (choice) {
case 1:
std::cout << "Enter the value to push: ";
std::cin >> value;
s.push(value);
std::cout << "Current contents of the stack: ";
s.print();
break;
case 2:
try {
std::cout << "Popped: " << s.pop() << std::endl;
std::cout << "Current contents of the stack: ";
s.print();
} catch (const std::runtime_error& e) {
std::cout << "Error: " << e.what() << std::endl;
}
break;
case 3:
std::cout << "Current contents of the stack: ";
s.print();
break;
case 4:
try {
int largest = s.getLargest();
std::cout << "Largest integer in the stack: " << largest << std::endl;
} catch (const std::runtime_error& e) {
std::cout << "Error: " << e.what() << std::endl;
}
break;
case 5:
std::cout << "Exiting..." << std::endl;
break;
default:
std::cout << "Invalid choice. Please try again.\n";
break;
}
std::cout << std::endl;
} while (choice != 5);
return 0;
}