Data Structure

Stack Data Structure

Stack Data Structure

Stack Data Structure plays a very important role because of it’s real world applications. A computer science student should know about stack, various operations of stack and it’s application.

Stack is also an important topic for GATE(CS/IT) and UGC NET exam. Questions on Stack are asked in GATE and UGC NET exam every year. We explained Stack Data Structure and it’s application.

stack data structure

Frequently Asked Questions

Some frequently asked questions on Stack are listed here.

  • What is Stack ?
  • What is difference between Stack and Queue ?
  • What are Push and Pop operations on Stack ?
  • What do you mean by Top of Stack ?
  • Write steps of Push Operation ?
  • How to delete an element from Stack ?
  • What are different applications of Stack ?

Students can easily answer all these questions after reading this tutorial.

What is Stack Data Structure ?

Stack in Data Structure is an abstract data type.

  • Stack a linear data structure that allows inserting and deleting elements in a specific manner.
  • When we want to insert an element in stack it can be added at the top of the stack.
  • All the insertion and deletion is performed at this TOP in stack in data structure.  
  • It use First in last out(FILO) process or Last in First Out ( LIFO)
  • Suppose if S={S0,S1,S2……Sn-1}

Here S0 is a bottom element and stack has n element.

This Top is a variable which contain position of the newly inserted element.

Various Operations on Stack in Data Structure

Various operations on Stack in Data Structure performed on stack are as follow –

Push and Pop Operations on Stack

  • The insertion of an element into stack is known as PUSH operation.
  • The deletion of element from stack is known as POP operation in stack.
  • Representation of Stack in Data Structure is as as shown in following figure.
applications of stack

We can access any element in array by its index but for a stack only the top element can be assessed

Algorithm for Push Operation

Steps to add an item in stack data structure are as follows:

  1. At first we check if the stack is full or not.
  2. If the stack is full, then in this situation print error of overflow and exit the program.
  3. If the stack is not full, then at first we increment the top value by one and add the element at this new position ( new value of TOP variable).

Code for Push Operation in Stack

Code for Push operation on Stack is given below –

Void push(s,N,Top,x) /*S-name of stack N-size of stack*/

{
if(top==N-1)
printf(“stack is overfull”)
exit();
}

else
{
top=top+1;
s[top]=x;
}

Algorithm for POP Operation

Steps to remove an item from stack data structure are as follows:

  1. At first we check if the stack is empty or not.
  2. If the stack is empty, then in this situation we print error of underflow and exit the program.
  3. If the stack is not empty, then print the element at the top and then we have to decrement the top by one. This new value of Top variable is Top at this time.

Code for POP Operation on Stack

Code for pop operation on stack is given below-

int pop(S,N,Top)
/*s- name of stack
N-size of stack*/
{
int y;
if(top== -1)
{
printf(“stack is underflow”);
exit(1);
}
else
{
y=s[top]
top= top – 1;
return(y)
}
}

Application of Stack in Data Structure

Various application of stack in Data Structure are as follows –

Parsing

Stacks are used by compilers to check the syntax of the program and for generating executable code.

Function Calls

When a function is called all local storage for the function is allocated on the system “stack” ,and the return address(within the calling function) is also pushed on the system stack.

Implementing Recursion

Stack can be used to implement recursion if the programming language does not provide the facility for the same.

Reversing a List

We can also use stack for reversing a list.

Some other applications of stack are given below

  • Real life
    • Pile of books
    • Plate trays
  • More applications related to computer science
    • Program execution stack (read more from your text)
    • Evaluating expressions
  • Recursion
  • Reverse polish notation(infix to postfix)
  • Postfix evolution
  • Prefix to postfix
  • Fibinocci Series
  • Tower of hanoi

Difference between Stack and Queue

  • Major difference between Stack and Queue is that Stack is a Last in First Out data Structure where as Queue is a First in First Out Data Structure.
  • In Queue There are Two ends Front and Rear where as in Stack there is only one end Top of the Stack.
  • In queue Insertion is performed at Rear end and deletion is performed at Front end where as in case of Stack insertion and deletion both performed at the Top of Stack.

Conclusion and Summary

Stack is a very useful data structure. Local Variable in a function is also store in stack. In this tutorial we studied about basic concepts of stack in data structure, stack application and use , we also learn about push and pop operations in stack.

If you have any query then ask in comment section.

Previous Tutorial – Array Data Structure

Next Tutorial – Binary Tree and It’s Types

 

Leave a Reply

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