Repository navigation
Expand file tree
/
Copy pathStackTest.java
More file actions
109 lines (108 loc) · 2.19 KB
/
Copy pathStackTest.java
File metadata and controls
109 lines (108 loc) · 2.19 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
import java.io.*;
class StackType
{
int st[];
int top;
int size;
StackType(int s)
{
size = s;
top = -1;
st = new int[s];
}
public void push(int add)
{
top++;
st[top] = add;
}
public int pop()
{
int d = st[top];
top--;
return d;
}
public boolean isEmpty()
{
if (top == -1)
return true;
else
return false;
}
public boolean isFull()
{
if (top == size - 1)
return true;
else
return false;
}
public void display()
{
if (this.isEmpty())
System.out.println("Cannot display. Empty Stack");
else
{
System.out.print("Contents of the stack :");
for (int i = 0;i <= top;i++)
System.out.print(st[i] + " ");
System.out.println();
}
}
}
class StackTest
{
public static void main(String[] args) throws IOException
{
StackType s;
int n;
int opChoice;
char choice;
int add, del;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Stack Operations Program ");
System.out.print("\nEnter size of the stack :");
n = Integer.parseInt(br.readLine());
s = new StackType(n);
do
{
System.out.println("\n***************************************************************************");
System.out.println("\nChoose an operation from the given menu :");
System.out.println("1.Push");
System.out.println("2.Pop");
System.out.println("3.Display");
System.out.print("Enter your choice(1-3) :");
opChoice = Integer.parseInt(br.readLine());
switch (opChoice)
{
case 1:
if (s.isFull())
System.out.println("Cannot Push. Stack Overflow. ");
else
{
System.out.print("Enter an integer to be added :");
add = Integer.parseInt(br.readLine());
s.push(add);
s.display();
}
break;
case 2:
if (s.isEmpty())
System.out.println("Cannot Pop. Stack Underflow. ");
else
{
del = s.pop();
System.out.println(del + " has been popped out of stack.");
s.display();
}
break;
case 3:
s.display();
break;
default:
System.out.println("Wrong Operation Choice...");
}
System.out.print("\nDo you want to continue?(Y/N) :");
choice = br.readLine().charAt(0);
}
while (choice == 'Y' || choice == 'y');
}
}