-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
76 lines (62 loc) · 2.02 KB
/
Stack.java
File metadata and controls
76 lines (62 loc) · 2.02 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
// Program: Stack Implementation Using Arrays
// Topic: Stack Data Structure
// Description: Implements a stack using an integer array with basic operations — `push()`, `pop()`, and `peek()`.
// Includes utility methods `isEmpty()` and `isFull()` to check stack conditions and prevent underflow or overflow.
// Demonstrates stack behavior (LIFO – Last In, First Out) and performs sample push operations in the `main()` method.
package stack;
/**
*
* @author Samim
*/
public class Stack {
private int maxSize;
private int[] stackArray;
private int top;
public Stack(int size) {
this.maxSize = size;
this.stackArray = new int[maxSize];
this.top = -1;
}
public void push(int value) {
if (isFull()) {
System.out.println("Stack is full. Cannot push " + value);
return;
}
top++;
stackArray[top] = value;
}
public int pop() {
if (isEmpty()) {
System.out.println("Stack is empty. Cannot pop.");
return -1;
}
int value = stackArray[top];
top--;
return value;
}
public int peek() {
if (isEmpty()) {
System.out.println("Stack is empty. Cannot peek.");
return -1;
}
return stackArray[top];
}
public boolean isEmpty() {
return (top == -1);
}
public boolean isFull() {
return (top == maxSize - 1);
}
public static void main(String[] args){
//The no of input defines which method will be invoked
Stack stack =new Stack(10);
stack.push(50);
stack.push(70);
// System.out.println("Peek: " + stack.peek());
// System.out.println("Pop: " + stack.pop());
// System.out.println("Peek: " + stack.peek());
stack.push(40);
stack.push(50);
stack.push(60); // This will cause overflow
}
}