-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStack.java
More file actions
76 lines (66 loc) · 1.92 KB
/
Copy pathStack.java
File metadata and controls
76 lines (66 loc) · 1.92 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.programs;
import java.util.Scanner;
class StackDemo {
int top,size,stack[];
public StackDemo(int arraySize) {
this.size = arraySize;
stack = new int[size];
top = -1;
}
public void push(int value) {
if(top==size-1)
System.out.println("Stack is full , cant push a value :(");
else
stack[++top] = value;
}
public int pop() {
int t=0;
if(top==-1) {
System.out.println("Cant pop...stack is empty :(");
return -1;
}
else {
t = top--;
return stack[t];
}
}
public void display() {
for(int i=top;i>=0;i--)
System.out.println(stack[i]);
System.out.println();
}
}
class Stack {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Stack operations");
System.out.println("Enter the size of stack:");
int n = in.nextInt();
int choice;
StackDemo stk = new StackDemo(n);
do {
System.out.println("1. Push");
System.out.println("2. Pop");
System.out.println("3. Display");
System.out.println("Enter the choice:");
int ch = in.nextInt();
switch(ch) {
case 1:
System.out.println("Enter the element to push:");
int ele = in.nextInt();
stk.push(ele);
break;
case 2:
int s = stk.pop();
if(s!=-1)
System.out.println("Popped Element is: "+s);
break;
case 3:
stk.display();
break;
}
System.out.println("Do u wish to continue:");
choice = in.nextInt();
} while(choice==1);
}
}