-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSTACK_with_array.java
More file actions
81 lines (72 loc) · 1.77 KB
/
Copy pathSTACK_with_array.java
File metadata and controls
81 lines (72 loc) · 1.77 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
package DATA_STRUCTURE;
public class STACK_with_array {
static final int max = 6;
int top = -1; //LAST IN FIRST OUT (LIFO)
int []arr = new int[max];
public boolean isFull(){ //Checking whether the stack is full or not
if(top == (max-1)){
return true;
}
else{
return false;
}
}
public boolean isEmpty(){ //Checking whether the stack is empty or not
if(top==-1){
return true;
}
else {
return false;
}
}
boolean push(int d){ //pushing elements into stack
if(isFull()){
System.out.println("Stack Overflow..!");
return false;
}
else{
arr[++top] = d;
System.out.println(d+" pushed into stack");
return true;
}
}
void pop(){//removing elements from stack
if(isEmpty()){
System.out.println("Stack Underflow");
}
else{
int x = arr[top--];
System.out.println(x+" Removed from stack");
}
}
void display(){
while(top>=0){ //DOUBT???????????
System.out.println(arr[top]);
top--;
}
}
void peek(){
if(isEmpty()){
return;
}
else{
System.out.println("Top is "+arr[top]);
}
}
public static void main(String[] args) {
STACK_with_array s = new STACK_with_array();
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
s.push(6);
s.peek();
s.pop();
s.pop();
s.peek();
System.out.println("\n\n");
s.display();
System.out.println();
}
}