-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLab05_3_630510606.java
114 lines (95 loc) · 2.52 KB
/
Lab05_3_630510606.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
Anawin Athawong
630510606
*/
import java.util.Scanner;
class Menu {
private static int choice;
public static void setChoice() {
Scanner input = new Scanner(System.in);
System.out.printf("**********\n1.push\n2.pop\n3.show\n4.exit\n**********\nPlease select choice : ");
do {
choice = input.nextInt();
} while (choice > 4 && choice < 1);
}
public static int getChoice() {
return choice;
}
}
class Stack {
private static int item[] = new int[5];// ;
private static int top = -1;
public Stack() {
top = -1;
}
public static void push(int x) {
if (!isFull()) {
item[++top] = x;
} else {
System.out.printf("stack is full.\n");
}
}
public static void pop() {
if (!isEmpty()) {
System.out.printf("pop %d\n", item[top--]);
} else {
System.out.printf("stack is empty.\n");
}
}
public static void show() {
if (!isEmpty()) {
for (int i = 0; i <= top; i++) {
System.out.print(item[i]);
System.out.print(" ");
}
System.out.println();
} else {
System.out.printf("stack is empty.\n");
}
}
private static boolean isEmpty() {
if (top == -1) {
return true;
} else {
return false;
}
}
private static boolean isFull() {
if (top == 4) {
return true;
} else {
return false;
}
}
public static int gettop() {
return top;
}
}
public class Lab05_3_630510606 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
do {
Menu.setChoice();
switch (Menu.getChoice()) {
case 1:
if (Stack.gettop() == 4) {
System.out.printf("stack is full.\n");
break;
} else {
System.out.print("Enter data: ");
Stack.push(keyboard.nextInt());
break;
}
case 2:
Stack.pop();
break;
case 3:
Stack.show();
break;
default:
System.out.println("Bye bye");
break;
}
} while (Menu.getChoice() != 4);
}
}