-
Notifications
You must be signed in to change notification settings - Fork 0
/
StackWithArray.java
83 lines (76 loc) · 1.63 KB
/
StackWithArray.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class StackWithArray {
int []stack=new int[5];//stack size
int top=-1;
public void push(int ele)
{
if(top==stack.length-1)
System.out.println("stack is already full");
else
stack[++top]=ele;
}
public int pop()
{
if(top==-1) {
System.out.println("Stack is already empty");
return 0;
}else
return stack[top--];
//top--;
//return temp;
}
public int peek()
{
if(top==-1) {
System.out.println("Stack is already empty");
return 0;
}else
return stack[top];
}
public boolean isEmpty()
{
if(top==-1)
return true;
else
return false;
}
public void print()
{
for(int i=top;i>=0;i--)
System.out.println(stack[i]+"-->");
}
public static void main(String[] args) throws NumberFormatException, IOException {
// TODO Auto-generated method stub
StackWithArray stack=new StackWithArray();
while(true)
{
System.out.println("push 1, pop 2, peek 3, empty 4, print 5, other than this for exit");
System.out.println("Enter the operation code");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
switch(Integer.parseInt(br.readLine()))
{
case 1:
System.out.println("Enter the element");
stack.push(Integer.parseInt(br.readLine()));
break;
case 2:
stack.pop();
break;
case 3:
System.out.println(stack.peek());
break;
case 4:
System.out.println(stack.isEmpty());
break;
case 5:
stack.print();
break;
default:
System.exit(0);
break;
}
}
}
}