Pitch your project

Random text






Blogs

0    
By admin Category Uncategorized, Posted March 21st, 2016
Stacks and Queues in Java
  • What is a Stack?
  • What is a Queues?
  • What is the difference between Stacks and Queues in Java?

What is a stack?

A stack is a collection or a container of objects that are inserted and removed using LAST IN FIRST OUT ORDER(LIFO), where addition and removal takes place only at one end known as the top.

Stacks operations are:
push: adds an item on the top
pop: removes the item at the top.
peek: returns the item at the top without removing it.
size: returns the number of items in the stack.
isEmpty: returns in case the stack has no items.

A real life example of a stack uses the slogan of last hired, First fired for example when you arrange a set of books on a table,to get a book in the middle of the pile you have to first have to remove all the books on top of the one your supposed to get, that is you first have to remove the books place last to get the books that are required first.

A Classic Example of how to use Stacks in Java

/*
Stack collection implements Last-In Fist-Out behavior on items stored within it.
*/
 int[] stack ;

    public StackMethods(int arraySize){
        size=arraySize;
        stack= new int[size];
        top=-1;
    }

    public void push(int value){
        if(top==size-1){
            System.out.println("Stack is full, can't push a value");
        }
        else{

            top=top+1;
            stack[top]=value;
        }
    }

    public void pop(){
        if(!isEmpty())
            top=top-1;
        else{
            System.out.println("Can't pop...stack is empty");
        }
    }

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

    public void display(){

        for(int i=0;i< =top;i++){
            System.out.print(stack[i]+ " ");
        }
        System.out.println();
    }

What is a Queue?

A queue is a container of objects that are inserted and removed using the FIRST IN FIRST OUT ORDER(FIFO).
The only similarity queues have with stacks is the fact that queues consist of a sequence of items, and there is a restriction in the way the items are added and removed.
A real world example is that of a web application that is used to place an order for a particular customer. As part of placing that order (and storing it in a database) you may wish to carry a number of additional tasks:

A Classic Example of how to use a Queue in java

/*
Queue collection implements First-In Fist-Out behavior on items stored within it.
*/
class Queue
{
    int Q[]; // Array to implement Queue
    int size; // Maximum size of the Queue
    int front; // Index of front element
    int rear; // Index of rear element

    Queue(int cap) // Parameterised Constructor
    {
        size = cap;
        Q = new int[size];
        front = 0;
        rear = 0;
    }

    void insert(int v) // Function to insert element in Queue
    {
        if(rear == size) // Condition for Overflow
        {
            System.out.println("OVERFLOW");
        }
        else
        {
            Q[rear] = v; // Storing value in Queue
            rear = rear + 1;
        }
    }

    int delete() // Function to delete element from Queue
    {
        if(front == 0 && rear == 0) // Condition for Underflow
        {
            System.out.println("UNDERFLOW");
            return -999;
        }
        else
        {
            int val = Q[front]; // Storing the element which will be removed
            front = front + 1;
            if(front == rear) // Condition for emptying the Queue
            {
                front = 0;
                rear = 0;
            }
            return val;
        }
    }

    void display() // Function for printing elements in the queue
    {
        if(front == 0 && rear == 0)
        {
            System.out.println("The Queue is empty");
        }
        else
        {
            System.out.println("The elements in the queue are : ");
            for(int i=front; i<rear; i++)
            {
                System.out.println(Q[i]);
            }
        }
    }
}

Difference between a stack and a queue?

  1. Stack is defined as a list of element in which we can insert or delete elements only at the top of the stack, while a Queue is a collection of the same type of element. It is a linear list in which insertions can take place at one end of the list,called rear of the list, and deletions can take place only at other end, called the front of the list

  2. The behaviour of a stack is like a Last-In First-Out(LIFO) system while The behaviour of a queue is like a First-In-First-Out (FIFO) system.

Posted by Rufus Nganga

Leave a Reply

Your email address will not be published. Required fields are marked *