-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha28Stacks.java
More file actions
98 lines (89 loc) · 2.43 KB
/
a28Stacks.java
File metadata and controls
98 lines (89 loc) · 2.43 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
import java.util.Scanner;
import java.util.Stack;
public class a28Stacks {
int stack[];
int top;
int size;
public a28Stacks(int stackSize){
stack = new int[stackSize];
size = stackSize;
top = -1;
}
public void push(int ele){
if (top>=size-1) {
System.out.println("Stack is full -> Overflow");
return;
}
top++;
stack[top] = ele;
}
public boolean isEmpty(){
return (top <= -1);
}
public int size(){
return top+1;
}
public int peek(){
if (isEmpty()) {
System.out.println("Stack is Empty");
return Integer.MIN_VALUE;
}
return stack[top];
}
public int pop(){
int val = peek();
if (val!=Integer.MIN_VALUE) {
top--;
}
return val;
}
public void printStack(){
System.out.println("Printing stack");
for (int i = 0; i <= top; i++) {
System.out.print(stack[i]);
if (i!=top) {
System.out.print(',');
}
}
System.out.println();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Size of Stack: ");
int n = sc.nextInt();
a28Stacks stack = new a28Stacks(n);
System.out.println("isEmpty:"+ stack.isEmpty());
stack.push(9);
stack.printStack();
stack.push(1);
stack.printStack();
stack.push(8);
stack.printStack();
stack.push(5);
stack.printStack();
stack.push(8);
stack.printStack();
stack.push(2);
stack.printStack();
stack.push(6);
stack.printStack();
stack.pop();
stack.printStack();
System.out.println("size:"+ stack.size());
System.out.println("isEmpty:"+ stack.isEmpty());
stack.pop();
stack.printStack();
stack.pop();
stack.printStack();
stack.pop();
stack.printStack();
System.out.println("peek:"+ stack.peek());
//Using stack from JC framework
// Stack<Integer> myClass = new Stack<>();
// myClass.peek();
// myClass.push(7);
// myClass.pop();
// myClass.size();
// myClass.isEmpty();
}
}