1.INTRODUCTION
A stack is a linear data structure. It is very useful in many applications of computer science. It is a list in which all insertions and deletions are made at one end, called the top of the stack. Some of the analogies used to visualize this data structure are
a) Stack of Trays (or) Plates placed on a Table:- Here, one plate is placed on top of another, thus creating a stack of plates. Suppose, a person takes a plate off the top of the stack of plates. The plate mostly recently placed on the stack is the first one to be taken off. The bottom plate is the first one placed on the stack and the last one to be removed.
b) Shipment in a Cargo:- For the shipment of goods, they have to be loaded into a cargo. During unloading, they are unloaded exactly in the opposite order as they are loaded, that is, the goods which are loaded last should be unloaded first.
1.2 DEFINITION
A stack is an ordered collection of homogeneous data elements where the insertion and deletion operations occur at one end only, called the top of the stack. Other names for a stack are pushdown list, and LIFO (or) Last in First Out list. The stack allows access to only one item, i.e., the last item inserted.
Fig: Schematic diagram of a stack
1.3 Primitive Operations
The primitive operations that can be performed on a stack are given below:
1. Inserting an element into the stack (PUSH operation)
2. Removing an element from the stack (POP operation)
3. Determining the top item of a stack without removing it from the stack (PEEP operation)
The syntax used for the PUSH operation is--
PUSH (stack, item)
where stack is the name of the stack into which the item specified as the second argument is placed at the top position of the stack.
The syntax used for the POP operation is--
POP (stack)
where stack is the name of the stack from which the item has to be removed, i.e., the element at the topmost position of the stack is removed.
1.4 Application of Stack :
Parsing
Recursive Function
Calling Function
Expression Evaluation
Expression Conversion
Infix to Postfix
Infix to Prefix
Postfix to Infix
Prefix to Infix
Towers of hanoi
1.5 AN ABSTRACT DATA TYPE (ADT)
An Abstract Data Type (ADT) is a keyword used in a programming language to specify the amount of memory needed to store data and the type of data that will be stored in that memory location.
ADT for Stack This is a Last-In First-Out structure. Items can only be inserted and removed from the top of the stack. The ADT for stack are the following:
1) Push() (or) Put (or) insert new item on top of stack
2) Pop() remove and return top item of stack
3) Top() Verifies the top item of stack
1.6 IMPLEMENTATION
A stack can be implemented in any one of the following two ways:
1. Using an array.
2. Using a linked list.
1.6.1 Array Implementation of Stack:- In the array implementation of a stack, a stack can grow either towards the high-indexed end of the array (or) towards the low-indexed end of the array.
The array implementation requires the following:
1) An array.
2) A variable top to hold the topmost element of the stack.
/*Array implementation of a stack */
#include<stdio.h>
#include<conio.h>
#define SIZE 10
void push(int);
void pop();
void display();
int stack[SIZE], top = -1;
void main()
{
int value, choice;
clrscr();
while(1)
{
printf("\n\n***** MENU *****\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the value to be insert: ");
scanf("%d",&value);
push(value);
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong selection!!! Try again!!!");
}
}
}
void push(int value)
{
if(top == SIZE-1)
{
printf("\nStack is Full!!! Insertion is not possible!!!");
}
else
{
top++;
stack[top] = value;
printf("\nInsertion success!!!");
}
}
void pop()
{
if(top == -1)
{
printf("\nStack is Empty!!! Deletion is not possible!!!");
}
else
{
printf("\nDeleted : %d", stack[top]);
top--;
}
}
void display()
{
if(top == -1)
{
printf("\nStack is Empty!!!");
}
else
{
int i;
printf("\nStack elements are:\n");
for(i=top; i>=0; i--)
{
printf("%d\n",stack[i]);
}
}
}
1.6.2 Linked List Implementation of a Stack
In the case of linked-list implementation of a stack, the first element inserted into the stack is pointed out by the second element, the second element by the third, and so on. In general the (n-1)th element is pointed out by the nth element. The pointer address corresponding to the nth element has to be maintained. The linked-list implementation of a stack is as follows.
The linked-list implementation of a stack requires the following:
1. Structure declaration containing some n fields.
2. Pointer to the first node (or) the last node.
/*Linked List implementation of a stack */ Go Through This Video:-
https://www.youtube.com/watch?v=tlAKGqwXZuc