-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackUsingArray.java
More file actions
67 lines (59 loc) · 1.18 KB
/
StackUsingArray.java
File metadata and controls
67 lines (59 loc) · 1.18 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
public class StackUsingArray {
private int[] arr;
private int size=0;
private int top=-1;
StackUsingArray(int size)
{
this.size=size;
arr=new int[size];
}
public void push(int no)
{
if(top==size-1)
{
throw new Error("Index out of Bound");
}
arr[++top]=no;
}
public void pop()
{
if(top==-1)
{
throw new Error("No element Present in the Erray");
}
top--;
}
public void display()
{
if(top==-1)
{
System.out.println("Stack is Empty");
return ;
}
for(int i=top;i>=0;i--)
{
System.out.println(arr[i]);
}
}
public int peek()
{
if(top==-1)
{
return -1;
}
return arr[top];
}
public int length()
{
return top+1;
}
public static void main(String args[]) {
StackUsingArray m=new StackUsingArray(2);
m.push(2);
m.push(3);
// m.pop();
m.display();
System.out.println(m.peek());
System.out.println(m.length());
}
}