-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackprogram.java
More file actions
54 lines (51 loc) · 1.23 KB
/
stackprogram.java
File metadata and controls
54 lines (51 loc) · 1.23 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
import java.util.Scanner;
class stackprogram{
int size;
int top;
int stack[];
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
System.out.println("Enter the size of the stack");
stackprogram stk = new stackprogram(scan.nextInt());
System.out.println("Stack operations\n1.push\n2.pull");
int choice;
do {
System.out.println("Enter the choice");
choice=scan.nextInt();
switch(choice){
case 1:
System.out.println("Enter the element you want to insert");
stk.push(scan.nextInt());
break;
case 2:
System.out.println(stk.pull());
break;
case 3:
java.lang.System.exit(0);
break;
}
} while (choice!=3);
}
stackprogram(int size){
this.size=size;
stack= new int[size];
top=-1;
}
void push(int num){
if(top==size-1){
System.out.println("Stack overflow");
}
else{
stack[++top]=num;
}
}
int pull(){
if(top==-1){
System.out.println("Stack underflow");
return(-1);
}
else{
return stack[top--];
}
}
}