Tuesday, November 1, 2016

Data Structures-STACK (continued)

Hello! Today I am going to give complete pseudocode of the stack array implementation.And at the end of the the page,you can find the java implementation of the stack array.

Complete pseudocode of the stack array implementation

class Stack{
                fields{
                        top < - 1;
                        array S;
                       CAPACITY <- n ;
                }
                isEmpty(){
                                if(top==-1)
                                                return TRUE;
                                else
                                                return FALSE;
                }
                push(x){
                                if(CAPACITY > size()){
                                                top <-top+1;
                                                S[top] <- x;
                                }else{
                                                print("stack is overflow");
                                }
                }
                size(){
                                return top;
                }
                pop(){
                                if (isEmpty()){
                                                print ("Stack is underflow");
                                }
                                else{
                                                temp <- S[top];
                                                S[top] <- NULL;
                                                top <- top-1;
                                                return temp;
                                }
                }
                top(){
                                if isEmpty()
                                                print ("NULL");
                                else
                                                return S[top];
                }
}


Java Implementation of Stack Array 
Note:My advise is try to write your own code snippets.After your creations ,then you can compare that with my implementation.This will be more interesting than reading my code snippet. Sometimes your code may better than me.

public class Stack {

    int CAPACITY = 4;
    int s[] = new int[CAPACITY];
    int top = -1;

    boolean isEmpty() {
        if (top == -1) {
            return true;
        } else {
            return false;
        }
    }

    void push(int x) {
        if (CAPACITY > top) {
            top = top + 1;
            s[top] = x;
        } else {
            System.out.println("Stack is overflow");
        }
    }

    int size() {
        if (isEmpty() == true) {
            return 0;
        } else {
            return top + 1;
        }

    }

    String pop() {
        if (isEmpty() == true) {
            return "Stack is Underflow";
        } else {
            int temp = s[top];
            //s[top]=null;
            top = top - 1;
            return Integer.toString(temp);
        }

    }

    String top() {
        if (isEmpty() == true) {
            return "NULL";

        } else {
            int temp = s[top];

            return Integer.toString(top);

        }

    }
}
BUT HOW WE KNOW THIS IS WORKING OR NOT???

Here is the solution.

Below is the demo class which include comments,"how we are going to test the above stack array implementation".

package datastructures;

/**
 *
 * @author hasitha
 */
public class StackDemo {
    public static void main(String[] args) {

         //Create a empty stack
         Stack s=new Stack();

        //Since there are no elements at the biginning stage,then the stack is currently underflow
        System.out.println(s.pop());

        //Check the initial size of the array stack
        System.out.println("Initial size :"+s.size());

        //Add 4 elements to the array stack
        s.push(10);
        s.push(15);
        s.push(4);
        s.push(5);
        //Now stack size should be 4
        System.out.println("After insrting 4 elements, size :"+s.size());

        //Call pop() method sth times,will remove and return elements in LIFO manner
        System.out.println("1st call for method pop() :"+s.pop());
        System.out.println("2st call for method pop() :"+s.pop());
        System.out.println("3 st callfor method pop() :"+s.pop());
        System.out.println("4 st call for method pop() :"+s.pop());

//Now corrently stack is empty.Again call for pop() method.This should print"stack is underflow"
        System.out.println("5 st call for method pop() :"+s.pop());

        //Check current status of the stack(empty or not?)
        System.out.println("call for isEmpty() :"+s.isEmpty());
    }
   
}
If there is any amendments, please drop me an email or comment below.