-
Notifications
You must be signed in to change notification settings - Fork 1
/
teststack.java
100 lines (97 loc) · 2.21 KB
/
teststack.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
import java.io.*;
interface Stackoperation
{
public void push();
public void pop();
public void display();
}
class StackADT implements Stackoperation
{
int size=5;
int stack[]=new int[size];
int top=-1;
public void push()
{
try
{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
if(top==(size-1))
{
System.out.println(" Stack Overflow");
return;
}
else
{
System.out.println("Enter the element");
int ele=Integer.parseInt(br.readLine());
stack[++top]=ele;
}
}
catch(IOException e)
{
System.err.println(e);
}
}
public void pop()
{
if(top<0)
{
System.out.println("Stack underflow");
return;
}
else
{
int popper=stack[top];
top--;
System.out.println("Popped element:" +popper);
}
}
public void display()
{
if(top<0)
{
System.out.println("Stack is empty");
return;
}
else
{
String str=" ";
for(int i=0; i<=top; i++)
str=str+" "+stack[i]+" <--";
System.out.println("Elements are:"+str);
}
}
}
/* MAIN METHOD */
public class teststack extends StackADT
{
public static void main(String arg[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Implementation of Stack using Array");
StackADT o=new StackADT();
int ch=0;
do
{
System.out.println("1.Push 2.Pop 3.Display 4.Exit");
System.out.println("Enter your choice:");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
o.push();
break;
case 2:
o.pop();
break;
case 3:
o.display();
break;
case 4:
System.exit(0);
default:
System.exit(0);
break;
}
} while(ch<5);
}
}