forked from VenusTokyo/first-year-java-practicals
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStackMain.java
More file actions
110 lines (91 loc) · 2.18 KB
/
StackMain.java
File metadata and controls
110 lines (91 loc) · 2.18 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
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
import java.util.Scanner;
class over_under_flow extends Exception {
//Class that extends Exception
//Meant for declaring custom-type exceptions
}
class Stack_Array {
static int max_size = 5;
int Stack[] = new int[max_size];
int sp = -1;
void Push(int value) throws over_under_flow {
if (this.sp == max_size - 1) {
throw new over_under_flow();
}
this.sp++;
this.Stack[sp] = value;
}
void Pop() throws over_under_flow {
int temp;
if (this.sp < 0) {
throw new over_under_flow();
}
else {
System.out.println("The element being popped is:" + this.Stack[this.sp]);
temp = this.Stack[this.sp];
this.sp--;
}
}
void PrintStack() {
int sp1 = this.sp;
if (sp1 < 0) {
System.out.println("Stack is empty\n");
} else {
System.out.print("\nThe Stack is laid as follows:\n[");
while (sp1 >= 0) {
System.out.print("\t\t" + this.Stack[sp1]);
sp1--;
}
System.out.print("\t\t]\n");
}
}
}
public class StackMain {
public static void main(String[] args) {
Stack_Array obj = new Stack_Array();
obj.sp = -1;
Scanner read = new Scanner(System.in);
int choice = 0;
while (choice != 4) {
for (int i = 0; i < 70; i++) {
System.out.print("*");
}
System.out.println("\n\t\t\t1.Push");
System.out.println("\n\t\t\t2.Pop");
System.out.println("\n\t\t\t3.Print");
System.out.println("\n\t\t\t4.Quit\n");
for (int j = 0; j < 70; j++) {
System.out.print("*");
}
System.out.println("\nEnter your choice\n");
choice = read.nextInt();
switch (choice) {
case 1:
try{
int value;
System.out.println("\nEnter the value to be added:\n");
value = read.nextInt();
obj.Push(value);
}catch(over_under_flow ob) {
System.out.println("Stack overflow exception");
}
break;
case 2:
try
{obj.Pop();
}catch(over_under_flow ob) {
System.out.println("Stack underflow exception");
}
break;
case 3:
obj.PrintStack();
break;
case 4:
System.exit(0);
break;
default:
System.out.println("\nEnter a valid choice\n");
break;
}
}
}
}