-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
57 lines (50 loc) · 1.03 KB
/
Stack.java
File metadata and controls
57 lines (50 loc) · 1.03 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
import java.util.*;
public class Stack
{ int stk[]=new int[200];
int capacity,top;
Stack()
{
for(int i=0;i<200;i++)
stk[i]=0;
}
Stack(int cap)
{
capacity=cap;
top=-1;
}
void pushItem(int val)
{
if(top==capacity)
System.out.println("Stack Overflow!");
else
{
++top;
stk[top]=val;
}
}
int popItem()
{
if(top == -1)
{
System.out.println("Stack Underflow!");
return -9999;
}
else
{
int x=stk[top];
top--;
return x;
}
}
void print_stack()
{
if(top== -1)
System.out.println("Stack Underflow!");
else
{
System.out.println("The Elements in the Stack:");
for(int i=top;i>=0;i--)
System.out.println(stk[i]);
}
}
}