ans will be n^2 ; where n is the no. of elements insterted in queue in descending order ,here n=16 so answer is 256 ,the complete working code for this including stacks nd queues is below:-
//author-ashwin.s.suthar software engg
//gate cs 2017;air 1880 gate cs 2018 all in top 10 rank final ans of given while loop total iteratons is 256 for 16 queue descending elememts it will result 16*16=256:iteraton
#include<stdlib.h>
#include<stdio.h>
#define max 500
#define size 500
struct stack
{
int s[size];
int top;
} st;
int pop();
void displaystk();
int stfull();
int top();
int head();
int gatewhile();
void push(int x);
int stempty();
int queue[max], front = 0, rear = 0;
int menu();
void enqueue();
void prenqueue(int x);
int dequeue();
void display();
int main()
{
int ch, outq = 0, outs = 0, e;
do
{
outq = 0;
printf("\nQueues using Arrays\n");
ch = menu();
switch (ch)
{
case 1:
enqueue();
break;
case 2:
e = dequeue();
break;
case 3:
display();
break;
case 4:
outq = 1;
break;
default:
printf("\n Please enter a valid choice for queues and then stacks will ru n next!!");
}
int item, choice;
char ans;
st.top = -1;
outs = 0;
printf("\n\tImplementation Of Stack");
printf("\nMain Menu");
printf
("\n1.Push \n2.Pop \n3.Display \n4.run the while loop of gate q 2016\n5.exit from stack& queue");
printf("\nEnter Your Choice");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("\nEnter The item to be pushed");
scanf("%d", &item);
if (stfull())
printf("\nStack is Full!");
else
push(item);
break;
case 2:
if (stempty())
printf("\nEmpty stack!Underflow !!");
else
{
item = pop();
printf("\nThe popped element is %d", item);
}
break;
case 3:
displaystk();
break;
case 4:
printf("\n final ans count of while loop is %d", gatewhile());
break;
case 5:
outs = 1;
break;
}
}
while ((outs + outq) < 2);
return 0;
}
int menu()
{
int ch;
printf("\n1.ENQUEUE \n2.DEQUEUE \n3.DISPLAY \n4goto stack and then exit");
printf("\nEnter your Choice:");
scanf("%d", &ch);
return ch;
}
void enqueue()
{
int element;
if (rear == max)
{
printf("\nOverflow!!");
}
else
{
printf("\nEnter Element:");
scanf("%d", &element);
queue[rear++] = element;
printf("\n %d Enqueued at %d", element, rear - 1);
}
}
void prenqueue(int x)
{
int element;
element = x;
if (rear == max)
{
printf("\nOverflow!!");
}
else
{
queue[rear++] = element;
printf("\n %d Enqueued at %d", element, rear - 1);
}
}
int dequeue()
{
if (rear == front)
{
printf("\nUnderflow!!");
}
else
{
front++;
printf("\n%d Element is Dequeued from %d", queue[front - 1], front - 1);
}
return queue[front - 1];
}
void display()
{
int i;
if (front == rear)
{
printf("\nQueue is Empty!!!");
}
else
{
printf(" \n");
for (i = front; i < max; i++)
{
printf(" | %d ", queue[i]);
}
printf("|");
}
}
int stfull()
{
if (st.top >= size - 1)
return 1;
else
return 0;
}
void push(int item)
{
st.top++;
st.s[st.top] = item;
printf("\n Item %d pushed at top %d ", item, st.top);
}
int stempty()
{
if (st.top == -1)
{
printf("\nstack is empty");
return 1;
}
else
{
printf("\nstack is non empty and topmost element %d and top %d", st.s[st.top], st.top);
return 0;
}
}
int pop()
{
int item;
item = st.s[st.top];
st.top--;
printf("\nItem popped is %d and current top is %d", item, st.top);
return (item);
}
void displaystk()
{
int i;
if (stempty())
printf("\nStack Is Empty!");
else
{
for (i = st.top; i >= 0; i--)
printf("\n%d", st.s[i]);
}
}
int top()
{
printf("\n topmost element is %d and top is %d", st.s[st.top], st.top);
if (st.top == -1)
return 0;
return st.s[st.top];
}
int head()
{
printf("\n element at front of queue is %d and front is %d", queue[front], front);
if (front == rear)
return 0;
return queue[front];
}
int gatewhile()
{
int c = 0, x;
while (rear != front) // queue is not empty by ashwi n code up
{
c++;
if (stempty() || (top() <= head()))
{
x = dequeue();
push(x);
}
else
{
x = pop();
prenqueue(x);
}
}
return c;
}