Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions C/Reverse n elements of a given queue.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
int front=-1, rear=-1;
int array[SIZE];
void enqueue(int item);
int dequeue();

Above variables are used for queue, SIZE, front and rear and all are global variables. Complete the below two methods. */
#include<stdlib.h>
static int check=1,l=1;
void reverseQueue(int q[])
{
if(front<rear)
{
int x=dequeue();
l++;
reverseQueue(q);
enqueue(x);
}
}
void reverseKelementsQueue (int q[],int k)
{
if(k==0)
{
check=1;
l=1;
return;
}
if(check==1)
{
check=0;
reverseQueue(q);
k=l-k;
}
if(front<rear&&check==0)
{
int x=dequeue();
reverseKelementsQueue(q,k-1);
enqueue(x);
}
}