-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathStack.java
More file actions
106 lines (87 loc) · 2.84 KB
/
Stack.java
File metadata and controls
106 lines (87 loc) · 2.84 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
// Stack implementation in Java using arrays
// A Stack follows LIFO (Last In First Out) principle.
// This means the last element inserted is the first one to be removed.
public class Stack {
// Maximum size of the stack
private static final int MAX = 5;
// Array to store stack elements
private int[] stack = new int[MAX];
// 'top' keeps track of the index of the top element in the stack
private int top;
// Constructor: initialize top to -1 (indicating empty stack)
public Stack() {
top = -1;
}
// Method to check if stack is empty
public boolean isEmpty() {
return (top == -1);
}
// Method to check if stack is full
public boolean isFull() {
return (top == MAX - 1);
}
// Method to push an element into the stack
public void push(int value) {
if (isFull()) {
System.out.println("Stack Overflow! Cannot push " + value);
} else {
stack[++top] = value; // increment top and add value
System.out.println(value + " pushed into stack.");
}
}
// Method to pop (remove) the top element from stack
public int pop() {
if (isEmpty()) {
System.out.println("Stack Underflow! Cannot pop element.");
return -1; // return an invalid value
} else {
int poppedValue = stack[top--]; // return top element then decrement top
System.out.println(poppedValue + " popped from stack.");
return poppedValue;
}
}
// Method to view the top element without removing it
public int peek() {
if (isEmpty()) {
System.out.println("Stack is empty!");
return -1;
} else {
return stack[top];
}
}
// Method to display all elements of stack
public void display() {
if (isEmpty()) {
System.out.println("Stack is empty!");
} else {
System.out.print("Stack elements (top to bottom): ");
for (int i = top; i >= 0; i--) {
System.out.print(stack[i] + " ");
}
System.out.println();
}
}
// Main method to test the stack implementation
public static void main(String[] args) {
Stack s = new Stack();
// Push elements into the stack
s.push(10);
s.push(20);
s.push(30);
s.push(40);
s.push(50);
// Attempt to push one more (will cause overflow)
s.push(60);
// Display current stack
s.display();
// View top element
System.out.println("Top element is: " + s.peek());
// Pop elements from stack
s.pop();
s.pop();
// Display stack after popping
s.display();
// Peek again
System.out.println("New top element is: " + s.peek());
}
}