-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPushStack.java
More file actions
44 lines (38 loc) · 983 Bytes
/
Copy pathPushStack.java
File metadata and controls
44 lines (38 loc) · 983 Bytes
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
public class PushStack
{
static class Stack{
private int maxsize;
private int stackArray[];
private int top;
// constructor to initialize stack with a max size
Stack(int size){
maxsize= size;
stackArray= new int[maxsize];
top= -1;
}
void push(int val){
if(top==maxsize-1){
System.out.println("Stack overflow");
return;
}
stackArray[++top] = val;
System.out.println(val + " pushed into stack");
}
void display(){
for(int i=0; i<maxsize; i++){
System.out.print(stackArray[i]+ " ");
}
}
}
public static void main(String[] args) {
Stack st= new Stack(5);
st.push(1);
st.push(2);
st.push(3);
st.push(4);
st.push(5);
st.push(6);
st.push(7);
st.display();
}
}