-
Notifications
You must be signed in to change notification settings - Fork 48
/
StackImplementationForCharacter.java
66 lines (49 loc) · 1.38 KB
/
StackImplementationForCharacter.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
public class StackImplementationForCharacter {
private char arr[];
private int top;
private int capacity;
// Creating a Stack
StackImplementationForCharacter(int size){
arr = new char [size];
capacity = size;
top = -1;
}
// Inserting onto Stack
public void push(char data){
if(isFull()) {
System.out.println("OVERFLOW\nProgram terminated");
System.exit(0);
}
System.out.println("Inserting..."+data);
arr[++top] = data;
}
// Removing from the Stack
public int pop(){
if (isEmpty()){
System.out.println("STACK is Empty");
System.exit(0);
}
return (char)arr[top--];
}
// Utility function to return Size of Stack
public int size(){
return top+1;
}
// Check if Stack is Full
boolean isFull(){
return top == capacity - 1;
}
// Check if Stack is Empty
boolean isEmpty(){
return top == -1;
}
// Printing the Stack
public void printStack(){
for(int i = 0;i<=top;i++){
System.out.println(arr[i]);
}
}
public static void main(String[] args) {
StackImplementationForCharacter stack = new StackImplementationForCharacter(10);
}
}