-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathstackpr1.java
98 lines (97 loc) · 2.27 KB
/
stackpr1.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
package Data_Structures;
import java.util.*;
class stackpr1
{
private int ar[];
private int top,size;
stackpr1(int n)
{
ar = new int[n];
top = -1;
size = n;
}
public boolean isEmpty()
{
return top == -1;
}
public boolean isFull()
{
return top == size - 1;
}
public void push(int x)
{
if(isFull())
{
System.out.println("OverFlow Condition");
}
else
{
ar[++top] = x;
System.out.println(ar[top]+ " has been added to the stack");
}
}
public void pop()
{
if(isEmpty())
{
System.out.println("UnderFlow Condition");
}
else
{
System.out.println(ar[top] + " has been removed");
top--;
}
}
public void display()
{
if(isEmpty())
{
System.out.println("There are no elements in the Stack");
}
else
{
System.out.println("Printing elements in Stack: ");
for(int i=0;i<=top;i++)
{
System.out.println(ar[i]);
}
}
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter Size of Stack");
int n = sc.nextInt();
char ch = 'a';
stackpr1 stack = new stackpr1(n);
do
{
System.out.println("Pick one of the choices: ");
System.out.println("1)Push");
System.out.println("2)Pop");
System.out.println("3)Display");
int ans = sc.nextInt();
if(ans==1)
{
System.out.println("Enter number to be pushed");
int a = sc.nextInt();
stack.push(a);
}
else if(ans==2)
{
stack.pop();
}
else if(ans==3)
{
stack.display();
}
else
{
System.out.println("Wrong chocie");
}
System.out.println("Would you like to continue? ");
ch = sc.next().charAt(0);
}while(ch=='y'||ch=='Y');
sc.close();
}
}