What would be the contents of queue Q1 after the following c
What would be the contents of queue Q1 after the following code is exe- cuted and the following data are entered?
1 Q1 = createQueue
2 S1 = createStack
3 loop (not end of file)
1 read number
2 if (number not 0)
1 pushStack (S1, number)
3 else
1 popStack (S1, x)
2 popStack (S1, x)
3 loop (not empty S1)
1 popStack (S1, x)
2 enqueue (Q1, x)
4 end loop
4 end if
4 end loop
The data are 5, 7, 12, 4, 0, 4, 6, 8, 67, 34, 23, 5, 0, 44, 33, 22, 6, 0.
This is a C data structure question. Please use C language, thank you very much!
Solution
#include <stdio.h>
#define MAX 50
void main()
{
int Stack[MAX];
int arr[MAX]={5,7,12,4,0,4,6,8,67,34,23,5,0,44,33,22,6,0};
int Queue[18];
int rear = - 1;
int front = - 1;
for(int i=0;i<arr.length;i++)
{
if(arr[i]!=0)
{
push(stack[],arr[i]);
}
else
{
pop(stack[]);
pop(stack[]);
while(!stack.empty())
{
int temp=pop(stack[]);
insert(temp);
}
}
}
}
void push (int stack[], int item)
{ if (top == (MAX-1))
status = 0;
else
{ status = 1;
++top;
stack [top] = item;
}
}
int pop (int stack[])
{
int ret;
if (top == -1)
{ ret = 0;
status = 0;
}
else
{ status = 1;
ret = stack [top];
--top;
}
return ret;
}
void insert(int temp1)
{
int add_item=temp1;
if (rear == MAX - 1)
printf(\"Queue Overflow \ \");
else
{
if (front == - 1)
/*If queue is initially empty */
front = 0;
printf(\"Insert the element in queue : \");
rear = rear + 1;
Queue[rear] = add_item;
}
}
Final Output of Queue after running above program : 7 5 34 67 8 6 4 33 44

