-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackX.java
More file actions
60 lines (49 loc) · 1.03 KB
/
StackX.java
File metadata and controls
60 lines (49 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
57
58
59
60
package exam;
public class StackX {
private int maxSize; //maximum size of stackarray
private int top; // define a top value
private char[ ] stackArray; //create stack array
//define constructor
public StackX(int size){
maxSize = size;
stackArray = new char[maxSize];
top = -1;
}
//push method
public void push(char ch) {
//check whether the stack is full
if(top == maxSize - 1)
System.out.println("Stack array is full");
else
stackArray[top++] = ch;
}
//pop method
public char pop() {
//check whether the stack is empty
if(top == -1)
{
System.out.println("Stack array is empty");
return ' ';
}
else
return stackArray[top--];
}
//peek method
public char peek() {
//check whether the stack is empty
if(top == -1)
{
System.out.println("Stack array is empty");
return ' ';
}
else
return stackArray[top];
}
public boolean isEmpty() {
//check whether the stack is empty
return (top == -1);
}
public boolean isFull() {
return (top == maxSize - 1);
}
}