-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.java
80 lines (70 loc) · 1.88 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
import java.util.Scanner;
public class stack {
int top;
int stack[];
int size;
stack(int size,int top){
this.size=size;
this.top=top;
this.stack=new int[size];
}
public void push(int ele){
if (top==size-1)
{
System.out.println("stqack overflow");
}
else {
stack[++top]=ele;
}
}
public int pop(){
if (top==-1){
return -1;
}
else {
return stack[top--];
}
}
public void display(){
if(top==-1)
System.out.println("the stack is empty");
else{
for(int i=top;i>=0;i--) {
System.out.print(stack[i] + " ");
}
}
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("enter the sizeof the stack");
int size=sc.nextInt();
stack s1=new stack(size,-1);
System.out.println("1.push");
System.out.println("2.pop");
System.out.println("3.dis[;ay");
System.out.println("4.exit");
while (true){
System.out.println("enter choice");
int choice=sc.nextInt();
switch (choice){
case 1:
System.out.println("enter elemnts to push");
int ele1=sc.nextInt();
s1.push(ele1);
break;
case 2:
int ele2= s1.pop();
if (ele2==-1){
System.out.println("stack underflow");}
break;
case 3:
s1.display();
break;
case 4:
System.exit(0);
break;
default:System.out.println("invalid choice");
}
}
}
}