-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackImplementationWithArray.java
More file actions
65 lines (56 loc) · 1.1 KB
/
Copy pathStackImplementationWithArray.java
File metadata and controls
65 lines (56 loc) · 1.1 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
public class StackImplementationWithArray {
int top=-1;
int stackSize;
int[] stack;
StackImplementationWithArray(int stackSize)
{
this.stackSize=stackSize;
stack=new int[stackSize];
}
public StackImplementationWithArray() {
// TODO Auto-generated constructor stub
}
public void push(int data) throws Exception
{
if(top==stackSize-1)
throw new StackOverflowException();
stack[++top]=data;
}
public int pop() throws Exception
{
if(top==-1)
throw new StackEmptyException();
int returnValue=stack[top];
stack[top--]=-1;
return returnValue;
}
public int peek() throws Exception
{
if(top==-1)
throw new StackEmptyException();
return stack[top];
}
public void display()
{
for(int i=top;i>=0;i--)
System.out.println(stack[i]);
}
public boolean isEmpty()
{
return top==-1;
}
class StackOverflowException extends Exception
{
public StackOverflowException() {
// TODO Auto-generated constructor stub
super("Stack Overflow!!!");
}
}
class StackEmptyException extends Exception
{
public StackEmptyException() {
// TODO Auto-generated constructor stub
super("Stack Empty...Can't pop tarts!!!");
}
}
}