-
Notifications
You must be signed in to change notification settings - Fork 50
/
stack.java
98 lines (83 loc) · 1.7 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
// Program to implement Stack in java
import java.io.DataInputStream;
import java.io.IOException;
class stack_operations
{
int stack[] = new int[10];
int TOP = -1;
DataInputStream dis = new DataInputStream(System.in);
public void insert_operation() throws IOException
{
int a;
if(TOP == 9)
{
System.out.println("STACK OVERFLOW");
}
else
{
System.out.println("ENTER THE ELEMENT WHICH YOU WANT TO INSERT :");
a = Integer.parseInt(dis.readLine());
TOP++;
stack[TOP] = a;
}
}
public void remove_operation() throws IOException
{
int a;
if(TOP == -1)
{
System.out.println("STACK UNDERFLOW");
}
else
{
System.out.print("REMOVED TOP ELEMENT FROM THE STACK IS : " +stack[TOP]);
TOP--;
}
}
public void display_operation() throws IOException
{
int i;
if(TOP == -1)
{
System.out.println("STACK IS EMPTY, NOTHING TO DISPLAY");
}
else
{
System.out.println("STACK CONTAINS");
for(i=TOP;i>=0;i--)
{
System.out.println(stack[i]);
}
}
}
}
class stack
{
public static void main(String arr[]) throws IOException
{
int ch;
stack_operations s = new stack_operations();
DataInputStream dis = new DataInputStream(System.in);
do
{
System.out.println("\n1 - INSERT");
System.out.println("2 - REMOVE");
System.out.println("3 - DISPLAY");
System.out.println("4 - EXIT");
System.out.print("PROVIDE YOUR CHOICE :");
ch = Integer.parseInt(dis.readLine());
switch(ch)
{
case 1: s.insert_operation();
break;
case 2: s.remove_operation();
break;
case 3: s.display_operation();
break;
case 4: System.exit(0);
break;
default: System.out.println("INVALID CHOICE");
}
}while(ch!=0);
}
}