SlideShare a Scribd company logo
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
SIMPLE QUEUE
SIMPLE QUEUE
(ARRAY IMPLEMENTATION)
• Some computer programming languages allow a module or
function to call itself.
• This technique is known as recursion.
• In recursion, a function α either calls itself directly or calls a
function β that in turn calls the original function α. The
function α is called recursive function.
• Using recursive algorithm, certain problems can be solved
quite easily.
• Examples of such problems are Tower of Hanoi (TOH),
Inorder/Preorder/Postorder Tree Traversal, DFS of Graph etc.
1/20/2023
2
Recursion (CO4)
• A recursive function can go infinite like a loop. To avoid
infinite running of recursive function, there are two properties
that a recursive function must have −
– Base criteria − There must be at least one base criteria or
condition, such that, when this condition is met the function
stops calling itself recursively.
– Progressive approach − The recursive calls should progress in
such a way that each time a recursive call is made it comes
closer to the base criteria.
• Example:-
int fact(int n)
{
if (n < = 1) // base case
return 1;
else
return n*fact(n-1); }
1/20/2023
3
Properties of Recursion
A function fun is called direct
recursive if it calls the same
function fun.
• // An example of direct
recursion
void directRecFun()
{
// Some code....
directRecFun();
// Some code...
}
A function fun is called indirect
recursive if it calls another function
say fun_new and fun_new calls fun
directly or indirectly.
• // An example of indirect
recursion
void indirectRecFun1()
{
// Some code...
IndirectRecFun2();
// Some code...
}
void indirectRecFun2()
{
// Some code...
indirectRecFun1();
// Some code...
}
1/20/2023
4
Direct and Indirect Recursion
• When any function is called from main(), the memory is
allocated to it on the stack. A recursive function calls itself,
the memory for a called function is allocated on top of
memory allocated to calling function and different copy of
local variables is created for each function call. When the base
case is reached, the function returns its value to the function
by whom it is called and memory is de-allocated and the
process continues.
• Example:-
int fact(int n)
{
if (n < = 1) // base case
return 1;
else
return n*fact(n-1); }
1/20/2023
5
Recursion Working
1/20/2023
6
Recursion Working (contd..)
1/20/2023
7
Factorial using Iteration
Int fact(int n)
{
int result=1;
for(i=n;i>=1;i--)
{
result=result*i;
}
return result;
}
• Fibonacci series
Int fib(int n)
{
if(n=0 or n=1)
return n;
else
return fib(n-1)+fib(n-
2);
}
• Power
int power(int a, int b)
{
if(b==0)
return 1;
else
return a*power(a, b-
1);
}
1/20/2023
8
More examples of recursion
• Advantage:-
– Recursion provides a clean and simple way to write code.
– Some problems are inherently recursive like tree
traversals, Tower of Hanoi, etc. For such problems, it is
preferred to write recursive code.
– We can write such codes also iteratively with the help of a stack
data structure.
• Disadvantage:-
– Every recursive program can be written iteratively and vice versa
is also true.
– The recursive program has greater space requirements than
iterative program as all functions will remain in the stack until
the base case is reached.
– It also has greater time requirements because of function calls
and returns overhead.
1/20/2023
9
Advantage and Disadvantage of
Recursion
• Tower of Hanoi, is a mathematical puzzle which consists of
three towers (pegs) and more than one rings is as depicted −
• These rings are of different sizes and stacked upon in an
ascending order, i.e. the smaller one sits over the larger one.
There are other variations of the puzzle where the number of
disks increase, but the tower count remains the same.
1/20/2023
10
Tower of Hanoi
• The mission is to move all the disks to some another tower
without violating the sequence of arrangement. A few rules to
be followed for Tower of Hanoi are −
– Only one disk can be moved among the towers at any
given time.
– Only the "top" disk can be removed.
– No large disk can sit over a small disk.
1/20/2023
11
Tower of Hanoi (Rules)
• We divide the stack of disks in two parts. The largest disk
(nth disk) is in one part and all other (n-1) disks are in the
second part.
• Our ultimate aim is to move disk n from source to destination
and then put all other (n-1) disks onto it. We can imagine to
apply the same in a recursive way for all given set of disks.
• The steps to follow are −
– Step 1 − Move n-1 disks from source to aux
– Step 2 − Move nth disk from source to dest
– Step 3 − Move n-1 disks from aux to dest
1/20/2023
12
Tower of Hanoi (Algorithm)
• Algorithm:-
START
Procedure Hanoi(disk, source, aux, dest)
IF disk == 1, THEN
move disk from source to dest
ELSE
Hanoi(disk - 1, source, dest, aux) // Step 1
move disk from source to dest // Step 2
Hanoi(disk - 1, aux, source, dest) // Step 3
END IF
END Procedure
STOP
1/20/2023
13
Tower of Hanoi (Algorithm)
1/20/2023
14
Tower of Hanoi (Example)
• A=Beginning, B=Auxillary, C=Destination
• If n is the number of disks then 2n-1 steps are required for
solving the problem.
1/20/2023
15
Iteration v/s Recursion
• Queue is an abstract data structure, somewhat similar to
Stacks. Unlike stacks, a queue is open at both its ends. One
end is always used to insert data (enqueue) and the other is
used to remove data (dequeue). Queue follows First-In-First-
Out methodology, i.e., the data item stored first will be
accessed first.
• A real-world example of queue can be a single-lane one-way
road, where the vehicle enters first, exits first. More real-
world examples can be seen as queues at the ticket windows
and bus-stops.
1/20/2023
16
Queues (CO1)
• As we now understand that in queue, we access both ends for
different reasons. The following diagram given below tries to
explain queue representation as data structure −
• As in stacks, a queue can also be implemented using Arrays,
Linked-lists
1/20/2023
17
Queue Representation
• A queue can be implemented by means of:-
– Array (Static Implementation)
– Linked List (Dynamic Implementation)
1/20/2023
18
Queue Implementation
• Queue operations may involve initializing or defining the
queue, utilizing it, and then completely erasing it from the
memory. Here we shall try to understand the basic operations
associated with queues −
– enqueue() − add (store) an item to the queue.
– dequeue() − remove (access) an item from the queue.
• In queue, we always dequeue (or access) data, pointed
by front pointer and while enqueing (or storing) data in the
queue we take help of rear pointer.
1/20/2023
19
Basic Operations
• Queues maintain two data pointers, front and rear.
Therefore, its operations are comparatively difficult to
implement than stacks.
• The following steps should be taken to enqueue (insert) data
into a queue
– Step 1 − Check if the queue is full.
– Step 2 − If the queue is full, produce overflow error and exit.
– Step 3 − If the queue is not full, increment rear pointer to
point the next empty space.
– Step 4 − Add data element to the queue location, where the
rear is pointing.
– Step 5 − return success.
1/20/2023
20
Enqueue Operation
• Implementation of enqueue() in C programming language −
• Example
int enqueue(int data)
if(isfull())
return 0;
rear = rear + 1;
queue[rear] = data;
return 1;
end procedure
1/20/2023
21
Enqueue Operation
• Accessing data from the queue is a process of two tasks −
access the data where front is pointing and remove the data
after access. The following steps are taken to
perform dequeue operation −
– Step 1 − Check if the queue is empty.
– Step 2 − If the queue is empty, produce underflow error and
exit.
– Step 3 − If the queue is not empty, access the data
where front is pointing.
– Step 4 − Increment front pointer to point to the next available
data element.
– Step 5 − Return success.
1/20/2023
22
Dequeue Operation
• Implementation of dequeue() in C programming language −
• Example
int dequeue()
{
if(isempty())
return 0;
int data = queue[front];
front = front + 1;
return data;
}
1/20/2023
23
Dequeue Operation
• A circular queue is an improvement over the standard queue
structure. In a standard queue, when an element is deleted,
the vacant space is not reutilized. However, in a circular
queue, vacant spaces are reutilized.
• While inserting elements, when you reach the end of an array
and you need to insert another element, you must insert that
element at the beginning (given that the first element has
been deleted and the space is vacant).
1/20/2023
24
Circular Queue
• The most common queue implementation is using arrays, but
it can also be implemented using lists.
• Array implementation:-
– https://www.programiz.com/dsa/circular-queue
1/20/2023
25
Circular Queue (Implementation)
• CPU scheduling
• Memory management
• Traffic Management
1/20/2023
26
Circular Queue Applications
• Dequeue or Double Ended Queue is a type of queue in which
insertion and removal of elements can be performed from
either from the front or rear.
• Thus, it does not follow FIFO rule (First In First Out).
• Types of Dequeue
– Input Restricted Deque
In this deque, input is restricted at a single end but allows
deletion at both the ends.
– Output Restricted Deque
In this deque, output is restricted at a single end but
allows insertion at both the ends.
1/20/2023
27
Dequeue
• Insert at the Front
• Insert at the Rear
• Delete from the Front
• Delete from the Rear
• Implementation of dequeue using array
– https://www.programiz.com/dsa/deque
1/20/2023
28
Operations on a Dequeue
• In undo operations on software.
• To store history in browsers.
• For implementing both stacks and queues.
1/20/2023
29
Dequeue Applications
• A priority queue is a special type of queue in which each
element is associated with a priority and is served according
to its priority.
• If elements with the same priority occur, they are served
according to their order in the queue.
1/20/2023
30
Priority Queue
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
PROGRAM TO IMPLEMENT SIMPLE QUEUE USING ARRAY
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 7
void display(int Queue[],int front,int rear)
{
int i;
if(front==-1)
{
printf("Queue is Emptyn");
return;
}
for(i=front;i<=rear;i++)
printf("%d ",Queue[i]);
printf("n");
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
FUNCTION : ENQUEUE OPERATION
void enqueue(int Queue[],int *front,int *rear,int item)
{ if(*rear==MAXSIZE-1)
{
printf("Overflown");
return;
}
if(*front==-1)
{
*front=0;
*rear=0;
}
else
*rear=*rear+1;
Queue[*rear]=item;
display(Queue,*front,*rear);
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
FUNCTION: DEQUEUE OPERATION
int dequeue(int Queue[],int *front,int *rear)
{
int x;
if(*front==-1)
{
printf("Underflown");
return -1;
}
x=Queue[*front];
if(*front==*rear)
*front=*rear=-1;
else
*front=*front+1;
display(Queue,*front,*rear);
return x;
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
MAIN() FUNCTION
int main()
{
int Queue[MAXSIZE];
int front=-1,rear=-1;
//display(Queue,front,rear);
enqueue(Queue,&front,&rear,1);
enqueue(Queue,&front,&rear,2);
enqueue(Queue,&front,&rear,3);
enqueue(Queue,&front,&rear,4);
enqueue(Queue,&front,&rear,5);
enqueue(Queue,&front,&rear,6);
enqueue(Queue,&front,&rear,7);
enqueue(Queue,&front,&rear,8);
dequeue(Queue,&front,&rear);
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
MAIN() FUNCTION
dequeue(Queue,&front,&rear);
dequeue(Queue,&front,&rear);
dequeue(Queue,&front,&rear);
dequeue(Queue,&front,&rear);
dequeue(Queue,&front,&rear);
dequeue(Queue,&front,&rear);
dequeue(Queue,&front,&rear);
return 0;
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
SIMPLE QUEUE
SIMPLE QUEUE
(LINKED LIST IMPLEMENTATION)
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
PROGRAM TO IMPLEMENT SIMPLE QUEUE LINKED LIST
#include <stdio.h>
#include <stdlib.h>
#include<limits.h>
struct node
{
int data;
struct node *next;
};
struct node *Allocate(int x)
{ struct node *nn=(struct node *)malloc(sizeof(struct node));
if(nn==NULL)
return NULL;
nn->data=x;
nn->next=NULL;
return nn;
};
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
PROGRAM TO IMPLEMENT SIMPLE QUEUE LINKED LIST
void display(struct node *fr,struct node *re)
{
struct node *t;
if(fr==NULL)
{
printf("Queue is empty");
return;
}
for(t=fr;t!=re;t=t->next)
{
printf("%d -> ",t->data);
}
printf("%d ",t->data);
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
PROGRAM TO IMPLEMENT SIMPLE QUEUE LINKED LIST
void enqueue(struct node **fr,struct node **re,int x)
{
struct node *nn=Allocate(x);
if(nn==NULL)
{
printf("nOverflow");
return;
}
if(*fr==NULL)
*fr=*re=nn;
else
{
(*re)->next=nn;
*re=nn;
}
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
PROGRAM TO IMPLEMENT SIMPLE QUEUE LINKED LIST
int dequeue(struct node **fr,struct node **re)
{ int x;
struct node *temp;
if(*fr==NULL)
{
printf("nUnderflow");
return INT_MIN;
}
temp=*fr;
if(*fr==*re)
*fr=*re=NULL;
else
*fr=(*fr)->next;
x=temp->data;
free(temp);
return x;
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
PROGRAM TO IMPLEMENT SIMPLE QUEUE LINKED LIST
int main()
{ struct node *front=NULL,*rear=NULL;
int x;
enqueue(&front,&rear,1);
enqueue(&front,&rear,2);
enqueue(&front,&rear,3);
enqueue(&front,&rear,4);
enqueue(&front,&rear,5);
enqueue(&front,&rear,6);
enqueue(&front,&rear,7);
enqueue(&front,&rear,8);
display(front,rear);
x=dequeue(&front,&rear);
if(x!=INT_MIN)
printf("n%d is deleted",x);
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
PROGRAM TO IMPLEMENT SIMPLE QUEUE LINKED LIST
x=dequeue(&front,&rear);
if(x!=INT_MIN)
printf("n%d is deleted",x);
x=dequeue(&front,&rear);
if(x!=INT_MIN)
printf("n%d is deleted",x);
x=dequeue(&front,&rear);
if(x!=INT_MIN)
printf("n%d is deleted",x);
x=dequeue(&front,&rear);
if(x!=INT_MIN)
printf("n%d is deleted",x);
x=dequeue(&front,&rear);
if(x!=INT_MIN)
printf("n%d is deleted",x);
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
PROGRAM TO IMPLEMENT SIMPLE QUEUE LINKED LIST
x=dequeue(&front, &rear);
if(x!=INT_MIN)
printf("n%d is deleted", x);
x=dequeue(&front, &rear);
if(x!=INT_MIN)
printf("n%d is deleted", x);
x=dequeue(&front, &rear);
if(x!=INT_MIN)
printf("n%d is deleted", x);
return 0;
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
CIRCULAR QUEUE
CIRCULAR QUEUE
(ARRAY IMPLEMENTATION)
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
PROGRAM TO IMPLEMENT CIRCULAR QUEUE USING ARRAY
#include <stdio.h>
#include <stdlib.h>
#include<limits.h>
#define MAXSIZE 20
void display(int *Queue,int front, int rear)
{
int i;
if(front==-1)
{
printf("Queue is emptyn");
return;
}
for(i=front;i!=rear;i=(i+1)%MAXSIZE)
{
printf("%d ",Queue[i]);
}
printf("%d",Queue[i]);
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
PROGRAM TO IMPLEMENT CIRCULAR QUEUE USING ARRAY
void enqueue(int *Queue,int *front, int *rear,int x)
{
if(*front == (*rear+1)%MAXSIZE){
printf("Overflown");
return;
}
if(*front==-1)
*front=*rear=0;
else
*rear=(*rear+1)%MAXSIZE;
Queue[*rear]=x;
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
PROGRAM TO IMPLEMENT CIRCULAR QUEUE USING ARRAY
int dequeue(int *Queue,int *front,int *rear)
{
int x;
if(*front==-1)
{
printf("Underflown");
return INT_MIN;
}
x=Queue[*front];
if(*front==*rear)
*front=*rear=-1;
else
*front=(*front+1)%MAXSIZE;
return x;
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
PROGRAM TO IMPLEMENT CIRCULAR QUEUE USING ARRAY
int main()
{
int Queue[MAXSIZE];
int front=-1,rear=-1,x;
enqueue(Queue,&front,&rear,1);
enqueue(Queue,&front,&rear,2);
enqueue(Queue,&front,&rear,3);
enqueue(Queue,&front,&rear,4);
enqueue(Queue,&front,&rear,5);
display(Queue,front,rear);
x=dequeue(Queue,&front,&rear);
if(x!=INT_MIN)
printf("n%d is deletedn",x);
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
PROGRAM TO IMPLEMENT CIRCULAR QUEUE USING ARRAY
x=dequeue(Queue,&front,&rear);
if(x!=INT_MIN)
printf("%d is deletedn",x);
x=dequeue(Queue,&front,&rear);
if(x!=INT_MIN)
printf("%d is deletedn",x);
x=dequeue(Queue,&front,&rear);
if(x!=INT_MIN)
printf("%d is deletedn",x);
x=dequeue(Queue,&front,&rear);
if(x!=INT_MIN)
printf("%d is deletedn",x);
x=dequeue(Queue,&front,&rear);
if(x!=INT_MIN)
printf("%d is deletedn",x);
return 0;
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
CIRCULAR QUEUE
CIRCULAR QUEUE
LINKED LIST IMPLEMENTATION
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
PROGRAM TO IMPLEMENT CIRCULAR QUEUE LINKED LIST
#include <stdio.h>
#include <stdlib.h>
#include<limits.h>
struct node
{
int data;
struct node *next;
};
struct node *Allocate(int x)
{
struct node *nn=(struct node *)malloc(sizeof(struct node));
if(nn==NULL)
return NULL;
nn->data=x;
nn->next=NULL;
return nn;
};
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
PROGRAM TO IMPLEMENT CIRCULAR QUEUE LINKED LIST
void display(struct node *fr,struct node *re)
{
struct node *t;
if(fr==NULL)
{
printf("Queue is empty");
return;
}
for(t=fr;t!=re;t=t->next)
{
printf("%d -> ",t->data);
}
printf("%d ",t->data);
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
PROGRAM TO IMPLEMENT CIRCULAR QUEUE LINKED LIST
void enqueue(struct node **fr,struct node **re,int x)
{
struct node *nn=Allocate(x);
if(nn==NULL)
{
printf("nOverflow");
return;
}
if(*fr==NULL)
{
nn->next=nn;
*fr=*re=nn;
}
else
{
nn->next=*fr;
(*re)->next=nn;
*re=nn;
}
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
PROGRAM TO IMPLEMENT CIRCULAR QUEUE LINKED LIST
int dequeue(struct node **fr,struct node **re)
{ int x;
struct node *temp;
if(*fr==NULL)
{
printf("nUnderflow");
return INT_MIN;
}
temp=*fr;
if(*fr==*re)
*fr=*re=NULL;
else
{
*fr=(*fr)->next;
(*re)->next=*fr;
}
x=temp->data;
free(temp);
return x;
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
PROGRAM TO IMPLEMENT CIRCULAR QUEUE LINKED LIST
int main()
{ struct node *front=NULL,*rear=NULL;
int x;
enqueue(&front,&rear,1);
enqueue(&front,&rear,2);
enqueue(&front,&rear,3);
enqueue(&front,&rear,4);
enqueue(&front,&rear,5);
enqueue(&front,&rear,6);
enqueue(&front,&rear,7);
enqueue(&front,&rear,8);
display(front,rear);
x=dequeue(&front,&rear);
if(x!=INT_MIN)
printf("n%d is deleted",x);
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
PROGRAM TO IMPLEMENT CIRCULAR QUEUE LINKED LIST
x=dequeue(&front,&rear);
if(x!=INT_MIN)
printf("n%d is deleted",x);
x=dequeue(&front,&rear);
if(x!=INT_MIN)
printf("n%d is deleted",x);
x=dequeue(&front,&rear);
if(x!=INT_MIN)
printf("n%d is deleted",x);
x=dequeue(&front,&rear);
if(x!=INT_MIN)
printf("n%d is deleted",x);
x=dequeue(&front,&rear);
if(x!=INT_MIN)
printf("n%d is deleted",x);
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
PROGRAM TO IMPLEMENT CIRCULAR QUEUE LINKED LIST
if(x!=INT_MIN)
printf("n%d is deleted",x);
x=dequeue(&front,&rear);
if(x!=INT_MIN)
printf("n%d is deleted",x);
x=dequeue(&front,&rear);
if(x!=INT_MIN)
printf("n%d is deleted",x);
return 0;
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
PRIORITY QUEUE
PRIORITY QUEUE
(ARRAY IMPLEMENTATION)
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
PRIORITY QUEUE
#include <stdio.h>
#include <stdlib.h>
#include<limits.h>
#define MAXPR 5
#define MAXSIZE 5
int Q[MAXPR][MAXSIZE];
int front[MAXPR];
int rear[MAXPR];
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
PRIORITY QUEUE
void display()
{ int i,j;
for(i=0;i<MAXPR;i++)
{
for(j=0;j<MAXSIZE;j++)
{
if(Q[i][j]==INT_MIN)
printf(" ");
else
printf("%-2d ",Q[i][j]);
}
printf("Front[%d] = %2d, ",i,front[i]);
printf("Rear[%d] = %2d ",i,rear[i]);
printf("n");
}
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
PRIORITY QUEUE: ENQUEUE
void enqueue(int val,int prn)
{
if(prn<0 || prn>=MAXPR)
return;
if(rear[prn]==MAXSIZE-1)
{
printf("Overflow");
return;
}
if(front[prn]==-1)
{
front[prn]++;
}
rear[prn]++;
Q[prn][rear[prn]]=val;
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
PRIORITY QUEUE: DEQUEUE
int dequeue()
{ int i,x;
i=0;
while(i<MAXPR && front[i]==-1)
i++;
if(i==MAXPR)
{
printf("nUnderflow");
return INT_MIN;
}
x=Q[i][front[i]];
Q[i][front[i]]=INT_MIN;
if(front[i]==rear[i]){
front[i]=-1;rear[i]=-1;
}
else
front[i]++;
return x;
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
PRIORITY QUEUE: MAIN()
int main()
{
int i,j,x;
for(i=0;i<MAXPR;i++)
{
front[i]=-1;
rear[i]=-1;
for(j=0;j<MAXSIZE;j++)
Q[i][j]=INT_MIN;
}
enqueue(5,1);
enqueue(6,2);
enqueue(7,1);
enqueue(28,4);
enqueue(5,2);
enqueue(3,2);
enqueue(23,0);
display();
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
PRIORITY QUEUE: MAIN()
x=dequeue();
printf("nn%d is deletednn",x);
display();
return 0;
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
PRIORITY QUEUE
PRIORITY QUEUE
LINKED LIST IMPLEMENTATION
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
PRIORITY QUEUE
#include <stdio.h>
#include <stdlib.h>
#include<limits.h>
struct node
{
int data;
int prn;
struct node *next;
};
struct node *Allocate(int val,int p)
{
struct node *nn=(struct node*)malloc(sizeof(struct node));
if(nn!=NULL)
{
nn->data=val;
nn->prn=p;
nn->next=NULL;
}
return nn;
};
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
PRIORITY QUEUE
void display(struct node *he)
{
struct node *cur;
if(he==NULL)
{
printf("Queue is empty");
return ;
}
for(cur=he;cur->next!=NULL;cur=cur->next)
{
printf("(%d,%d)-> ",cur->data,cur->prn);
}
printf("(%d,%d)",cur->data,cur->prn);
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
PRIORITY QUEUE
void enqueue(struct node **he,int val,int p)
{
struct node *save,*cur;
struct node *nn=Allocate(val,p);
if(nn==NULL)
{
printf("Overflown");
return;
}
if(*he==NULL || p<(*he)->prn)
{
nn->next=*he;
*he=nn;
return;
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
PRIORITY QUEUE
cur=*he;
while(cur!=NULL && p>=cur->prn)
{
save=cur;
cur=cur->next;
}
save->next=nn;
nn->next=cur;
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
PRIORITY QUEUE
int dequeue(struct node **he)
{
struct node *temp;
int x;
if(*he==NULL)
{
printf("nUnderflown");
return INT_MIN;
}
temp=*he;
*he=(*he)->next;
x=temp->data;
free(temp);
return x;
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
PRIORITY QUEUE
int main()
{
struct node *head= NULL;
int x;
enqueue(&head,15,4);
enqueue(&head,8,2);
enqueue(&head,6,7);
enqueue(&head,15,1);
enqueue(&head,19,5);
enqueue(&head,23,2);
display(head);
x=dequeue(&head);
if(x!=INT_MIN)
printf("n%d is deleted",x);
x=dequeue(&head);
if(x!=INT_MIN)
printf("n%d is deleted",x);
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
PRIORITY QUEUE
x=dequeue(&head);
if(x!=INT_MIN)
printf("n%d is deleted",x);
x=dequeue(&head);
if(x!=INT_MIN)
printf("n%d is deleted",x);
x=dequeue(&head);
if(x!=INT_MIN)
printf("n%d is deleted",x);
x=dequeue(&head);
if(x!=INT_MIN)
printf("n%d is deleted",x);
x=dequeue(&head);
if(x!=INT_MIN)
printf("n%d is deleted",x);
return 0;
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
DEQUE
DOUBLE ENDED QUEUE
(DEQUE)
LINKED LIST IMPLEMENTATION
(SIMPLE INPUT RESTRICTED DEQUE)
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
INPUT RESTRICTED DEQUE
#include <stdio.h>
#include <stdlib.h>
#include<limits.h>
struct node
{
int data;
struct node *next;
};
struct node *Allocate(int x)
{
struct node *nn=(struct node *)malloc(sizeof(struct node));
if(nn==NULL)
return NULL;
nn->data=x;
nn->next=NULL;
return nn;
};
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
INPUT RESTRICTED DEQUE
void display(struct node *fr,struct node *re)
{
struct node *t;
if(fr==NULL)
{
printf("Queue is empty");
return;
}
for(t=fr;t!=re;t=t->next)
{
printf("%d -> ",t->data);
}
printf("%d ",t->data);
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
INPUT RESTRICTED DEQUE
void enqueue_rear(struct node **fr,struct node **re,int x)
{
struct node *nn=Allocate(x);
if(nn==NULL)
{
printf("nOverflow");
return;
}
if(*fr==NULL)
{
*fr=*re=nn;
}
else
{
(*re)->next=nn;
*re=nn;
}
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
INPUT RESTRICTED DEQUE
int dequeue_front(struct node **fr,struct node **re)
{
int x;
struct node *temp;
if(*fr==NULL)
{
printf("nUnderflow");
return INT_MIN;
}
temp=*fr;
if(*fr==*re)
*fr=*re=NULL;
else
*fr=(*fr)->next;
x=temp->data;
free(temp);
return x;
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
INPUT RESTRICTED DEQUE
int dequeue_rear(struct node **fr,struct node **re)
{ int x;
struct node *temp,*save,*cur;
if(*fr==NULL)
{
printf("nUnderflow");
return INT_MIN;
}
temp=*re;
if(*fr==*re)
*fr=*re=NULL;
else
{
cur=*fr;
while(cur!=*re)
{
save=cur;
cur=cur->next;
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
INPUT RESTRICTED DEQUE
save->next=NULL;
re=save;
}
x=temp->data;
free(temp);
return x;
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
INPUT RESTRICTED DEQUE
int main()
{ int x;
struct node *front=NULL,*rear=NULL;
enqueue_rear(&front,&rear,1);
enqueue_rear(&front,&rear,2);
display(front, rear);
x=dequeue_front(&front,&rear);
if(x!=INT_MIN)
printf("n%d is deleted",x);
x=dequeue_rear(&front,&rear);
if(x!=INT_MIN)
printf("n%d is deleted",x);
x=dequeue_front(&front,&rear);
if(x!=INT_MIN)
printf("n%d is deleted",x);
return 0;
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
DEQUE
DOUBLE ENDED QUEUE
(DEQUE)
LINKED LIST IMPLEMENTATION
(SIMPLE OUTPUT RESTRICTED DEQUE)
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
OUPUT RESTRICTED DEQUE
#include <stdio.h>
#include <stdlib.h>
#include<limits.h>
struct node
{
int data;
struct node *next;
};
struct node *Allocate(int x)
{
struct node *nn=(struct node *)malloc(sizeof(struct node));
if(nn==NULL)
return NULL;
nn->data=x;
nn->next=NULL;
return nn;
};
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
OUPUT RESTRICTED DEQUE
void display(struct node *fr,struct node *re)
{
struct node *t;
if(fr==NULL)
{
printf("Queue is empty");
return;
}
for(t=fr;t!=re;t=t->next)
{
printf("%d -> ",t->data);
}
printf("%d ",t->data);
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
OUPUT RESTRICTED DEQUE
void enqueue_rear(struct node **fr,struct node **re,int x)
{
struct node *nn=Allocate(x);
if(nn==NULL)
{
printf("nOverflow");
return;
}
if(*fr==NULL)
{
*fr=*re=nn;
}
else
{
(*re)->next=nn;
*re=nn;
}
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
OUPUT RESTRICTED DEQUE
void enqueue_front(struct node **fr,struct node **re,int x)
{
struct node *nn=Allocate(x);
if(nn==NULL)
{
printf("nOverflow");
return;
}
if(*fr==NULL)
{
*fr=*re=nn;
}
else
{
nn->next=*fr;
*fr=nn;
}
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
OUPUT RESTRICTED DEQUE
int dequeue_front(struct node **fr,struct node **re)
{
int x;
struct node *temp;
if(*fr==NULL)
{
printf("nUnderflow");
return INT_MIN;
}
temp=*fr;
if(*fr==*re)
*fr=*re=NULL;
else
*fr=(*fr)->next;
x=temp->data;
free(temp);
return x;
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
OUPUT RESTRICTED DEQUE
int main()
{ int x;
struct node *front=NULL,*rear=NULL;
enqueue_rear(&front,&rear,1);
enqueue_front(&front,&rear,2);
display(front, rear);
x=dequeue_front(&front,&rear);
if(x!=INT_MIN)
printf("n%d is deleted",x);
x=dequeue_front(&front,&rear);
if(x!=INT_MIN)
printf("n%d is deleted",x);
x=dequeue_front(&front,&rear);
if(x!=INT_MIN)
printf("n%d is deleted",x);
return 0;
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
DEQUE
DOUBLE ENDED QUEUE
(DEQUE)
LINKED LIST IMPLEMENTATION
(CIRCULAR INPUT RESTRICTED DEQUE)
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
INPUT RESTRICTED DEQUE
#include <stdio.h>
#include <stdlib.h>
#include<limits.h>
struct node
{
int data;
struct node *next;
};
struct node *Allocate(int x)
{
struct node *nn=(struct node *)malloc(sizeof(struct node));
if(nn==NULL)
return NULL;
nn->data=x;
nn->next=NULL;
return nn;
};
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
INPUT RESTRICTED DEQUE
void display(struct node *fr,struct node *re)
{
struct node *t;
if(fr==NULL)
{
printf("Queue is empty");
return;
}
for(t=fr;t!=re;t=t->next)
{
printf("%d -> ",t->data);
}
printf("%d ",t->data);
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
INPUT RESTRICTED DEQUE
void enqueue_rear(struct node **fr,struct node **re,int x)
{
struct node *nn=Allocate(x);
if(nn==NULL)
{
printf("nOverflow");
return;
}
if(*fr==NULL)
{
nn->next=nn;
*fr=*re=nn;
}
else
{
nn->next=*fr;
(*re)->next=nn;
*re=nn;
}
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
INPUT RESTRICTED DEQUE
int dequeue_front(struct node **fr,struct node **re)
{ int x;
struct node *temp;
if(*fr==NULL)
{
printf("nUnderflow");
return INT_MIN;
}
temp=*fr;
if(*fr==*re)
*fr=*re=NULL;
else
{
*fr=(*fr)->next;
(*re)->next=*fr;
}
x=temp->data;
free(temp);
return x;
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
INPUT RESTRICTED DEQUE
int dequeue_rear(struct node **fr,struct node **re)
{ int x;
struct node *cur,*save;
if(*re==NULL)
{
printf("nUnderflow");
return INT_MIN;
}
cur=*fr;
if(*fr==*re)
*fr=*re=NULL;
else
{
cur=*fr;
while(cur!=*re)
{
save=cur;
cur=cur->next;
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
INPUT RESTRICTED DEQUE
save->next=*fr;
*re=save;
}
x=cur->data;
free(cur);
return x;
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
INPUT RESTRICTED DEQUE
int main()
{
struct node *front=NULL,*rear=NULL;
int x;
enqueue_rear(&front,&rear,1);
enqueue_rear(&front,&rear,2);
enqueue_rear(&front,&rear,3);
enqueue_rear(&front,&rear,4);
enqueue_rear(&front,&rear,5);
enqueue_rear(&front,&rear,6);
display(front,rear);
x=dequeue_rear(&front,&rear);
if(x!=INT_MIN)
printf("n%d is deleted",x);
x=dequeue_front(&front,&rear);
if(x!=INT_MIN)
printf("n%d is deleted",x);
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
INPUT RESTRICTED DEQUE
x=dequeue_rear(&front,&rear);
if(x!=INT_MIN)
printf("n%d is deleted",x);
x=dequeue_front(&front,&rear);
if(x!=INT_MIN)
printf("n%d is deleted",x);
x=dequeue_rear(&front,&rear);
if(x!=INT_MIN)
printf("n%d is deleted",x);
x=dequeue_front(&front,&rear);
if(x!=INT_MIN)
printf("n%d is deleted",x);
x=dequeue_rear(&front,&rear);
if(x!=INT_MIN)
printf("n%d is deleted",x);
return 0;
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
DEQUE
DOUBLE ENDED QUEUE
(DEQUE)
LINKED LIST IMPLEMENTATION
(CIRCULAR OUTPUT RESTRICTED DEQUE)
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
OUTPUT RESTRICTED DEQUE
#include <stdio.h>
#include <stdlib.h>
#include<limits.h>
struct node
{
int data;
struct node *next;
};
struct node *Allocate(int x)
{
struct node *nn=(struct node *)malloc(sizeof(struct node));
if(nn==NULL)
return NULL;
nn->data=x;
nn->next=NULL;
return nn;
};
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
OUTPUT RESTRICTED DEQUE
void display(struct node *fr,struct node *re)
{
struct node *t;
if(fr==NULL)
{
printf("Queue is empty");
return;
}
for(t=fr;t!=re;t=t->next)
{
printf("%d -> ",t->data);
}
printf("%d ",t->data);
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
OUTPUT RESTRICTED DEQUE
void enqueue_rear(struct node **fr,struct node **re,int x)
{
struct node *nn=Allocate(x);
if(nn==NULL)
{
printf("nOverflow");
return;
}
if(*fr==NULL)
{
nn->next=nn;
*fr=*re=nn;
}
else
{
nn->next=*fr;
(*re)->next=nn;
*re=nn;
}
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
OUPUT RESTRICTED DEQUE
void enqueue_front(struct node **fr,struct node **re,int x)
{
struct node *nn=Allocate(x);
if(nn==NULL)
{
printf("nOverflow");
return;
}
if(*fr==NULL)
{
nn->next=nn;
*fr=*re=nn;
}
else
{
nn->next=*fr;
(*re)->next=nn;
*fr=nn;
}
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
OUPUT RESTRICTED DEQUE
int dequeue_front(struct node **fr,struct node **re)
{ int x;
struct node *temp;
if(*fr==NULL)
{
printf("nUnderflow");
return INT_MIN;
}
temp=*fr;
if(*fr==*re)
*fr=*re=NULL;
else
{
*fr=(*fr)->next;
(*re)->next=*fr;
}
x=temp->data;
free(temp);
return x;
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
OUPUT RESTRICTED DEQUE
int dequeue_rear(struct node **fr,struct node **re)
{ int x;
struct node *cur,*save;
if(*re==NULL)
{
printf("nUnderflow");
return INT_MIN;
}
cur=*fr;
if(*fr==*re)
*fr=*re=NULL;
else
{
cur=*fr;
while(cur!=*re)
{
save=cur;
cur=cur->next;
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
OUPUT RESTRICTED DEQUE
save->next=*fr;
*re=save;
}
x=cur->data;
free(cur);
return x;
}
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
OUPUT RESTRICTED DEQUE
int main()
{
struct node *front=NULL,*rear=NULL;
int x;
enqueue_rear(&front,&rear,1);
enqueue_rear(&front,&rear,2);
enqueue_rear(&front,&rear,3);
enqueue_rear(&front,&rear,4);
enqueue_rear(&front,&rear,5);
enqueue_rear(&front,&rear,6);
display(front,rear);
x=dequeue_front(&front,&rear);
if(x!=INT_MIN)
printf("n%d is deleted",x);
x=dequeue_front(&front,&rear);
if(x!=INT_MIN)
printf("n%d is deleted",x);
NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions
OUPUT RESTRICTED DEQUE
x=dequeue_front(&front,&rear);
if(x!=INT_MIN)
printf("n%d is deleted",x);
x=dequeue_front(&front,&rear);
if(x!=INT_MIN)
printf("n%d is deleted",x);
x=dequeue_front(&front,&rear);
if(x!=INT_MIN)
printf("n%d is deleted",x);
x=dequeue_front(&front,&rear);
if(x!=INT_MIN)
printf("n%d is deleted",x);
x=dequeue_front(&front,&rear);
if(x!=INT_MIN)
printf("n%d is deleted",x);
return 0;
}

More Related Content

Similar to Queue (1)(1).ppt

Computer arithmetic in computer architecture
Computer arithmetic in computer architectureComputer arithmetic in computer architecture
Computer arithmetic in computer architecture
ishapadhy
 
Stack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTStack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADT
Soumen Santra
 
Search algorithms for discrete optimization
Search algorithms for discrete optimizationSearch algorithms for discrete optimization
Search algorithms for discrete optimization
Sally Salem
 
Data Structures - Lecture 6 [queues]
Data Structures - Lecture 6 [queues]Data Structures - Lecture 6 [queues]
Data Structures - Lecture 6 [queues]
Muhammad Hammad Waseem
 
Unit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxUnit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptx
Yogesh Pawar
 
Chapter 7 ds
Chapter 7 dsChapter 7 ds
Chapter 7 ds
Hanif Durad
 
Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)
Self-Employed
 
queue.pptx
queue.pptxqueue.pptx
queue.pptx
Dr.Shweta
 
DSJ_Unit I & II.pdf
DSJ_Unit I & II.pdfDSJ_Unit I & II.pdf
DSJ_Unit I & II.pdf
Arumugam90
 
2 b queues
2 b queues2 b queues
2 b queues
Nguync91368
 
Unit ii linear data structures
Unit ii linear data structures Unit ii linear data structures
Unit ii linear data structures
LavanyaJ28
 
Lesson 4 - Queue ADT.pdf
Lesson 4 - Queue ADT.pdfLesson 4 - Queue ADT.pdf
Lesson 4 - Queue ADT.pdf
LeandroJrErcia
 
Parallel search
Parallel searchParallel search
Parallel search
Md. Mahedi Mahfuj
 
A New Tracer for Reverse Engineering - PacSec 2010
A New Tracer for Reverse Engineering - PacSec 2010A New Tracer for Reverse Engineering - PacSec 2010
A New Tracer for Reverse Engineering - PacSec 2010
Tsukasa Oi
 
19-7960-07-notes.pptx
19-7960-07-notes.pptx19-7960-07-notes.pptx
19-7960-07-notes.pptx
MuhammadFahmiPamungk
 
Queue
QueueQueue
Data structure and algorithm using java
Data structure and algorithm using javaData structure and algorithm using java
Data structure and algorithm using java
Narayan Sau
 
Basic Queue Operation in DataStructure.pptx
Basic Queue Operation in DataStructure.pptxBasic Queue Operation in DataStructure.pptx
Basic Queue Operation in DataStructure.pptx
LakshmiSamivel
 
backpropagation in neural networks
backpropagation in neural networksbackpropagation in neural networks
backpropagation in neural networks
Akash Goel
 
Queue ADT for data structure for computer
Queue ADT for data structure for computerQueue ADT for data structure for computer
Queue ADT for data structure for computer
abinathsabi
 

Similar to Queue (1)(1).ppt (20)

Computer arithmetic in computer architecture
Computer arithmetic in computer architectureComputer arithmetic in computer architecture
Computer arithmetic in computer architecture
 
Stack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTStack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADT
 
Search algorithms for discrete optimization
Search algorithms for discrete optimizationSearch algorithms for discrete optimization
Search algorithms for discrete optimization
 
Data Structures - Lecture 6 [queues]
Data Structures - Lecture 6 [queues]Data Structures - Lecture 6 [queues]
Data Structures - Lecture 6 [queues]
 
Unit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxUnit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptx
 
Chapter 7 ds
Chapter 7 dsChapter 7 ds
Chapter 7 ds
 
Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)
 
queue.pptx
queue.pptxqueue.pptx
queue.pptx
 
DSJ_Unit I & II.pdf
DSJ_Unit I & II.pdfDSJ_Unit I & II.pdf
DSJ_Unit I & II.pdf
 
2 b queues
2 b queues2 b queues
2 b queues
 
Unit ii linear data structures
Unit ii linear data structures Unit ii linear data structures
Unit ii linear data structures
 
Lesson 4 - Queue ADT.pdf
Lesson 4 - Queue ADT.pdfLesson 4 - Queue ADT.pdf
Lesson 4 - Queue ADT.pdf
 
Parallel search
Parallel searchParallel search
Parallel search
 
A New Tracer for Reverse Engineering - PacSec 2010
A New Tracer for Reverse Engineering - PacSec 2010A New Tracer for Reverse Engineering - PacSec 2010
A New Tracer for Reverse Engineering - PacSec 2010
 
19-7960-07-notes.pptx
19-7960-07-notes.pptx19-7960-07-notes.pptx
19-7960-07-notes.pptx
 
Queue
QueueQueue
Queue
 
Data structure and algorithm using java
Data structure and algorithm using javaData structure and algorithm using java
Data structure and algorithm using java
 
Basic Queue Operation in DataStructure.pptx
Basic Queue Operation in DataStructure.pptxBasic Queue Operation in DataStructure.pptx
Basic Queue Operation in DataStructure.pptx
 
backpropagation in neural networks
backpropagation in neural networksbackpropagation in neural networks
backpropagation in neural networks
 
Queue ADT for data structure for computer
Queue ADT for data structure for computerQueue ADT for data structure for computer
Queue ADT for data structure for computer
 

Recently uploaded

Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
Aditya Rajan Patra
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
IJECEIAES
 
CSM Cloud Service Management Presentarion
CSM Cloud Service Management PresentarionCSM Cloud Service Management Presentarion
CSM Cloud Service Management Presentarion
rpskprasana
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
thanhdowork
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
JamalHussainArman
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
MDSABBIROJJAMANPAYEL
 
sieving analysis and results interpretation
sieving analysis and results interpretationsieving analysis and results interpretation
sieving analysis and results interpretation
ssuser36d3051
 
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
Mukeshwaran Balu
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
Hitesh Mohapatra
 
PPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testingPPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testing
anoopmanoharan2
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
gerogepatton
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
IJNSA Journal
 
Swimming pool mechanical components design.pptx
Swimming pool  mechanical components design.pptxSwimming pool  mechanical components design.pptx
Swimming pool mechanical components design.pptx
yokeleetan1
 
Low power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniquesLow power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniques
nooriasukmaningtyas
 
Wearable antenna for antenna applications
Wearable antenna for antenna applicationsWearable antenna for antenna applications
Wearable antenna for antenna applications
Madhumitha Jayaram
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
IJECEIAES
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
SyedAbiiAzazi1
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
jpsjournal1
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
Madan Karki
 

Recently uploaded (20)

Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
 
CSM Cloud Service Management Presentarion
CSM Cloud Service Management PresentarionCSM Cloud Service Management Presentarion
CSM Cloud Service Management Presentarion
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
 
sieving analysis and results interpretation
sieving analysis and results interpretationsieving analysis and results interpretation
sieving analysis and results interpretation
 
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
 
PPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testingPPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testing
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
 
Swimming pool mechanical components design.pptx
Swimming pool  mechanical components design.pptxSwimming pool  mechanical components design.pptx
Swimming pool mechanical components design.pptx
 
Low power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniquesLow power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniques
 
Wearable antenna for antenna applications
Wearable antenna for antenna applicationsWearable antenna for antenna applications
Wearable antenna for antenna applications
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
 

Queue (1)(1).ppt

  • 1. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions SIMPLE QUEUE SIMPLE QUEUE (ARRAY IMPLEMENTATION)
  • 2. • Some computer programming languages allow a module or function to call itself. • This technique is known as recursion. • In recursion, a function α either calls itself directly or calls a function β that in turn calls the original function α. The function α is called recursive function. • Using recursive algorithm, certain problems can be solved quite easily. • Examples of such problems are Tower of Hanoi (TOH), Inorder/Preorder/Postorder Tree Traversal, DFS of Graph etc. 1/20/2023 2 Recursion (CO4)
  • 3. • A recursive function can go infinite like a loop. To avoid infinite running of recursive function, there are two properties that a recursive function must have − – Base criteria − There must be at least one base criteria or condition, such that, when this condition is met the function stops calling itself recursively. – Progressive approach − The recursive calls should progress in such a way that each time a recursive call is made it comes closer to the base criteria. • Example:- int fact(int n) { if (n < = 1) // base case return 1; else return n*fact(n-1); } 1/20/2023 3 Properties of Recursion
  • 4. A function fun is called direct recursive if it calls the same function fun. • // An example of direct recursion void directRecFun() { // Some code.... directRecFun(); // Some code... } A function fun is called indirect recursive if it calls another function say fun_new and fun_new calls fun directly or indirectly. • // An example of indirect recursion void indirectRecFun1() { // Some code... IndirectRecFun2(); // Some code... } void indirectRecFun2() { // Some code... indirectRecFun1(); // Some code... } 1/20/2023 4 Direct and Indirect Recursion
  • 5. • When any function is called from main(), the memory is allocated to it on the stack. A recursive function calls itself, the memory for a called function is allocated on top of memory allocated to calling function and different copy of local variables is created for each function call. When the base case is reached, the function returns its value to the function by whom it is called and memory is de-allocated and the process continues. • Example:- int fact(int n) { if (n < = 1) // base case return 1; else return n*fact(n-1); } 1/20/2023 5 Recursion Working
  • 7. 1/20/2023 7 Factorial using Iteration Int fact(int n) { int result=1; for(i=n;i>=1;i--) { result=result*i; } return result; }
  • 8. • Fibonacci series Int fib(int n) { if(n=0 or n=1) return n; else return fib(n-1)+fib(n- 2); } • Power int power(int a, int b) { if(b==0) return 1; else return a*power(a, b- 1); } 1/20/2023 8 More examples of recursion
  • 9. • Advantage:- – Recursion provides a clean and simple way to write code. – Some problems are inherently recursive like tree traversals, Tower of Hanoi, etc. For such problems, it is preferred to write recursive code. – We can write such codes also iteratively with the help of a stack data structure. • Disadvantage:- – Every recursive program can be written iteratively and vice versa is also true. – The recursive program has greater space requirements than iterative program as all functions will remain in the stack until the base case is reached. – It also has greater time requirements because of function calls and returns overhead. 1/20/2023 9 Advantage and Disadvantage of Recursion
  • 10. • Tower of Hanoi, is a mathematical puzzle which consists of three towers (pegs) and more than one rings is as depicted − • These rings are of different sizes and stacked upon in an ascending order, i.e. the smaller one sits over the larger one. There are other variations of the puzzle where the number of disks increase, but the tower count remains the same. 1/20/2023 10 Tower of Hanoi
  • 11. • The mission is to move all the disks to some another tower without violating the sequence of arrangement. A few rules to be followed for Tower of Hanoi are − – Only one disk can be moved among the towers at any given time. – Only the "top" disk can be removed. – No large disk can sit over a small disk. 1/20/2023 11 Tower of Hanoi (Rules)
  • 12. • We divide the stack of disks in two parts. The largest disk (nth disk) is in one part and all other (n-1) disks are in the second part. • Our ultimate aim is to move disk n from source to destination and then put all other (n-1) disks onto it. We can imagine to apply the same in a recursive way for all given set of disks. • The steps to follow are − – Step 1 − Move n-1 disks from source to aux – Step 2 − Move nth disk from source to dest – Step 3 − Move n-1 disks from aux to dest 1/20/2023 12 Tower of Hanoi (Algorithm)
  • 13. • Algorithm:- START Procedure Hanoi(disk, source, aux, dest) IF disk == 1, THEN move disk from source to dest ELSE Hanoi(disk - 1, source, dest, aux) // Step 1 move disk from source to dest // Step 2 Hanoi(disk - 1, aux, source, dest) // Step 3 END IF END Procedure STOP 1/20/2023 13 Tower of Hanoi (Algorithm)
  • 14. 1/20/2023 14 Tower of Hanoi (Example) • A=Beginning, B=Auxillary, C=Destination • If n is the number of disks then 2n-1 steps are required for solving the problem.
  • 16. • Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a queue is open at both its ends. One end is always used to insert data (enqueue) and the other is used to remove data (dequeue). Queue follows First-In-First- Out methodology, i.e., the data item stored first will be accessed first. • A real-world example of queue can be a single-lane one-way road, where the vehicle enters first, exits first. More real- world examples can be seen as queues at the ticket windows and bus-stops. 1/20/2023 16 Queues (CO1)
  • 17. • As we now understand that in queue, we access both ends for different reasons. The following diagram given below tries to explain queue representation as data structure − • As in stacks, a queue can also be implemented using Arrays, Linked-lists 1/20/2023 17 Queue Representation
  • 18. • A queue can be implemented by means of:- – Array (Static Implementation) – Linked List (Dynamic Implementation) 1/20/2023 18 Queue Implementation
  • 19. • Queue operations may involve initializing or defining the queue, utilizing it, and then completely erasing it from the memory. Here we shall try to understand the basic operations associated with queues − – enqueue() − add (store) an item to the queue. – dequeue() − remove (access) an item from the queue. • In queue, we always dequeue (or access) data, pointed by front pointer and while enqueing (or storing) data in the queue we take help of rear pointer. 1/20/2023 19 Basic Operations
  • 20. • Queues maintain two data pointers, front and rear. Therefore, its operations are comparatively difficult to implement than stacks. • The following steps should be taken to enqueue (insert) data into a queue – Step 1 − Check if the queue is full. – Step 2 − If the queue is full, produce overflow error and exit. – Step 3 − If the queue is not full, increment rear pointer to point the next empty space. – Step 4 − Add data element to the queue location, where the rear is pointing. – Step 5 − return success. 1/20/2023 20 Enqueue Operation
  • 21. • Implementation of enqueue() in C programming language − • Example int enqueue(int data) if(isfull()) return 0; rear = rear + 1; queue[rear] = data; return 1; end procedure 1/20/2023 21 Enqueue Operation
  • 22. • Accessing data from the queue is a process of two tasks − access the data where front is pointing and remove the data after access. The following steps are taken to perform dequeue operation − – Step 1 − Check if the queue is empty. – Step 2 − If the queue is empty, produce underflow error and exit. – Step 3 − If the queue is not empty, access the data where front is pointing. – Step 4 − Increment front pointer to point to the next available data element. – Step 5 − Return success. 1/20/2023 22 Dequeue Operation
  • 23. • Implementation of dequeue() in C programming language − • Example int dequeue() { if(isempty()) return 0; int data = queue[front]; front = front + 1; return data; } 1/20/2023 23 Dequeue Operation
  • 24. • A circular queue is an improvement over the standard queue structure. In a standard queue, when an element is deleted, the vacant space is not reutilized. However, in a circular queue, vacant spaces are reutilized. • While inserting elements, when you reach the end of an array and you need to insert another element, you must insert that element at the beginning (given that the first element has been deleted and the space is vacant). 1/20/2023 24 Circular Queue
  • 25. • The most common queue implementation is using arrays, but it can also be implemented using lists. • Array implementation:- – https://www.programiz.com/dsa/circular-queue 1/20/2023 25 Circular Queue (Implementation)
  • 26. • CPU scheduling • Memory management • Traffic Management 1/20/2023 26 Circular Queue Applications
  • 27. • Dequeue or Double Ended Queue is a type of queue in which insertion and removal of elements can be performed from either from the front or rear. • Thus, it does not follow FIFO rule (First In First Out). • Types of Dequeue – Input Restricted Deque In this deque, input is restricted at a single end but allows deletion at both the ends. – Output Restricted Deque In this deque, output is restricted at a single end but allows insertion at both the ends. 1/20/2023 27 Dequeue
  • 28. • Insert at the Front • Insert at the Rear • Delete from the Front • Delete from the Rear • Implementation of dequeue using array – https://www.programiz.com/dsa/deque 1/20/2023 28 Operations on a Dequeue
  • 29. • In undo operations on software. • To store history in browsers. • For implementing both stacks and queues. 1/20/2023 29 Dequeue Applications
  • 30. • A priority queue is a special type of queue in which each element is associated with a priority and is served according to its priority. • If elements with the same priority occur, they are served according to their order in the queue. 1/20/2023 30 Priority Queue
  • 31. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions PROGRAM TO IMPLEMENT SIMPLE QUEUE USING ARRAY #include <stdio.h> #include <stdlib.h> #define MAXSIZE 7 void display(int Queue[],int front,int rear) { int i; if(front==-1) { printf("Queue is Emptyn"); return; } for(i=front;i<=rear;i++) printf("%d ",Queue[i]); printf("n"); }
  • 32. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions FUNCTION : ENQUEUE OPERATION void enqueue(int Queue[],int *front,int *rear,int item) { if(*rear==MAXSIZE-1) { printf("Overflown"); return; } if(*front==-1) { *front=0; *rear=0; } else *rear=*rear+1; Queue[*rear]=item; display(Queue,*front,*rear); }
  • 33. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions FUNCTION: DEQUEUE OPERATION int dequeue(int Queue[],int *front,int *rear) { int x; if(*front==-1) { printf("Underflown"); return -1; } x=Queue[*front]; if(*front==*rear) *front=*rear=-1; else *front=*front+1; display(Queue,*front,*rear); return x; }
  • 34. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions MAIN() FUNCTION int main() { int Queue[MAXSIZE]; int front=-1,rear=-1; //display(Queue,front,rear); enqueue(Queue,&front,&rear,1); enqueue(Queue,&front,&rear,2); enqueue(Queue,&front,&rear,3); enqueue(Queue,&front,&rear,4); enqueue(Queue,&front,&rear,5); enqueue(Queue,&front,&rear,6); enqueue(Queue,&front,&rear,7); enqueue(Queue,&front,&rear,8); dequeue(Queue,&front,&rear);
  • 35. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions MAIN() FUNCTION dequeue(Queue,&front,&rear); dequeue(Queue,&front,&rear); dequeue(Queue,&front,&rear); dequeue(Queue,&front,&rear); dequeue(Queue,&front,&rear); dequeue(Queue,&front,&rear); dequeue(Queue,&front,&rear); return 0; }
  • 36. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions SIMPLE QUEUE SIMPLE QUEUE (LINKED LIST IMPLEMENTATION)
  • 37. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions PROGRAM TO IMPLEMENT SIMPLE QUEUE LINKED LIST #include <stdio.h> #include <stdlib.h> #include<limits.h> struct node { int data; struct node *next; }; struct node *Allocate(int x) { struct node *nn=(struct node *)malloc(sizeof(struct node)); if(nn==NULL) return NULL; nn->data=x; nn->next=NULL; return nn; };
  • 38. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions PROGRAM TO IMPLEMENT SIMPLE QUEUE LINKED LIST void display(struct node *fr,struct node *re) { struct node *t; if(fr==NULL) { printf("Queue is empty"); return; } for(t=fr;t!=re;t=t->next) { printf("%d -> ",t->data); } printf("%d ",t->data); }
  • 39. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions PROGRAM TO IMPLEMENT SIMPLE QUEUE LINKED LIST void enqueue(struct node **fr,struct node **re,int x) { struct node *nn=Allocate(x); if(nn==NULL) { printf("nOverflow"); return; } if(*fr==NULL) *fr=*re=nn; else { (*re)->next=nn; *re=nn; } }
  • 40. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions PROGRAM TO IMPLEMENT SIMPLE QUEUE LINKED LIST int dequeue(struct node **fr,struct node **re) { int x; struct node *temp; if(*fr==NULL) { printf("nUnderflow"); return INT_MIN; } temp=*fr; if(*fr==*re) *fr=*re=NULL; else *fr=(*fr)->next; x=temp->data; free(temp); return x; }
  • 41. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions PROGRAM TO IMPLEMENT SIMPLE QUEUE LINKED LIST int main() { struct node *front=NULL,*rear=NULL; int x; enqueue(&front,&rear,1); enqueue(&front,&rear,2); enqueue(&front,&rear,3); enqueue(&front,&rear,4); enqueue(&front,&rear,5); enqueue(&front,&rear,6); enqueue(&front,&rear,7); enqueue(&front,&rear,8); display(front,rear); x=dequeue(&front,&rear); if(x!=INT_MIN) printf("n%d is deleted",x);
  • 42. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions PROGRAM TO IMPLEMENT SIMPLE QUEUE LINKED LIST x=dequeue(&front,&rear); if(x!=INT_MIN) printf("n%d is deleted",x); x=dequeue(&front,&rear); if(x!=INT_MIN) printf("n%d is deleted",x); x=dequeue(&front,&rear); if(x!=INT_MIN) printf("n%d is deleted",x); x=dequeue(&front,&rear); if(x!=INT_MIN) printf("n%d is deleted",x); x=dequeue(&front,&rear); if(x!=INT_MIN) printf("n%d is deleted",x);
  • 43. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions PROGRAM TO IMPLEMENT SIMPLE QUEUE LINKED LIST x=dequeue(&front, &rear); if(x!=INT_MIN) printf("n%d is deleted", x); x=dequeue(&front, &rear); if(x!=INT_MIN) printf("n%d is deleted", x); x=dequeue(&front, &rear); if(x!=INT_MIN) printf("n%d is deleted", x); return 0; }
  • 44. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions CIRCULAR QUEUE CIRCULAR QUEUE (ARRAY IMPLEMENTATION)
  • 45. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions PROGRAM TO IMPLEMENT CIRCULAR QUEUE USING ARRAY #include <stdio.h> #include <stdlib.h> #include<limits.h> #define MAXSIZE 20 void display(int *Queue,int front, int rear) { int i; if(front==-1) { printf("Queue is emptyn"); return; } for(i=front;i!=rear;i=(i+1)%MAXSIZE) { printf("%d ",Queue[i]); } printf("%d",Queue[i]); }
  • 46. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions PROGRAM TO IMPLEMENT CIRCULAR QUEUE USING ARRAY void enqueue(int *Queue,int *front, int *rear,int x) { if(*front == (*rear+1)%MAXSIZE){ printf("Overflown"); return; } if(*front==-1) *front=*rear=0; else *rear=(*rear+1)%MAXSIZE; Queue[*rear]=x; }
  • 47. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions PROGRAM TO IMPLEMENT CIRCULAR QUEUE USING ARRAY int dequeue(int *Queue,int *front,int *rear) { int x; if(*front==-1) { printf("Underflown"); return INT_MIN; } x=Queue[*front]; if(*front==*rear) *front=*rear=-1; else *front=(*front+1)%MAXSIZE; return x; }
  • 48. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions PROGRAM TO IMPLEMENT CIRCULAR QUEUE USING ARRAY int main() { int Queue[MAXSIZE]; int front=-1,rear=-1,x; enqueue(Queue,&front,&rear,1); enqueue(Queue,&front,&rear,2); enqueue(Queue,&front,&rear,3); enqueue(Queue,&front,&rear,4); enqueue(Queue,&front,&rear,5); display(Queue,front,rear); x=dequeue(Queue,&front,&rear); if(x!=INT_MIN) printf("n%d is deletedn",x);
  • 49. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions PROGRAM TO IMPLEMENT CIRCULAR QUEUE USING ARRAY x=dequeue(Queue,&front,&rear); if(x!=INT_MIN) printf("%d is deletedn",x); x=dequeue(Queue,&front,&rear); if(x!=INT_MIN) printf("%d is deletedn",x); x=dequeue(Queue,&front,&rear); if(x!=INT_MIN) printf("%d is deletedn",x); x=dequeue(Queue,&front,&rear); if(x!=INT_MIN) printf("%d is deletedn",x); x=dequeue(Queue,&front,&rear); if(x!=INT_MIN) printf("%d is deletedn",x); return 0; }
  • 50. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions CIRCULAR QUEUE CIRCULAR QUEUE LINKED LIST IMPLEMENTATION
  • 51. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions PROGRAM TO IMPLEMENT CIRCULAR QUEUE LINKED LIST #include <stdio.h> #include <stdlib.h> #include<limits.h> struct node { int data; struct node *next; }; struct node *Allocate(int x) { struct node *nn=(struct node *)malloc(sizeof(struct node)); if(nn==NULL) return NULL; nn->data=x; nn->next=NULL; return nn; };
  • 52. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions PROGRAM TO IMPLEMENT CIRCULAR QUEUE LINKED LIST void display(struct node *fr,struct node *re) { struct node *t; if(fr==NULL) { printf("Queue is empty"); return; } for(t=fr;t!=re;t=t->next) { printf("%d -> ",t->data); } printf("%d ",t->data); }
  • 53. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions PROGRAM TO IMPLEMENT CIRCULAR QUEUE LINKED LIST void enqueue(struct node **fr,struct node **re,int x) { struct node *nn=Allocate(x); if(nn==NULL) { printf("nOverflow"); return; } if(*fr==NULL) { nn->next=nn; *fr=*re=nn; } else { nn->next=*fr; (*re)->next=nn; *re=nn; } }
  • 54. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions PROGRAM TO IMPLEMENT CIRCULAR QUEUE LINKED LIST int dequeue(struct node **fr,struct node **re) { int x; struct node *temp; if(*fr==NULL) { printf("nUnderflow"); return INT_MIN; } temp=*fr; if(*fr==*re) *fr=*re=NULL; else { *fr=(*fr)->next; (*re)->next=*fr; } x=temp->data; free(temp); return x; }
  • 55. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions PROGRAM TO IMPLEMENT CIRCULAR QUEUE LINKED LIST int main() { struct node *front=NULL,*rear=NULL; int x; enqueue(&front,&rear,1); enqueue(&front,&rear,2); enqueue(&front,&rear,3); enqueue(&front,&rear,4); enqueue(&front,&rear,5); enqueue(&front,&rear,6); enqueue(&front,&rear,7); enqueue(&front,&rear,8); display(front,rear); x=dequeue(&front,&rear); if(x!=INT_MIN) printf("n%d is deleted",x);
  • 56. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions PROGRAM TO IMPLEMENT CIRCULAR QUEUE LINKED LIST x=dequeue(&front,&rear); if(x!=INT_MIN) printf("n%d is deleted",x); x=dequeue(&front,&rear); if(x!=INT_MIN) printf("n%d is deleted",x); x=dequeue(&front,&rear); if(x!=INT_MIN) printf("n%d is deleted",x); x=dequeue(&front,&rear); if(x!=INT_MIN) printf("n%d is deleted",x); x=dequeue(&front,&rear); if(x!=INT_MIN) printf("n%d is deleted",x);
  • 57. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions PROGRAM TO IMPLEMENT CIRCULAR QUEUE LINKED LIST if(x!=INT_MIN) printf("n%d is deleted",x); x=dequeue(&front,&rear); if(x!=INT_MIN) printf("n%d is deleted",x); x=dequeue(&front,&rear); if(x!=INT_MIN) printf("n%d is deleted",x); return 0; }
  • 58. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions PRIORITY QUEUE PRIORITY QUEUE (ARRAY IMPLEMENTATION)
  • 59. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions PRIORITY QUEUE #include <stdio.h> #include <stdlib.h> #include<limits.h> #define MAXPR 5 #define MAXSIZE 5 int Q[MAXPR][MAXSIZE]; int front[MAXPR]; int rear[MAXPR];
  • 60. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions PRIORITY QUEUE void display() { int i,j; for(i=0;i<MAXPR;i++) { for(j=0;j<MAXSIZE;j++) { if(Q[i][j]==INT_MIN) printf(" "); else printf("%-2d ",Q[i][j]); } printf("Front[%d] = %2d, ",i,front[i]); printf("Rear[%d] = %2d ",i,rear[i]); printf("n"); } }
  • 61. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions PRIORITY QUEUE: ENQUEUE void enqueue(int val,int prn) { if(prn<0 || prn>=MAXPR) return; if(rear[prn]==MAXSIZE-1) { printf("Overflow"); return; } if(front[prn]==-1) { front[prn]++; } rear[prn]++; Q[prn][rear[prn]]=val; }
  • 62. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions PRIORITY QUEUE: DEQUEUE int dequeue() { int i,x; i=0; while(i<MAXPR && front[i]==-1) i++; if(i==MAXPR) { printf("nUnderflow"); return INT_MIN; } x=Q[i][front[i]]; Q[i][front[i]]=INT_MIN; if(front[i]==rear[i]){ front[i]=-1;rear[i]=-1; } else front[i]++; return x; }
  • 63. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions PRIORITY QUEUE: MAIN() int main() { int i,j,x; for(i=0;i<MAXPR;i++) { front[i]=-1; rear[i]=-1; for(j=0;j<MAXSIZE;j++) Q[i][j]=INT_MIN; } enqueue(5,1); enqueue(6,2); enqueue(7,1); enqueue(28,4); enqueue(5,2); enqueue(3,2); enqueue(23,0); display();
  • 64. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions PRIORITY QUEUE: MAIN() x=dequeue(); printf("nn%d is deletednn",x); display(); return 0; }
  • 65. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions PRIORITY QUEUE PRIORITY QUEUE LINKED LIST IMPLEMENTATION
  • 66. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions PRIORITY QUEUE #include <stdio.h> #include <stdlib.h> #include<limits.h> struct node { int data; int prn; struct node *next; }; struct node *Allocate(int val,int p) { struct node *nn=(struct node*)malloc(sizeof(struct node)); if(nn!=NULL) { nn->data=val; nn->prn=p; nn->next=NULL; } return nn; };
  • 67. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions PRIORITY QUEUE void display(struct node *he) { struct node *cur; if(he==NULL) { printf("Queue is empty"); return ; } for(cur=he;cur->next!=NULL;cur=cur->next) { printf("(%d,%d)-> ",cur->data,cur->prn); } printf("(%d,%d)",cur->data,cur->prn); }
  • 68. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions PRIORITY QUEUE void enqueue(struct node **he,int val,int p) { struct node *save,*cur; struct node *nn=Allocate(val,p); if(nn==NULL) { printf("Overflown"); return; } if(*he==NULL || p<(*he)->prn) { nn->next=*he; *he=nn; return; }
  • 69. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions PRIORITY QUEUE cur=*he; while(cur!=NULL && p>=cur->prn) { save=cur; cur=cur->next; } save->next=nn; nn->next=cur; }
  • 70. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions PRIORITY QUEUE int dequeue(struct node **he) { struct node *temp; int x; if(*he==NULL) { printf("nUnderflown"); return INT_MIN; } temp=*he; *he=(*he)->next; x=temp->data; free(temp); return x; }
  • 71. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions PRIORITY QUEUE int main() { struct node *head= NULL; int x; enqueue(&head,15,4); enqueue(&head,8,2); enqueue(&head,6,7); enqueue(&head,15,1); enqueue(&head,19,5); enqueue(&head,23,2); display(head); x=dequeue(&head); if(x!=INT_MIN) printf("n%d is deleted",x); x=dequeue(&head); if(x!=INT_MIN) printf("n%d is deleted",x);
  • 72. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions PRIORITY QUEUE x=dequeue(&head); if(x!=INT_MIN) printf("n%d is deleted",x); x=dequeue(&head); if(x!=INT_MIN) printf("n%d is deleted",x); x=dequeue(&head); if(x!=INT_MIN) printf("n%d is deleted",x); x=dequeue(&head); if(x!=INT_MIN) printf("n%d is deleted",x); x=dequeue(&head); if(x!=INT_MIN) printf("n%d is deleted",x); return 0; }
  • 73. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions DEQUE DOUBLE ENDED QUEUE (DEQUE) LINKED LIST IMPLEMENTATION (SIMPLE INPUT RESTRICTED DEQUE)
  • 74. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions INPUT RESTRICTED DEQUE #include <stdio.h> #include <stdlib.h> #include<limits.h> struct node { int data; struct node *next; }; struct node *Allocate(int x) { struct node *nn=(struct node *)malloc(sizeof(struct node)); if(nn==NULL) return NULL; nn->data=x; nn->next=NULL; return nn; };
  • 75. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions INPUT RESTRICTED DEQUE void display(struct node *fr,struct node *re) { struct node *t; if(fr==NULL) { printf("Queue is empty"); return; } for(t=fr;t!=re;t=t->next) { printf("%d -> ",t->data); } printf("%d ",t->data); }
  • 76. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions INPUT RESTRICTED DEQUE void enqueue_rear(struct node **fr,struct node **re,int x) { struct node *nn=Allocate(x); if(nn==NULL) { printf("nOverflow"); return; } if(*fr==NULL) { *fr=*re=nn; } else { (*re)->next=nn; *re=nn; } }
  • 77. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions INPUT RESTRICTED DEQUE int dequeue_front(struct node **fr,struct node **re) { int x; struct node *temp; if(*fr==NULL) { printf("nUnderflow"); return INT_MIN; } temp=*fr; if(*fr==*re) *fr=*re=NULL; else *fr=(*fr)->next; x=temp->data; free(temp); return x; }
  • 78. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions INPUT RESTRICTED DEQUE int dequeue_rear(struct node **fr,struct node **re) { int x; struct node *temp,*save,*cur; if(*fr==NULL) { printf("nUnderflow"); return INT_MIN; } temp=*re; if(*fr==*re) *fr=*re=NULL; else { cur=*fr; while(cur!=*re) { save=cur; cur=cur->next; }
  • 79. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions INPUT RESTRICTED DEQUE save->next=NULL; re=save; } x=temp->data; free(temp); return x; }
  • 80. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions INPUT RESTRICTED DEQUE int main() { int x; struct node *front=NULL,*rear=NULL; enqueue_rear(&front,&rear,1); enqueue_rear(&front,&rear,2); display(front, rear); x=dequeue_front(&front,&rear); if(x!=INT_MIN) printf("n%d is deleted",x); x=dequeue_rear(&front,&rear); if(x!=INT_MIN) printf("n%d is deleted",x); x=dequeue_front(&front,&rear); if(x!=INT_MIN) printf("n%d is deleted",x); return 0; }
  • 81. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions DEQUE DOUBLE ENDED QUEUE (DEQUE) LINKED LIST IMPLEMENTATION (SIMPLE OUTPUT RESTRICTED DEQUE)
  • 82. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions OUPUT RESTRICTED DEQUE #include <stdio.h> #include <stdlib.h> #include<limits.h> struct node { int data; struct node *next; }; struct node *Allocate(int x) { struct node *nn=(struct node *)malloc(sizeof(struct node)); if(nn==NULL) return NULL; nn->data=x; nn->next=NULL; return nn; };
  • 83. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions OUPUT RESTRICTED DEQUE void display(struct node *fr,struct node *re) { struct node *t; if(fr==NULL) { printf("Queue is empty"); return; } for(t=fr;t!=re;t=t->next) { printf("%d -> ",t->data); } printf("%d ",t->data); }
  • 84. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions OUPUT RESTRICTED DEQUE void enqueue_rear(struct node **fr,struct node **re,int x) { struct node *nn=Allocate(x); if(nn==NULL) { printf("nOverflow"); return; } if(*fr==NULL) { *fr=*re=nn; } else { (*re)->next=nn; *re=nn; } }
  • 85. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions OUPUT RESTRICTED DEQUE void enqueue_front(struct node **fr,struct node **re,int x) { struct node *nn=Allocate(x); if(nn==NULL) { printf("nOverflow"); return; } if(*fr==NULL) { *fr=*re=nn; } else { nn->next=*fr; *fr=nn; } }
  • 86. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions OUPUT RESTRICTED DEQUE int dequeue_front(struct node **fr,struct node **re) { int x; struct node *temp; if(*fr==NULL) { printf("nUnderflow"); return INT_MIN; } temp=*fr; if(*fr==*re) *fr=*re=NULL; else *fr=(*fr)->next; x=temp->data; free(temp); return x; }
  • 87. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions OUPUT RESTRICTED DEQUE int main() { int x; struct node *front=NULL,*rear=NULL; enqueue_rear(&front,&rear,1); enqueue_front(&front,&rear,2); display(front, rear); x=dequeue_front(&front,&rear); if(x!=INT_MIN) printf("n%d is deleted",x); x=dequeue_front(&front,&rear); if(x!=INT_MIN) printf("n%d is deleted",x); x=dequeue_front(&front,&rear); if(x!=INT_MIN) printf("n%d is deleted",x); return 0; }
  • 88. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions DEQUE DOUBLE ENDED QUEUE (DEQUE) LINKED LIST IMPLEMENTATION (CIRCULAR INPUT RESTRICTED DEQUE)
  • 89. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions INPUT RESTRICTED DEQUE #include <stdio.h> #include <stdlib.h> #include<limits.h> struct node { int data; struct node *next; }; struct node *Allocate(int x) { struct node *nn=(struct node *)malloc(sizeof(struct node)); if(nn==NULL) return NULL; nn->data=x; nn->next=NULL; return nn; };
  • 90. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions INPUT RESTRICTED DEQUE void display(struct node *fr,struct node *re) { struct node *t; if(fr==NULL) { printf("Queue is empty"); return; } for(t=fr;t!=re;t=t->next) { printf("%d -> ",t->data); } printf("%d ",t->data); }
  • 91. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions INPUT RESTRICTED DEQUE void enqueue_rear(struct node **fr,struct node **re,int x) { struct node *nn=Allocate(x); if(nn==NULL) { printf("nOverflow"); return; } if(*fr==NULL) { nn->next=nn; *fr=*re=nn; } else { nn->next=*fr; (*re)->next=nn; *re=nn; } }
  • 92. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions INPUT RESTRICTED DEQUE int dequeue_front(struct node **fr,struct node **re) { int x; struct node *temp; if(*fr==NULL) { printf("nUnderflow"); return INT_MIN; } temp=*fr; if(*fr==*re) *fr=*re=NULL; else { *fr=(*fr)->next; (*re)->next=*fr; } x=temp->data; free(temp); return x; }
  • 93. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions INPUT RESTRICTED DEQUE int dequeue_rear(struct node **fr,struct node **re) { int x; struct node *cur,*save; if(*re==NULL) { printf("nUnderflow"); return INT_MIN; } cur=*fr; if(*fr==*re) *fr=*re=NULL; else { cur=*fr; while(cur!=*re) { save=cur; cur=cur->next; }
  • 94. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions INPUT RESTRICTED DEQUE save->next=*fr; *re=save; } x=cur->data; free(cur); return x; }
  • 95. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions INPUT RESTRICTED DEQUE int main() { struct node *front=NULL,*rear=NULL; int x; enqueue_rear(&front,&rear,1); enqueue_rear(&front,&rear,2); enqueue_rear(&front,&rear,3); enqueue_rear(&front,&rear,4); enqueue_rear(&front,&rear,5); enqueue_rear(&front,&rear,6); display(front,rear); x=dequeue_rear(&front,&rear); if(x!=INT_MIN) printf("n%d is deleted",x); x=dequeue_front(&front,&rear); if(x!=INT_MIN) printf("n%d is deleted",x);
  • 96. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions INPUT RESTRICTED DEQUE x=dequeue_rear(&front,&rear); if(x!=INT_MIN) printf("n%d is deleted",x); x=dequeue_front(&front,&rear); if(x!=INT_MIN) printf("n%d is deleted",x); x=dequeue_rear(&front,&rear); if(x!=INT_MIN) printf("n%d is deleted",x); x=dequeue_front(&front,&rear); if(x!=INT_MIN) printf("n%d is deleted",x); x=dequeue_rear(&front,&rear); if(x!=INT_MIN) printf("n%d is deleted",x); return 0; }
  • 97. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions DEQUE DOUBLE ENDED QUEUE (DEQUE) LINKED LIST IMPLEMENTATION (CIRCULAR OUTPUT RESTRICTED DEQUE)
  • 98. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions OUTPUT RESTRICTED DEQUE #include <stdio.h> #include <stdlib.h> #include<limits.h> struct node { int data; struct node *next; }; struct node *Allocate(int x) { struct node *nn=(struct node *)malloc(sizeof(struct node)); if(nn==NULL) return NULL; nn->data=x; nn->next=NULL; return nn; };
  • 99. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions OUTPUT RESTRICTED DEQUE void display(struct node *fr,struct node *re) { struct node *t; if(fr==NULL) { printf("Queue is empty"); return; } for(t=fr;t!=re;t=t->next) { printf("%d -> ",t->data); } printf("%d ",t->data); }
  • 100. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions OUTPUT RESTRICTED DEQUE void enqueue_rear(struct node **fr,struct node **re,int x) { struct node *nn=Allocate(x); if(nn==NULL) { printf("nOverflow"); return; } if(*fr==NULL) { nn->next=nn; *fr=*re=nn; } else { nn->next=*fr; (*re)->next=nn; *re=nn; } }
  • 101. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions OUPUT RESTRICTED DEQUE void enqueue_front(struct node **fr,struct node **re,int x) { struct node *nn=Allocate(x); if(nn==NULL) { printf("nOverflow"); return; } if(*fr==NULL) { nn->next=nn; *fr=*re=nn; } else { nn->next=*fr; (*re)->next=nn; *fr=nn; } }
  • 102. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions OUPUT RESTRICTED DEQUE int dequeue_front(struct node **fr,struct node **re) { int x; struct node *temp; if(*fr==NULL) { printf("nUnderflow"); return INT_MIN; } temp=*fr; if(*fr==*re) *fr=*re=NULL; else { *fr=(*fr)->next; (*re)->next=*fr; } x=temp->data; free(temp); return x; }
  • 103. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions OUPUT RESTRICTED DEQUE int dequeue_rear(struct node **fr,struct node **re) { int x; struct node *cur,*save; if(*re==NULL) { printf("nUnderflow"); return INT_MIN; } cur=*fr; if(*fr==*re) *fr=*re=NULL; else { cur=*fr; while(cur!=*re) { save=cur; cur=cur->next; }
  • 104. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions OUPUT RESTRICTED DEQUE save->next=*fr; *re=save; } x=cur->data; free(cur); return x; }
  • 105. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions OUPUT RESTRICTED DEQUE int main() { struct node *front=NULL,*rear=NULL; int x; enqueue_rear(&front,&rear,1); enqueue_rear(&front,&rear,2); enqueue_rear(&front,&rear,3); enqueue_rear(&front,&rear,4); enqueue_rear(&front,&rear,5); enqueue_rear(&front,&rear,6); display(front,rear); x=dequeue_front(&front,&rear); if(x!=INT_MIN) printf("n%d is deleted",x); x=dequeue_front(&front,&rear); if(x!=INT_MIN) printf("n%d is deleted",x);
  • 106. NAGESH SHARMA, A.P. Department of CSE (AI), KIET Group of Institutions OUPUT RESTRICTED DEQUE x=dequeue_front(&front,&rear); if(x!=INT_MIN) printf("n%d is deleted",x); x=dequeue_front(&front,&rear); if(x!=INT_MIN) printf("n%d is deleted",x); x=dequeue_front(&front,&rear); if(x!=INT_MIN) printf("n%d is deleted",x); x=dequeue_front(&front,&rear); if(x!=INT_MIN) printf("n%d is deleted",x); x=dequeue_front(&front,&rear); if(x!=INT_MIN) printf("n%d is deleted",x); return 0; }