In my previous post we talked about basic stack terminologies and simple
stack implementation using integer array.
In this post I will be sharing my code for a stack implementation which can hold char values in other words stack implemented using char array in java.
char Array Based Implementation of stack.
Let see the code directly as it is self explanatory
public class StackUsingCharArray {
// Every stack has a maximum capacity i.e. number of objects it can hold,
private static final int CAPACITY = 10;
// Top will always point to most recently added element in stack therefore
// top = -1 implies stack is empty
private int top = -1;
//array to hold stack elemnts.lt is initialized to capacity of stack
private char[] stackArray = new char[CAPACITY];
public static void main(String[] args) {
StackUsingCharArray stack = new StackUsingCharArray();
stack.pop();
stack.push('a');
stack.push('b');
stack.push('c');
stack.push('d');
stack.push('e');
stack.printStack();
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
}
public boolean isEmpty() {
return top == -1;
}
public boolean push(char data) {
//Check if stack is full
//If it is full then we show Stack overflow message
if(isFull()) {
System.out.println("Stack overflow");
return false;
}
else {
stackArray[++top] =data;
return true;
}
}
public Character pop() {
// Check if stack is empty
//If it is empty then we show Stack underflow message
if(isEmpty()) {
System.out.println("Stack underflow");
return null;
}
else {
//decrementing top by one to make second element from top as top element
top --;
//we will be returning top+1 value from array as we have already made
//top to point second element in stack
return stackArray[top+1];
}
}
public Character peek() {
// we implement peek by first popping top element then pushing it back
Character element = null ;
if(!isEmpty()){
element = pop();
push(element);
}
return element;
}
// this method just gives view of the stack.
//As there are only two operation for stack i.e. push and pop
//so in theory we can never print a stack
//without calling those two methods
public void printStack() {
System.out.print("[");
for(int i = top; i>=0;i--){
System.out.print(" "+stackArray[i]);
}
System.out.println(" ]");
}
public boolean isFull() {
return top == CAPACITY -1;
}
}
If we run the program we will get output like this:
Output
================================================================================
Stack underflow
[ e d c b a ]
e
d
c
b
a
Stack underflow
null
Stack underflow
null
Please share your
comments if you find anything incorrect, or you want to share more
information about the topic discussed above.
For further reading:
practice questions on stack
Stack implementation using link list
Stack implementation by link list node,
Stack implementation using doubly link list
Stack implementation by doubly link list node .
For complete listing see data structure and algorithm page
No comments :
Post a Comment