Java Data Structure Implementation : Stack using Array

This program demonstrates the Stack implementation using Array in Java. The Push, Pop, Print Elements Operation has been implemented.

Find the Java Code below :

import java.util.Scanner;

/*
* This class demonstrates Stack Implementation using Array in java with Push/Pop/Print Operations.
* @author : Madan Gopal Singh
* @version : 2.0
* @date : 11th April, 2014
*/

public class StackUsingArray {

public static Object[] stackElements;
public static int top = -1;
public static int STACK_SIZE = 5;

/**
* This method push the element from the stack.
* @param void.
* @return void.
*/
public static void push(Object insertElement){
if(top==STACK_SIZE-1){
System.out.println("Stack Overflow");
}else{
top=top+1;
stackElements[top] = insertElement;
Object insertedElement = stackElements[top];
System.out.println("Top of Stack : "+top);
System.out.println(insertedElement.toString()+" inserted at top of stack");
}
}

/**
* This method pop the element from the stack.
* @param void.
* @return void.
*/
public static Object pop(){
if(top==-1){
System.out.println("Stack Underflow");
return null;
}else{
Object deletedElement = stackElements[top];
System.out.println(deletedElement.toString()+" deleted from top of stack");
top = top-1;
return deletedElement;
}
}

/**
* This method print all elements of the stack.
* @param void.
* @return void.
*/
public static void printAllElements(){
System.out.println("Printing element from top "+top);
if(top == -1){
System.out.println("Stack has no element");
}else{
int i = top;
while(i>=0){
System.out.println("Element at position "+ i + "is :- "+stackElements[i].toString());
i = i-1;
}
}
}

/**
* This method to check whether element already exists or not.
* @param Object.
* @return boolean.
*/
public static boolean isElementExists(Object obj){
if(top==-1){
return false;
}else{
int i = top;
while(i>=0){
if(stackElements[i].toString().equalsIgnoreCase((String)obj.toString())){
return true;
}
i = i-1;
}
return false;
}
}

/**
* This method to check whether stack is empty or not.
* @return boolean.
*/
public static boolean isEmpty(){
if(top==-1){
return true;
}else{
return false;
}
}

/**
* Main Method
* @param args command line argument list
* @return void.
*/
public static void main(String[] args) {
stackElements = new Object[STACK_SIZE];
int userOperation=0;
String mainMenuString = "";
Scanner sc = new Scanner(System.in);
do{
mainMenuString = "What operation you want to perform?"+
"\n 1. Push Element "+
"\n 2. Pop Element "+
"\n 3. Print All Elements "+
"\n 4. Cheque Element Exists Or Not "+
"\n -1. Exit from Menu"+
"\n Please enter Operation Number :";
System.out.println(mainMenuString);
userOperation = sc.nextInt();
switch (userOperation) {
case 1:
System.out.println("Please enter element to push into stack?");
String pushElement = sc.next();
StackUsingArray.push(pushElement);
break;
case 2:
StackUsingArray.pop();
break;
case 3:
StackUsingArray.printAllElements();
break;
case 4:
System.out.println("Please enter element to check Existency?");
boolean isExists = StackUsingArray.isElementExists(sc.next());
if(isExists){
System.out.println("Exists");
}else{
System.out.println("Not Exists");
}
break;
case -1:
System.out.println("Exit Application");
System.exit(0);
default:
System.out.println("Please enter a valid operation number");
}
}while(userOperation!=-1);

}

}

Any suggestion is invited if any.

Advertisements

One thought on “Java Data Structure Implementation : Stack using Array

  1. Pingback: Java Data Structure Implementation : Undirected Simple Graph (BFS/DFS) | Madan Gopal Singh Shekhawat

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s