Module 2 DS
Module 2 DS
Module 2
Iliyaz Pasha M
Assistant Professor
De
1 partment of Computer Science, RLJIT
Outline
Stacks:
Definition
Stack Operations
Array Representation of Stacks
Stacks using Dynamic Arrays
Stack Applications: Polish notation
Infix to postfix conversion
Evaluation of postfix expression
Recursion:
Factorial
GCD
Fibonacci Sequence
Tower of Hanoi
Ackerman's function.
Queues:
Definition, Array Representation, Queue Operations
Circular Queues, Circular queues using Dynamic arrays
Dequeues
Priority Queues
A Mazing Problem Department of Computer Science, RLJIT 2
Multiple Stacks and Queues
Stacks
A stack is a linear data structure in which an element may be
inserted or deleted at only one end ( i.e called as the top of the
stack).
Stack is a data structure which works on the principle Last In
First Out (LIFO) i.e the Last element inserted will be the first
element to be deleted.
# define SIZE 5
int s[SIZE];
int top = -1;
if ( top == SIZE -1 )
{
printf(“ Stack is Full or Overflow”);
return;
}
if ( top == SIZE -1 )
{
printf(“ Stack is Full or Overflow”);
return;
}
if ( top == -1 )
{
printf(“ Stack is Empty or Underflow”);
return;
}
void pop ( )
{
if ( top == -1 )
{
printf(“ Stack is Empty or Underflow”);
return;
}
printf(“Deleted item =%d”, s[top--]);
}
void display ( )
{
int i;
if ( top == -1 )
{
printf(“ Stack is Empty or Underflow”);
return;
}
printf(“Contents of the stack are\n”);
for(i=top; i>=0; i--)
{
printf(“%d\t”, s[i]);
} Department of Computer Science, RLJIT 10
}
Write a C Program to implement stacks using arrays:
#include<stdio.h>
#define SIZE 5
int s[SIZE], top=-1;
void push ( )
{
int item;
if ( top == SIZE -1 )
{
printf(“ Stack is Full or Overflow”);
return;
}
printf(“ Enter an item \n”);
scanf(“ %d”, &item);
s[++top] = item;
}
void display ( )
{
int i;
if ( top == -1 )
{
printf(“Stack is empty or Underflow”);
return;
}
printf(“Contents of the stack are\n”);
for(i=top; i>=0; i--)
{
printf(“%d\t”, s[i]);
}
} Department of Computer Science, RLJIT 12
void main( )
{
int ch;
clrscr( );
for(;;)
{
printf(“ 1. Push \n 2. Pop \n 3. Display\n 4. Exit\n”);
printf(“Enter your Choice\n”);
scanf(“%d”,&ch);
switch(ch)
{
case 1: push( );
break;
case 2: pop( );
break;
case 3: display( );
break;
default: printf(“Invalid Choice\n”);
exit( 0);
}
} Department of Computer Science, RLJIT 13
}
Stacks using Dynamic Arrays
We can allocate or create a stack dynamically during run time.
#include <stdio.h>
#include <stdlib.h>
int size;
int top=-1;
a/b–c+d*e–a*c
Example: A + B
Prefix or Polish Expression: In this expression, the operator appears before its
operand.
Example: + A B
Example: A B +
Department of Computer Science, RLJIT
Here, A & B are operands and + is operator 20
Stack Applications: Polish notation
Once control comes out of the above loop, if the precedence of the symbol on top
of the stack is not equal to the precedence of the current input symbol, push the
current symbol on to the stack. Otherwise, delete an item from the stack.
if ( F (s[top]) != G (symbol) )
s[++top] == symbol;
else
top--;
Step 4:Repeat the above procedure till the end of the input
Department of Computer Science, RLJIT
expression. 33
Evaluation of postfix expression
for( i=0; i < strlen(postfix); i++)
{
symbol = postfix[i];
if(isdigit(symbol))
s[++top] = symbol;
else
{
op2=s[top--];
op1=s[top--];
void main ( )
{
int n;
printf(“EnteraNumber\n”);
scanf(“%d”, &n);
printf(“Factorial = %d”, fact(n));
getch();
} Department of Computer Science, RLJIT 43
2. Fibonacci Sequence:-
The Fibonacci sequence is as follows:
0, 1,1, 2, 3, 5, 8, 13, 21, 34, 55, ………
i.e., F0=0 and F1=1, and each succeeding term is the sum of the two preceding terms
Fibonacci Sequence can be defined as:
a) If n=0, or n=1, then Fn=n
b) If n>1, then Fn= Fn-1 + Fn-2
#include<stdio.h>
#include<conio.h>
int fib (int n)
{
if (n == 0)
return 0;
if (n == 1)
return 1;
else
return (fib(n-1) + fib(n-2));
}
void main ( )
{
int n;
printf(“EnteraNumber\n”);
scanf(“%d”, &n); Department of Computer Science, RLJIT 44
printf(“Fibonacci = %d”, fib(n));
3. Find GCD of 2 Numbers:-
#include<stdio.h>
#include<conio.h>
int gcd (int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a>b)
return gcd(a%b, b);
else
return gcd(a, b%a);
}
void main ( )
{
int m,n;
printf(“Enter the values of m, n\”);
scanf(“%d%d”, &m,&n);
printf(“GCD of 2 Numbers is %d”, gcd(m,n));
getch(); Department of Computer Science, RLJIT 45
}
4. Tower of Hanoi:- Recursion may be used as a tool in solving the problem of
Tower of Hanoi.
Problem description:
Suppose three pegs (poles), labelled A, Band C, are given, and suppose on peg A
finite number n of disks with decreasing size are placed.
The objective of the game is to move the disks from peg A to peg C using peg B
as an auxiliary.
When n > 1, the solution may be reduced to the solution of the following
three sub- problems:
(a) TOWER (N - I, BEG, END, AUX)
(b) TOWER (l, BEG, AUX, END) or BEG → END
(c) TOWER (N - I, AUX, BEG, END)
Department of Computer Science, RLJIT 48
Procedure: TOWER (N, BEG, AUX, END)
This procedure gives a recursive solution to the Towers of Hanoi
problem for N disks.
1. If N=l, then:
(a) Write: BEG → END.
(b) Return.
[End of If structure.]
void display ()
{
if (front > rear)
{
printf(“Queue is Underflow\n”);
return ;
}
printf(“Contents of Queue are:\n”);
for( i = front; i <= rear; i++)
{
printf(“%d\n”, q[i]);
}
}
void insert ()
{
if ( rear == QUEUE_SIZE - 1)
{
printf("Queue Overflow\n");
return ;
}
printf("Enter the item\n");
scanf("%d",&item);
q[++rear] = item;
}
In the above situation, even through space is available at the front end
we can’t insert the elements. i.e., rear insertion is denied.
How means before inserting an element will check one condition is
rear==QUEUE_SIZE-1. if this condition is satisfied it indicates
queue is full.
This disadvantage can be overcome by using 2 methods
1. Shift left
2. Circular Queue or Circular Representation
Department of Computer Science, RLJIT 63
Circular Queue :-
In circular queue, the elements of a given queue can be stored
efficiently in an array so as to “wrap around” so that end of the
queue is followed by the front of queue.
Before insertion the value of rear is calculated by
rear = (rear + 1) % QUEUE_SIZE
After deletion the value of front is calculated by
front = (front + 1) % QUEUE_SIZE
To check for overflow and underflow, global variable count is
maintained.
If count = = QUEUE_SIZE then Queue is Full or Overflow
If count = = 0 then Queue is empty or Underflow.
void insert ()
{
if ( count == QUEUE_SIZE )
{
printf("Queue is Overflow or Queue is Full\n");
return ;
}
printf("Enter the item\n");
scanf("%d",&item);
rear = (rear + 1)%QUEUE_SIZE;
q[rear] = item;
count++;
Department of Computer Science, RLJIT 65
}
void del ()
{
if (count == 0)
{
printf("Queue is Underflow or Queue is Empty\n");
return ;
}
printf("Item deleted = %d\n",q[front]);
front = (front + 1)%QUEUE_SIZE;
count--;
}
void insert( )
{
if ( count == SIZE )
{
printf(“ Q Full: Update size, insert item\n”);
SIZE++;
q = (int *) realloc(q, SIZE*sizef(int));