You are on page 1of 56

www.davandvg.

com

Spurthi Educational Trust ®


Davan Institute of Advanced Management Studies
Nutana Institute of Management Studies
13th Main, MCC 'B' Block, Davangere-04: Pho: 08192-225550, 7026-225550
Instructions To The Students
01 Every one have to keep your video ON through out the session.

02 Mute your Audio. Unmute only when you need to answer.

03 Any kind of misbehaviour is not tolerated and strict action will be


taken .
2

04 These topics will not be repeated in the offline classes.

05 Attendance will be automatically taken by the


system. And the students whose video will be ON
considered as present. g. c om
a v andv
www.d
www.davandvg.com

Welcome!!
Hello!

a B S
et h
m S hw
I a

3
Data Structures Using C

g. c om
a v andv
www.d
06/07/2021
17/5/2021 www.davandvg.com

Topic Of The Day

Queue and recursion

Shwetha BS Data Structure www.davandvg.com 5


17/5/2021
06/07/2021
Quotations

6
Data Structure
Shwetha BS
Today’s topic

Unit 3. Queue and Recursion

1. Definition , types of Queue


2. Simple , Circular , Double ended and Priority queue
3. Array implementation of queue
4. Operations on all types of queues 7

5. Recursion: Definition
6. Writing recursive programs: Binomial coefficient
7. Fibonacci, GCD, towers of Hanoi

g. c om
a v andv
www.d
06/07/2021
15/06/2021
03-06-2021
Queue
What is a queue?
Definition: A queue is a special type of data
structure (an ordered collection of items) where
elements are inserted from one end and elements are
deleted from the other end. The end at which new
elements are enqueue (insertion))is called the rear
end and the end from which elements are dequeue
(deletion) is called the front end. Using this
approach, the First element Inserted is the First
element to be deleted Out, and hence, queue is also
called First In First Out (FIFO) data structure.
Shwetha BS Data Structure www.davandvg.com 8
06/07/2021
03-06-2021

Since a queue elements are stored in linear order, it can be implemented using
arrays and linked lists. Based on the method of insertion and deletion the queues are
classified as shown below:

Linear queue(ordinary queue)

Circular queue
Types of
queues
Double ended queue ( dequeue)

Priority queue

Shwetha BS Data Structure www.davandvg.com 9


03-06-2021

Shwetha BS Data Structure www.davandvg.com 10


06/07/2021
03-06-2021
Linear queue(ordinary queue)

Insert

Queue Delete
operation

Display

Shwetha BS Data Structure www.davandvg.com 11


06/07/2021
03-06-2021
Insert into queue
In a queue, an item is always inserted at the rear end

Shwetha BS Data Structure www.davandvg.com 12


06/07/2021
03-06-2021

void qinsert()
{
if (rear == QUEUE_SIZE-1)
{
printf(“Queue overflow\n");
return;
Function to insert an item into the }
queue rear = rear + 1;
q[rear] = item;
}

Shwetha BS Data Structure www.davandvg.com 13


06/07/2021
03-06-2021 Delete from queue
In a queue, an item is always removed from the front end.

 Queue is empty or not

10 20 30 30
0 1 2 3 4 0 1 2 3 4
Front rear Front
rear
front < rear front = rear

Shwetha BS Data Structure www.davandvg.com 14


06/07/2021
03-06-2021 Display queue items

Shwetha BS Data Structure www.davandvg.com 15


06/07/2021
03-06-2021

Function to delete an item from the Function to display queue elements


queue
void display()
{
void delete() int i;
{ if (front> rear)
if(front>rear) {
{ printf(“Queue is empty\n");
printf(“queue underflow\n”); return;
return; }
} printf("Contents of the queue\n");
printf(“the element deleted is %d\n”,q[front]); for (i = front;i <= rear;i++)
qfront=qfront+1; {
} printf("%d\n", q[i]);}
}}
Shwetha BS Data Structure www.davandvg.com 16
06/07/2021
03-06-2021

Shwetha BS Data Structure www.davandvg.com 17


08/07/2021
03-06-2021

Shwetha BS Data Structure www.davandvg.com 18


08/07/2021
03-06-2021
Circular queue
 Circular Queue is a linear data structure in which the
operations are performed based on FIFO (First In First
Out) principle and the last position is connected back to
the first position to make a circle.
 In circular queue, the elements of a given queue can be
stored efficiently in an array so as to "wrap around" so
that rear end of the queue is followed by the front of
queue.
 Graphical representation of a circular queue is as
follows...
Shwetha BS Data Structure www.davandvg.com 19
08/07/2021
03-06-2021

Shwetha BS Data Structure www.davandvg.com 20


08/07/2021
03-06-2021

Shwetha BS Data Structure www.davandvg.com 21


08/07/2021
03-06-2021

Shwetha BS Data Structure www.davandvg.com 22


08/07/2021
03-06-2021

Shwetha BS Data Structure www.davandvg.com 23


08/07/2021
Functionss
03-06-2021

Shwetha BS Data Structure www.davandvg.com 24


09/07/2021
03-06-2021
Double ended queue ( dequeue)
A Dequeue is a special type of data structure in which insertions are done from
both ends and deletions are done at both ends.

Insert an item from front end

Insert an item from rear end


operations

Delete an item from front end

Delete an item from rear end

Display the content of queue


Shwetha BS Data Structure www.davandvg.com 25
09/07/2021
03-06-2021

Insert an item from rear end


Delete an item from front end
Display the contents of queue
Are same as linear queue…
pls refer slide no 12 to 15

Shwetha BS Data Structure www.davandvg.com 26


09/07/2021
03-06-2021

Shwetha BS Data Structure www.davandvg.com 27


09/07/2021
03-06-2021

Shwetha BS Data Structure www.davandvg.com 28


09/07/2021
03-06-2021

Shwetha BS Data Structure www.davandvg.com 29


09/07/2021
03-06-2021
Function to insert front

Shwetha BS Data Structure www.davandvg.com 30


09/07/2021
03-06-2021

Shwetha BS Data Structure www.davandvg.com 31


09/07/2021
03-06-2021
Function to delete rear

Shwetha BS Data Structure www.davandvg.com 32


13/07/2021
03-06-2021
Priority queue

 A queue in which we are able to insert items or remove items from any position based
on some priority is often referred to as a priority queue.
 Always an element with highest priority is processed before processing any of the
lower priority elements.
 If the elements in the queue are of same priority, then the element, which is inserted
first into the queue, is processed.
 Priority queues are used in job scheduling algorithms in the design of operating
system where the jobs with highest priorities have to be processed first.

Shwetha BS Data Structure www.davandvg.com 33


13/07/2021
03-06-2021

The priority queues are classified into two groups:


 Ascending priority queue:
In an ascending priority queue elements can be inserted in any order but, while deleting an
element from the queue, only the smallest element is removed first.
 Descending priority queue:
In descending priority also elements can be inserted in any order but, while deleting an
element from the queue, only the largest element is deleted first.

Shwetha BS Data Structure www.davandvg.com 34


13/07/2021
03-06-2021
Implementing priority queues using arrays
Design 1:
 One of the methods is to implement an ascending priority queue where elements can be inserted in
any fashion and only the smallest element is removed.
 Here, an element is inserted from the rear end of the queue but an element with least value should
be deleted . After deleting the smallest number, store a very large value in that location, indicating
the absence of an item.
 The variable count can be used to keep track of number of elements in the array.
 The three functions useful for this purpose are
 insert_rear() - which inserts the item at the end of the queue.
 remove_small() - which returns the smallest item from the queue and at the same time store
maximum number in that location indicating an item has been deleted.
 Display() - which displays the contents of queue.
Shwetha BS Data Structure www.davandvg.com 35
13/07/2021
03-06-2021

Design 2:
 The second technique is to insert the items based on the priority.
 In this technique, we assume the item to be inserted itself denotes the priority.
So, the items with least value can be considered as the items with highest priority
and items with highest value can be considered as the items with least priority.
 So, to implement priority queue, we insert the elements into queue in such a way
that they are always ordered in increasing order.
 With this technique the highest priority elements are at the front end of the queue
and lowest priority elements are at the rear end of queue.
 Hence, while deleting an item, always delete from the front end so that highest
priority element is deleted first.
Shwetha BS Data Structure www.davandvg.com 36
13/07/2021
03-06-2021

The function to insert an item at the appropriate place is shown below:

Shwetha BS Data Structure www.davandvg.com 37


13/07/2021
03-06-2021
Applications of queue
 In a multitasking operating system, the CPU cannot run all jobs at once. So, all the
jobs are arranged in batches and are scheduled based on FIFO which is an
application queue.
 Operating System maintains a queue of processes that are ready to execute
 Operating System maintains a queue of processes that are waiting for a particular
event to occur
 When multiple users use a printer in a networked computer system, all the files to
printed will be sent to a queue. All the files will be printed by the printer in the
order in which they are queued in a print queue. This ensures that only one person
has access to the printer at a given time.
Shwetha BS Data Structure www.davandvg.com 38
15/07/2021
03-06-2021
Recursion
 A recursion is a method of solving the problem where the solution to a problem
depends on solutions to smaller instances of the same problem. Thus, a
recursive function is a function that calls itself during execution. This enables
the function to repeat itself several times to solve a given problem.
 The various types of recursion are shown below:
 Direct recursion
 Indirect recursion

Shwetha BS Data Structure www.davandvg.com 39


15/07/2021
03-06-2021

 Direct recursion:
A recursive function that invokes itself is said to have direct recursion.
For example, the factorial function calls itself and hence the function is said to have
direct recursion

Shwetha BS Data Structure www.davandvg.com 40


15/07/2021
03-06-2021

 Indirect recursion:
A function which contains a call to another function which in turn calls another function
which in turn calls another function and so on and eventually calls the first function is
called indirect recursion. It is very difficult to read, understand and find any logical errors
in a function that has indirect recursion. For example, a function fl invokes f2 which in
turn invokes f3 which in turn invokes fl is said to have indirect recursion. This is
pictorially represented as shown below:

Shwetha BS Data Structure www.davandvg.com 41


15/07/2021
03-06-2021
Base Case And General Case
 Definition: A base case is a special case where solution can be obtained without
using recursion. This is also called base/terminal condition. Each recursive function
must have a base case. A base case serves two purposes:
1) It acts as terminating condition.
2) The recursive function obtains the solution from the base case it reaches.
 Definition: In any recursive function, the part of the function except base case is
called general case. This portion of the code contains the logic required to reduce
the size (or instance) of the problem so as to move towards the base case or terminal
condition. Here, each time the function is called, the size (or instance) of the
problem is reduced.
Shwetha BS Data Structure www.davandvg.com 42
15/07/2021
03-06-2021
Compute factorial of n

Computing factorial of 5 using recursion

Shwetha BS Data Structure www.davandvg.com 43


15/07/2021
03-06-2021
Compute factorial of n

Shwetha BS Data Structure www.davandvg.com 44


15/07/2021
03-06-2021

#include<stdio.h>
int fact(int n)
{
if(n==0) return 1;
return n*fact(n-1);
C program to compute }
factorial of n void main()
{
int n;
float res;
printf("Enter n \n");
scanf("%d %d",&n, &r);
res = fact(n);
printf("%d! = %d\n", n, res);
}

Shwetha BS Data Structure www.davandvg.com 45


15/07/2021
03-06-2021 How recursion works

Shwetha BS Data Structure www.davandvg.com 46


15/07/2021
03-06-2021

Shwetha BS Data Structure www.davandvg.com 47


20/07/2021
03-06-2021
GCD using recursion

Shwetha BS Data Structure www.davandvg.com 48


20/07/2021
03-06-2021
#include<stdio.h>
int gcd(int,int);
void main()
{
int a,b,r;
printf("Enter 2 numbers \n");
scanf("%d %d",&a, &b);
r = gcd(a,b);
printf(“gcd = %d\n", r);
}
int gcd(int m,int n)
{
if(n==0) return (m);
else
If(m<n)
return (gcd(n,m));
else
return(gcd(n,m%n));
}

Shwetha BS Data Structure www.davandvg.com 49


20/07/2021
03-06-2021 Fibonacci numbers

Shwetha BS Data Structure www.davandvg.com 50


20/07/2021
03-06-2021

Shwetha BS Data Structure www.davandvg.com 51


20/07/2021
03-06-2021
Tower of Hanoi

Shwetha BS Data Structure www.davandvg.com 52


03-06-2021

#include <stdio.h>
void transfer (int n, char s, char t, char d)
{
if (n=0) return;
transfer (n-1, s, d, t);
printf("Move disc %d from %c to %c\n", n, s, d);
transfer (n-1, t, s, d);
}
void main()
{
int n;
printf("Enter the number of discs\n");
scanf("%d",&n);
tower(n, 'A', 'B', 'C’);
}

Shwetha BS Data Structure www.davandvg.com 53


20/07/2021
Binomial coefficient
03-06-2021

Shwetha BS Data Structure www.davandvg.com 54


03-06-2021
#include <stdio.h>
int C(int n, int k)
{
if (k > n)
return 0;
if (k == 0 || k == n)
return 1;
return C (n - 1, k - 1)+ C (n - 1, k);
}
void main()
{
int n,k,res;
printf(“ Enter N and K \n”);
scanf( “%d%d”,&n,&k);
res=C(n,k);
printf(“%d C %d\n”,n,k,res);
}

Shwetha BS Data Structure www.davandvg.com 55


www.davandvg.com

Thank you

56

You might also like