-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStackADT.java
84 lines (75 loc) · 1.48 KB
/
StackADT.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
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
class Stack
{
int stk[];
int top;
final int MAX = 6;
public Stack()// Constructor initializes Stack & top
{
stk = new int[MAX];
top = 0;
}
public void push(int e)
{
if(isFull())
{
System.out.println("Stack is Overflow ..!");
return;
}
stk[++top] = e;
System.out.println("Item "+e+" pushed...");
}
public int pop()
{
if(isEmpty())
{
System.out.println("Stack is Underflow..!");
return -1;
}
int k = stk[top--];
return k;
}
public boolean isFull()
{
return (top==MAX-1)? true : false ;
}
public boolean isEmpty()
{
return (top == 0)? true : false ;
}
public int peek()
{
return stk[top]; // returns topmost element in the stack
}
public void viewStack()
{
if(isEmpty())
{
System.out.println("No Data in Stack...!");
return;
}
System.out.println("\n The Content of Stack is...\n");
for(int i = top; i>0;i--)
System.out.print(stk[i]+" ");
}
}
public class StackADT
{
public static void main(String args[])
{
Stack obj = new Stack();
obj.push(77);
obj.push(57);
obj.push(47);
obj.push(67);
obj.push(12);
obj.push(80);
obj.viewStack();
System.out.println("Topmost element = "+obj.peek());
System.out.println("Popped..."+obj.pop());
System.out.println("Popped..."+obj.pop());
System.out.println("Popped..."+obj.pop());
System.out.println("Topmost element = "+obj.peek());
obj.push(100);
obj.viewStack();
}
}