-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStackA.java
90 lines (77 loc) · 1.48 KB
/
StackA.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
Copyright (C) Deepali Srivastava - All Rights Reserved
This code is part of DSA course available on CourseGalaxy.com
*/
package stackArray;
import java.util.EmptyStackException;
public class StackA
{
private int[] stackArray;
private int top;
public StackA()
{
stackArray = new int[10];
top = -1;
}
public StackA(int maxSize)
{
stackArray = new int[maxSize];
top = -1;
}
public int size()
{
return top+1;
}
public boolean isEmpty()
{
return (top==-1);
}
public boolean isFull()
{
return (top==stackArray.length-1);
}
public void push(int x)
{
if(isFull())
{
System.out.println("Stack Overflow");
return;
}
top=top+1;
stackArray[top]=x;
}
public int pop()
{
int x;
if(isEmpty())
{
System.out.println("Stack Underflow");
throw new EmptyStackException();
}
x=stackArray[top];
top=top-1;
return x;
}
public int peek()
{
if(isEmpty())
{
System.out.println("Stack Underflow");
throw new EmptyStackException();
}
return stackArray[top];
}
public void display()
{
int i;
if(isEmpty())
{
System.out.println("Stack is empty");
return;
}
System.out.println("Stack is : ");
for(i=top; i>=0; i--)
System.out.println(stackArray[i] + " ");
System.out.println();
}
}