-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackDemo.java
More file actions
170 lines (153 loc) · 5.71 KB
/
StackDemo.java
File metadata and controls
170 lines (153 loc) · 5.71 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package TimeAndSpace.DSA;
import java.util.Scanner;
import java.util.InputMismatchException;
class Stack {
private int[] stack;
private int top;
private int capacity;
// Constructor to initialize the stack with an initial capacity
public Stack(int initialCapacity) {
capacity = initialCapacity;
stack = new int[capacity];
top = -1; // Stack is initially empty
}
// Method to check if the stack is empty
public boolean isEmpty() {
return top == -1;
}
// Method to check if the stack is full
public boolean isFull() {
return top == capacity - 1;
}
// Method to increase the stack size when it is full
private void resize() {
int newCapacity = capacity * 2;
int[] newStack = new int[newCapacity];
System.arraycopy(stack, 0, newStack, 0, capacity);
stack = newStack;
capacity = newCapacity;
System.out.println("Stack capacity increased to " + newCapacity);
}
// Method to push an element into the stack
public void push(int value) {
if (isFull()) {
resize(); // Increase capacity if the stack is full
}
stack[++top] = value;
System.out.println("Pushed " + value + " into the stack.");
}
// Method to pop an element from the stack
public int pop() {
if (isEmpty()) {
System.out.println("Stack is empty! Cannot pop.");
return -1; // Return an invalid value when the stack is empty
}
int poppedValue = stack[top--];
return poppedValue;
}
// Method to peek at the top element of the stack
public int peek() {
if (isEmpty()) {
System.out.println("Stack is empty! Nothing to peek.");
return -1; // Return an invalid value when the stack is empty
}
return stack[top];
}
// Method to get the size of the stack
public int size() {
return top + 1;
}
// Method to display the stack contents
public void display() {
if (isEmpty()) {
System.out.println("Stack is empty!");
} else {
System.out.print("Stack elements: ");
for (int i = 0; i <= top; i++) {
System.out.print(stack[i] + " ");
}
System.out.println();
}
}
}
public class StackDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Prompt user to enter the initial size of the stack
int size = 0;
while (size <= 0) {
try {
System.out.print("Enter a positive size for the stack: ");
size = scanner.nextInt();
if (size <= 0) {
System.out.println("Please enter a valid positive number.");
}
} catch (InputMismatchException e) {
System.out.println("Invalid input! Please enter a valid integer.");
scanner.next(); // Clear the invalid input
}
}
// Create a stack object with the specified size
Stack stack = new Stack(size);
while (true) {
// Menu for user input
System.out.println("\nChoose an operation:");
System.out.println("1. Push");
System.out.println("2. Pop");
System.out.println("3. Peek");
System.out.println("4. Size");
System.out.println("5. Display Stack");
System.out.println("6. Exit");
int choice = 0;
while (choice < 1 || choice > 6) {
try {
System.out.print("Enter your choice (1-6): ");
choice = scanner.nextInt();
if (choice < 1 || choice > 6) {
System.out.println("Invalid choice! Please select a number between 1 and 6.");
}
} catch (InputMismatchException e) {
System.out.println("Invalid input! Please enter a valid integer.");
scanner.next(); // Clear the invalid input
}
}
switch (choice) {
case 1:
// Push an element
System.out.print("Enter a value to push: ");
int value = scanner.nextInt();
stack.push(value);
break;
case 2:
// Pop an element
int poppedValue = stack.pop();
if (poppedValue != -1) {
System.out.println("Popped: " + poppedValue);
}
break;
case 3:
// Peek at the top element
int peekedValue = stack.peek();
if (peekedValue != -1) {
System.out.println("Top element: " + peekedValue);
}
break;
case 4:
// Get size of the stack
System.out.println("Stack size: " + stack.size());
break;
case 5:
// Display the stack contents
stack.display();
break;
case 6:
// Exit the program
System.out.println("Exiting...");
scanner.close();
return;
default:
System.out.println("Invalid choice! Please try again.");
}
}
}
}