forked from ranjansharma255/Java_Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackInterface.java
More file actions
87 lines (84 loc) · 1.64 KB
/
StackInterface.java
File metadata and controls
87 lines (84 loc) · 1.64 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
import java.util.Scanner;
interface StackADT
{
void push() throws Exception;
void pop();
void display();
}
class StackInterface implements StackADT
{
int size,top;
int arr[];
Scanner s = new Scanner(System.in);
StackInterface()
{
System.out.println("Enter the size of the stack");
size = s.nextInt();
arr = new int[size];
top = -1;
}
public void push() throws Exception
{
if(top == arr.length-1)
{
System.out.println("StackOverflow");
}
else
{
System.out.println("Enter the element to push in the stack");
int element = s.nextInt();
top = top+1;
arr[top] = element;
}
}
public void pop()
{
if(top == -1)
{
System.out.println("Stack Underflow");
}
else
{
System.out.println("The Popped element is "+ arr[top]);
top--;
}
}
public void display()
{
if(top == -1)
{
System.out.println("The Stack is empty");
}
else
{
for (int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
}
}
public static void main(String [] args) throws Exception
{
Scanner s = new Scanner(System.in);
System.out.println("Stack Array ADT");
StackInterface si = new StackInterface();
String ch ="y";
while(ch.equals("y"))
{
System.out.println("Stack Array operations");
System.out.println("1.Push\t2.Pop\t3.Display\t4.Exit");
System.out.println("Enter your choice");
int opt = s.nextInt();
switch(opt)
{
case 1: si.push(); break;
case 2: si.pop(); break;
case 3: si.display(); break;
case 4: System.exit(0);
default : System.out.println("Invalid option");
}
System.out.println("Do you want to continue (y/n)");
ch = s.next();
}
}
}