Tuesday, March 10, 2015

Stack of Strings Implemented in Java

In my previous post we talked about stack implementation using character array.

In this post I will be sharing my code for a stack implementation which can hold String values in other words stack implemented using array of String in java.


Sections in this post: 
  1. String Array Based Implementation of stack.
 Let see the code directly as it is self explanatory

public class StackOfStrings {
 // 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 String[] stackArray = new String[CAPACITY];

 public static void main(String[] args) {
  StackOfStrings stack = new StackOfStrings();
  stack.pop();
  stack.push("aa");
  stack.push("bb");
  stack.push("cc");
  stack.push("dd");
  stack.push("ee");
  stack.printStack();
  System.out.println("=============================");
  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(String 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 String pop() {
  
   // Check if stack is empty
   //  If it is empty then we show Stack underflow message
   
  if (isEmpty()) {
   System.out.println("Stack underflow");
  } else {
   // decrementing top by one to sake second element from top as top
   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];
  }
  return null;
 }

 public String peek() {
  // we implement peek by first popping top element then pushing it back
  String element = null;
  if (!isEmpty()) {
   element = pop();
   push(element);
  }
  return element;
 } 

 public boolean isFull() {
  return top == CAPACITY - 1;
 }

 
  // 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(" ]");
 } 
}
 If we run the program we will get output like this:
Output
================================================================================
Stack underflow
[    ee    dd    cc    bb    aa    ]
=============================
ee
dd
cc
bb
aa
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