-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path9StackOperations.java
More file actions
126 lines (111 loc) · 3.57 KB
/
Copy path9StackOperations.java
File metadata and controls
126 lines (111 loc) · 3.57 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
// i. Interface for Stack Operations
interface Stack {
void push(int element);
int pop();
boolean isEmpty();
int size();
}
// ii. Fixed-Length Stack Implementation
class FixedLengthStack implements Stack {
private int[] stackArray;
private int top;
private int capacity;
public FixedLengthStack(int capacity) {
this.capacity = capacity;
this.stackArray = new int[capacity];
this.top = -1;
}
@Override
public void push(int element) {
if (top == capacity - 1) {
System.out.println("Stack Overflow: Cannot push element, stack is full.");
return;
}
stackArray[++top] = element;
}
@Override
public int pop() {
if (isEmpty()) {
System.out.println("Stack Underflow: Cannot pop element, stack is empty.");
return -1;
}
return stackArray[top--];
}
@Override
public boolean isEmpty() {
return top == -1;
}
@Override
public int size() {
return top + 1;
}
}
// iii. Dynamic-Length Stack Implementation
class DynamicLengthStack implements Stack {
private int[] stackArray;
private int top;
private int capacity;
public DynamicLengthStack() {
this.capacity = 10; // Initial capacity
this.stackArray = new int[capacity];
this.top = -1;
}
@Override
public void push(int element) {
if (top == capacity - 1) {
// If the stack is full, double the capacity
capacity *= 2;
int[] newStackArray = new int[capacity];
System.arraycopy(stackArray, 0, newStackArray, 0, stackArray.length);
stackArray = newStackArray;
}
stackArray[++top] = element;
}
@Override
public int pop() {
if (isEmpty()) {
System.out.println("Stack Underflow: Cannot pop element, stack is empty.");
return -1;
}
return stackArray[top--];
}
@Override
public boolean isEmpty() {
return top == -1;
}
@Override
public int size() {
return top + 1;
}
}
// iv. Class Demonstrating Runtime Binding and Stack Operations
public class 9StackOperations {
public static void main(String[] args) {
// Using FixedLengthStack through Interface reference
Stack fixedStack = new FixedLengthStack(5);
System.out.println("Fixed Length Stack Operations:");
performStackOperations(fixedStack);
// Using DynamicLengthStack through Interface reference
Stack dynamicStack = new DynamicLengthStack();
System.out.println("\nDynamic Length Stack Operations:");
performStackOperations(dynamicStack);
}
public static void performStackOperations(Stack stack) {
System.out.println("Stack Size: " + stack.size());
stack.push(10);
System.out.println("Stack Size: " + stack.size());
stack.push(20);
System.out.println("Stack Size: " + stack.size());
stack.push(30);
System.out.println("Stack Size: " + stack.size());
System.out.println("Pop: " + stack.pop());
System.out.println("Stack Size: " + stack.size());
System.out.println("Pop: " + stack.pop());
System.out.println("Stack Size: " + stack.size());
System.out.println("Is Empty: " + stack.isEmpty());
System.out.println("Stack Size: " + stack.size());
System.out.println("Pop: " + stack.pop());
System.out.println("Stack Size: " + stack.size());
System.out.println("Is Empty: " + stack.isEmpty());
}
}