-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
70 lines (59 loc) · 1.64 KB
/
Stack.java
File metadata and controls
70 lines (59 loc) · 1.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
/*
* Program to implement a stack using arrays:
* Write a Java program to implement a stack data structure using arrays,
* along with basic operations like push, pop, and peek.
*
*/
public class Stack
{
private int[] stack;
private int top;
private int capacity;
public Stack(int capacity) {
this.capacity = capacity;
this.stack = new int[capacity];
this.top = -1;
}
public void push(int item) {
if (top == capacity - 1) {
System.out.println("Stack Overflow");
return;
}
stack[++top] = item;
System.out.println(item + " pushed into the stack");
}
public int pop() {
if (top == -1) {
System.out.println("Stack Underflow");
return -1;
}
int poppedItem = stack[top--];
System.out.println(poppedItem + " popped from the stack");
return poppedItem;
}
public int peek() {
if (top == -1) {
System.out.println("Stack is empty");
return -1;
}
return stack[top];
}
public boolean isEmpty() {
return top == -1;
}
public static void main(String[] args) {
Stack stack = new Stack(5);
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
System.out.println("Top element of stack: " + stack.peek());
stack.pop();
stack.pop();
stack.pop();
System.out.println("Is stack empty? " + stack.isEmpty());
stack.pop();
stack.pop(); // Pop additional item to demonstrate underflow condition
}
}