Professional Documents
Culture Documents
Unit 3 Queue and Recursion
Unit 3 Queue and Recursion
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
6
Data Structure
Shwetha BS
Today’s topic
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:
Circular queue
Types of
queues
Double ended queue ( dequeue)
Priority queue
Insert
Queue Delete
operation
Display
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;
}
10 20 30 30
0 1 2 3 4 0 1 2 3 4
Front rear Front
rear
front < rear front = rear
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.
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
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
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:
#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);
}
#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’);
}
Thank you
56