Tuesday 5 July 2016

Here are some real life applications of the stack

Stack is an abstract data type and a data structure that follows LIFO (last in first out) strategy. It means the element added last will be removed first. Stack allows two operations push and pop. Push adds an element at the top of the stack and pop removes an element from top of the stack.
Below I have mentioned few applications of stack data structure.

Applications of Stack

Expression Evaluation
Stack is used to evaluate prefix, postfix and infix expressions.

Expression Conversion
An expression can be represented in prefix, postfix or infix notation. Stack can be used to convert one form of expression to another.

Syntax Parsing
Many compilers use a stack for parsing the syntax of expressions, program blocks etc. before translating into low level code.

Backtracking
Suppose we are finding a path for solving maze problem. We choose a path and after following it we realize that it is wrong. Now we need to go back to the beginning of the path to start with new path. This can be done with the help of stack.

Parenthesis Checking
Stack is used to check the proper opening and closing of parenthesis.
String Reversal
Stack is used to reverse a string. We push the characters of string one by one into stack and then pop character from stack.

Function Call
Stack is used to keep information about the active functions or subroutines.
Take your time comment on this article.

What is the difference between stack and queue


Stack
A Stack data structure works on Last In First Out principle, also abbreviated as LIFO. A Stack requires only one reference variable, known as a top pointer. Stack data structures are normally represented as a vertical representation of data items.
A Stack Data Structure can be represented in static arrays as well as linked lists. In a stack, the data items can be inserted and deleted from the same end. The element to be inserted first is the last element to get deleted. Both the operations, insertion and deletion occur at the TOP reference pointer. A Stack doesn’t necessarily contain ordered collection of data items.
Stack Implementations:
Stack data structure is used in infix to postfix conversion, scheduling algorithms, evaluation of an expression and depth first search.
The condition to check if a stack is empty:
int isEmpty()
{
    if(top==-1)
        return 1;
    else
        return 0;
}

The condition to check if a stack is full:
int isFull()
{
    if(top==MAX-1)
        return 1;
    else
        return 0;
}
Queue
A Queue data structure works on First In First Out principle, also abbreviated as FIFO. A Queue requires two reference variables. Queue is normally represented as a horizontal representation of data items. The Queue data structure contains FRONT and REAR as its reference points for data processing.
A Queue can also be represented in by static arrays as well as linked lists. In a Queue, the data items can be inserted and deleted from different ends. The element to be inserted first is the first element to get deleted. The insertion operation is done at rear end whereas the deletion occurs at the front end. A Queue contains ordered collection of data items. A Queue can be divided into sub sections and it has the following extensions: Double Ended Queue, Simple Queue, Priority Queue and Circle Queue.
Queue Implementations:
A Queue offers services in operations research, transportation and computer science that involves persons, data, events and objects to be stored for later processing.
The condition to check if a queue is empty:
int isEmpty()
{
    if(front==-1 || front==rear+1)
        return 1;
    else
        return 0;
}
The condition to check if a queue is full:
int isFull()
{
    if(rear==MAX-1)
        return 1;
    else
        return 0;
}
Here is the image representing the stack and queue
Difference-Between-Stack-and-Queue

Difference Between Stack and Queue

StackQueue
A Stack Data Structure works on Last In First Out (LIFO) principle.A Queue Data Structure works on First In First Out (FIFO) principle.
A Stack requires only one reference pointer.A Queue requires two reference pointers.
A Stack is primarily a vertical representation of data items.A Queue is a horizontal representation of data items.
A Stack contains TOP as its reference for data processing.A Queue contains REAR and FRONT as its reference for data processing.
The data items are inserted and deleted from the same end.The data items in a queue are inserted and deleted from different ends.
The element to be inserted first is removed last.The element to be inserted first is removed first.
To check if a stack is empty, following condition is used:TOP == -1To check if a queue is empty, following condition is used:FRONT == -1 || FRONT == REAR + 1
The insertion and deletion operations occur at the TOP end of a Stack.The insertion operation occurs at REAR end and the deletion operation occurs at FRONT end.
A Stack data structure is not necessarily an ordered collection of data items.A Queue data structure is an ordered collection of data.
To check if a stack is full, following condition is used:TOP == MAX – 1To check if a queue is full, following condition is used:REAR == MAX – 1
A Stack cannot be divided into sub sections and it doesn’t have extensions.A Queue can be divided into sub sections and it has the following extensions: Double Ended Queue, Simple Queue, Priority Queue and Circle Queue.
Used in infix to postfix conversion, scheduling algorithms, depth first search and evaluation of an expression.A Queue offers services in operations research, transportation and computer science that involves persons, data, events and objects to be stored for later processing.

Take your time to comment on this article.