-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.java
131 lines (113 loc) · 2.29 KB
/
Stack.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
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
127
128
129
130
131
package dataStructures;
import arrays.Beautify;
public class Stack
{
Object[] stack; private int top=-1;private boolean shred=false;
public void create(int size)
{
stack=new Object[size];
System.out.println("stack had been created");
}
public boolean isEmpty()
{
return top==-1;
}
public boolean isFull()
{
return top<stack.length;
}
public void push(Object data)
{
if(top<stack.length-1)
{
++top; //coz top starts from -1
stack[top]=data;
System.out.println("Push Successful");
}
else System.out.println("stack overflow....");
}
public void pop()
{
if(top>-1)
{
System.out.println(stack[top]+" popped");
top--;
}
else System.out.println("Stack is Empty");
}
public void clear()
{
top=-1;
System.out.println("Stack cleared");
}
public void display()
{
new Beautify().display(top,stack);
}
public void recovery()
{
System.out.println("Welcome to Recovery Wizard!\n\nRecovery is not possible if stack is shredded\n");
if(!shred)
{
top=stack.length-1;
display();
System.out.println("Congratulations! Recovery Successful");
}
else System.out.println("OOppss... we have found that you have been shredded the stack");
}
public void shred()
{
while(top>-1)
{
stack[top]=0;
top--;
}
System.out.println("shredding completed...");
shred=true;
}
public static void main(String[] args)
{
Stack s=new Stack();
s.create(7);
System.out.println();
s.isEmpty();
System.out.println();
s.isFull();
System.out.println();
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
// s.push(6);
// s.push(7);
// s.push(8);
System.out.println();
s.display();
System.out.println();
s.isEmpty();
System.out.println();
s.isFull();
s.pop();
s.pop();
s.pop();
System.out.println();
System.out.println();
s.display();
System.out.println();
s.isEmpty();
s.recovery();
System.out.println();
s.clear();
System.out.println();
s.display();
System.out.println();
s.recovery();
System.out.println();
s.shred();
System.out.println();
s.recovery();
//int a[] = {1,2,3,4,5,6,7};
//new ArrayDisplay().display(new char[2], new int[2],new int[3]);
}
}