-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.java
67 lines (66 loc) · 1.32 KB
/
Stack.java
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
import java.util.Scanner;
public class stack {
int num[] ;
int i;
int top;
int maxSize;
public stack(int length)
{
this.maxSize=length;
top=-1;
num=new int [maxSize];
}
public boolean isEmpty()
{
return top==-1;
}
public boolean isFull()
{
return top==maxSize-1;
}
public void push(int newElm)
{
if(!isFull()) {
num[++top] = newElm;
}
else
{
System.out.println("array is full");
}
}
public int pop()
{
return num[top--];
}
public void peek()
{
if(!isEmpty())
System.out.println("top element is "+num[top]);
else
System.out.println("array is Empty");
}
public void print() {
if(isEmpty())
{
System.out.println("array is Empty");
}
else {
System.out.println("numbers are:");
for (i = 0; i <= top; i++) {
System.out.print(num[i] + " ");
System.out.println();
}
}
}
public static void main(String a [])
{
stack st=new stack(5);
st.push(50);
st.push(45);
st.push(65);
st.push(85);
st.pop();
st.print();
st.peek();
}
}