Stack

Stack is a way of buffering a set of elements, in which the element that enters into the
stack last, is the element that comes out first (LIFO). A stack may be considered as a one-
end barrel to which new elements may be inserted and from which elements may be
deleted, only at one end.

Stack can be described in the following way:
· Stack follows the principle of LIFO (Last-In-First-Out), last element added to the stack is
the first one removed.
· Alternatively, the principle may also be termed as FILO (First-In-Last-Out), first element
added to the stack is the last one to be removed.
· Elements are accessible only from one end of the list.
· Elements are added or deleted from the list only from the top
· The process of inserting an element at the top, is called, PUSHing.
· The process of deleting an element from the top, is called, POPing.
· Push operation adds an element to the top of a non full stack.
· Pushing an element into a full stack leads to OVERFLOW condition.
· Pop operation removes an element from the non empty stack.
· Popping an element from an empty stack leads to UNDERFLOW condition.
Operations on stacks
isEmpty(): It returns TRUE, if the stack is empty, otherwise, it returns FALSE. This
operation is used internally by the Pop() operation before popping an element
from the stack.
isFull(): It returns TRUE, if the stack is full, otherwise, it returns FALSE. This operation
is used internally by the Push() operation before pushing an element onto the
stack.
Push(): It adds an element onto the top of the stack. If the stack is full, this operation
becomes unsuccessful.
Pop(): It deletes an element from the top of the stack. If the stack is empty, this
operation becomes unsuccessful.
Length(): It returns the number of elements present in the stack. For an empty stack, it
returns 0.
Peep(): It returns the top element of the stack. Stack remains unchanged after this
operation. If the stack is empty, this operation becomes unsuccessful.
This operation is also known as Peek().
Locate(): It locates the desired element in the stack and returns its index / position. If
the element is not present, then it returns an invalid index / position.
Print(): It prints the elements of the stack on the screen / monitor.

There are different ways of implementing a stack. One such implementation needs an array
(for storing values) and an indexing variable, say top, to access the elements of the stack.
The top variable is managed such that, it always points to the top most element of the
stack.






















Few applications of stack

· A palindrome is a string that reads the same forward and backward. dad, sees,
madam, malayalam are few examples of a palindrome. With the help of the stack, it
can be very easily checked, whether a string forms a palindrome.
· An infix expression requires multiple scans for its processing, which takes lot of
processing time. If it is converted into an equivalent postfix expression, in one scan






Pop()

top=1



0


1


2


3


4






Pop()

top=0



the processing can be completed. The infix expression can be converted into an
equivalent postfix expression using the stack.
· Stack is used to evaluate postfix expressions.
· Compilers use stack to process programs involving recursive procedures.

Program

#include <stdio.h>

#define SIZE 10
#define TRUE 1
#define FALSE 0

int stack [SIZE];
int top = -1;

int empty()
{
if (top <= -1)
return(1);
else
return(0);
}

int full()
{
if (top >= SIZE-1)
return(1);
else
return(0);
}

void push(int key)
{
if (full())
printf("Stack is full. Overflow.\n");
else
{
top++;
stack[top] = key;
}
}

int pop()
{
int temp;

if (empty())
{
printf("Stack is empty. Underflow.\n");
return(-32768);
}
else
{
temp = stack[top];
top--;
return(temp);
}
}

int peep()
{
if (empty())
{
printf("Stack is empty.Cannot peep.\n");
return(-32768);
}
else
return(stack[top]);
}

void print()
{
int i;

if (empty())
printf("Stack is empty.\n");
else
{
printf("Stack has the following elements :\n");
for(i = top; i >= 0; i--)
printf("%d\t",stack[i]);
printf("\n");
}
}

int length()
{
int count = 0,temp;
if (!empty())
{
temp = top;
while(temp >= 0)
{
count++;
temp--;
}
}
return(count);
}

int locate(int key)
{
int temp = top;
while(temp >=0)
{
if (stack[temp] == key)
return(temp);
temp--;
}
return(-1);
}

void menu()
{
printf("Following operations are allowed in the Stack : \n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Length\n");
printf("4. Printing\n");
printf("5. Peep\n");
printf("6. Locate\n");
printf("7. Exit\n");
printf("Enter your choice : ");
}

void main()
{
int choice,ele,num;
do
{
menu();
scanf("%d",&choice);
switch(choice)
{

case 1 : printf("Enter the element to be pushed : ");
scanf("%d", &num);
push(num);
break;

case 2 : num = pop();
if (num != -32768)
printf("The popped element is %d.\n",num);
break;

case 3 : printf("The length of the stack is %d\n",length());
break;

case 4 : print(); break;

case 5 : num = peep();
if ( num != -32768)
printf("The top element is %d.\n",num);
break;

case 6 : printf("Enter the element to be located : ");
scanf("%d",&num);
ele = locate(num);
if (ele == -1)
printf("%d is not found in the stack.\n", num);
else
printf("%d is found at %d position.",num,ele+1);
break;

case 7 : printf("Program terminates.\n");
break;

default : printf("Enter the correct choice. Try again.\n");
} /* end of switch */

} while(choice != 7);
printf(" Press any key to continue : "); getch();
} /* end of the program. */

Exercises

· Implement the stack program without using any of the global variables.
· Implement the stack program using a structure, such that the array and the top
variable are the fields of the same structure.
· Using the stack, check whether a given arithmetic expressions has sufficient number
of parenthesis?