-
Notifications
You must be signed in to change notification settings - Fork 0
/
StackProgram.java
56 lines (51 loc) · 1016 Bytes
/
StackProgram.java
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
package OCJP;
class Stack{
private int stackSize;
private long[] stack;
private int top;
// Constructor
public Stack(int maxSize){
stackSize = maxSize;
stack = new long[stackSize];
top = -1;
}
// Check whether the Stack is empty or not
public boolean isEmpty(){
return (top == -1);
}
// Check whether stack is full or not
public boolean FullorNot(){
return (top == stackSize-1);
}
// Pushing an item onto the stack
public void push(long item){
stack[++top] = item;
}
// Pop off an item
public long pop(){
return stack[top--];
}
// Display the top
public long seek(){
return stack[top];
}
// Display the stack values
public void display(){
for(int i=0;i<=top;i++){
System.out.print(" "+stack[i]);
}
System.out.println();
}
}
public class StackProgram {
public static void main(String[] args) {
Stack s = new Stack(10);
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
s.display();
System.out.println("The popped item is: "+s.pop());
}
}