-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
108 lines (75 loc) · 1.52 KB
/
Stack.java
File metadata and controls
108 lines (75 loc) · 1.52 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
public class Stack {
//if top = -1, then the stack is empty
int top = -1;
int stack[];
//Define the stack size here
void stackSize(int num) {
//initialize array to preffered size
stack = new int[num];
System.out.println("The stack size is: "+stack.length);
}
//inserting into a stack
public int push(int num) throws ArrayIndexOutOfBoundsException{
top = top + 1;
if(top >= stack.length) {
System.out.println("Stack is full");
}else {
stack[top] = num;
System.out.println("pushed "+stack[top]);
}
return 0;
}
public int pop() {
if(top == -1) {
System.out.println("Stack is empty");
}
else {
System.out.println("popped " + stack[top]);
top = top - 1;
}
return 0;
}
int peek() {
if(top == -1) {
System.out.println("Stack is empty");
}
System.out.println(stack[top]);
return 0;
}
int isFull() {
if(top < stack.length - 1) {
System.out.println("Not yet full my friend");
}
else {
System.out.println("Very much my friend");
}
return 0;
}
int isEmpty() {
if(top == -1 ) {
System.out.println("Stack is empty");
}
else {
System.out.println("No, some elements are there");
}
return 0;
}
public static void main(String args[]) {
Stack s1 = new Stack();
s1.stackSize(5);
//s1.push(4);
//s1.push(6);
//s1.push(4);
//s1.push(4);
//s1.push(9);
//s1.pop();
//s1.pop();
//s1.pop();
//s1.pop();
//s1.pop();
//s1.pop();
//s1.peek();
s1.isFull();
s1.isEmpty();
}
}