-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStack.java
116 lines (91 loc) · 2.25 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import java.util.*;
class Stack{
private int top=-1;
private int maxSize;
private int[] stackArray;
public Stack(int s){
maxSize = s;
stackArray = new int[maxSize];
}
public void push(int d){
if(top==maxSize-1)
System.out.println("Overflow!");
else
top = top + 1;
stackArray[top] = d;
}
public int pop(){
if(top==-1){
System.out.println("Underflow!");
return -1;
}
return stackArray[top--];
}
public int peep(){
if(top==-1){
System.out.println("No element exists!");
return -1;
}
return stackArray[top];
}
public boolean isEmpty(){
return (top == -1);
}
public boolean searchifExists(int d){
int i=0;
while(i<=top){
if(stackArray[i]==d){
System.out.println("Yes, the number exists and its index is : "+i);
return true;
}
else
i++;
}
return false;
}
public void display(){
int i;
for(i=0;i<=top;i++){
System.out.print(stackArray[i] + " ");
}
}
}
class test{
public static void main(String args[]){
Scanner ip = new Scanner(System.in);
Stack obj = new Stack(5);
System.out.println("[1].Enter 1 to push in the stack.");
System.out.println("[2].Enter 2 to pop from the stack.");
System.out.println("[3].Enter 3 to peep in the stack.");
System.out.println("[4].Enter 4 to display elements in the stack.");
System.out.println("[5].Enter 5 to find if a number exists in the stack.");
System.out.println("[6].Enter 6 to exit.");
int v,res;
while(true){
res = ip.nextInt();
switch(res){
case 1:
obj.push(v = ip.nextInt());
break;
case 2:
System.out.println(obj.pop());
break;
case 3:
System.out.println(obj.peep());
break;
case 4:
obj.display();
break;
case 5:
System.out.println(obj.searchifExists(v = ip.nextInt()));
break;
case 6:
break;
default:
System.out.println("You entered a wrong input !");
}
if(res==6)
break;
}
}
}