-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
47 lines (41 loc) · 1.01 KB
/
Stack.java
File metadata and controls
47 lines (41 loc) · 1.01 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
/*This is to basically implement the pushing and popping operation in a stack.*/
import java.util.*;
public class Stack {
static int maximum = 1000;
int top;
int[] A = new int[maximum];
boolean isStackEmpty() {
if(top<0)
return false;
else
return true;
}
Stack(){
top = -1;
}
public void push(int x) {
if(top >= maximum-1)
System.out.println("Stack Overflow! ");
else
A[++top] = x;
}
//This function is basically to delete element or pop element
public int pop() {
int x;
if(top<0){
System.out.println("Stack Underflow! ");
return 0;
}
else
x = A[top--];
return x;
}
//Main function to call all other functions.
public static void main(String[] args){
Stack s = new Stack();
s.push(1);
s.push(2);
s.push(3);
System.out.println(s.pop() + " popped from the stack");
}
}