-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathStack.java
More file actions
66 lines (59 loc) · 1.07 KB
/
Stack.java
File metadata and controls
66 lines (59 loc) · 1.07 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
class Stack
{
int MAX;
int top;
int a[];
boolean isEmpty()
{
return (top < 0);
}
Stack()
{
this.top = -1;
this.MAX=1000;
this.a = new int[MAX];
}
Stack(int cap)
{
this.top=-1;
this.MAX=cap;
this.a = new int[MAX];
}
void push(int x)
{
if (top >= (MAX - 1))
System.out.println("Stack capacity reached");
else
a[++top] = x;
}
void pop()
{
if (top < 0)
System.out.println("Stack empty");
else
top--;
}
int peek()
{
if (top < 0) {
return 0;
}
else {
int x = a[top];
return x;
}
}
public static void main(String[] args)
{
Stack s = new Stack(5);
s.push(5);
s.push(7);
s.push(4);
System.out.println(s.peek());
s.pop();
System.out.println(s.peek());
s.pop();
s.pop();
s.pop();
}
}