-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
55 lines (55 loc) · 1.51 KB
/
Stack.java
File metadata and controls
55 lines (55 loc) · 1.51 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
import java.util.Scanner;
class Stack {
int size = 5;
int Stk[] = new int[size];
int i, ele, top;
Stack() {
top = -1;
}
void push() {
if (top == size - 1) {
System.out.println("Stack is full");
} else {
Scanner sc = new Scanner(System.in);
System.out.print("Enter element to push: ");
ele = sc.nextInt();
top++;
Stk[top] = ele;
}
}
void pop() {
if (top == -1) {
System.out.println("Stack is empty");
} else {
ele = Stk[top];
top--;
System.out.println("Popped element: " + ele);
}
}
void display() {
if (top == -1) {
System.out.println("Stack is empty");
} else {
System.out.println("Stack elements are:");
for (int i = top; i >= 0; i--) {
System.out.println(Stk[i]);
}
}
}
public static void main(String[] args) {
Stack s = new Stack();
Scanner sc = new Scanner(System.in);
int select;
do {
System.out.println("\n1. Push\n2. Pop\n3. Display");
System.out.print(": Select from following: ");
select = sc.nextInt();
switch (select) {
case 1: s.push(); break;
case 2: s.pop(); break;
case 3: s.display(); break;
default: System.out.println("Invalid choice! Please try again.");
}
} while (select != 3);
}
}