Note that there are some explanatory texts on larger screens.

plurals
  1. POPushing values to a Stack from a Queue
    primarykey
    data
    text
    <p>I am trying to push values in to a stack using a queue, but the thing is am not getting any pop from the stack( No output). Here is what i have done :</p> <pre><code>#include&lt;stdio.h&gt; #include&lt;malloc.h&gt; #include&lt;conio.h&gt; #define MAX 180 #define TRUE 1 #define FALSE 0 struct cakes { int spongecake; int meringue; int chocalate; int red_velvet; struct cakes *next; }; struct stack{ int top; int cake[10]; }; struct Queue{ int front; int rear; int count; int cake[10]; }; void conveyer_order(struct cakes *); void baking_order(struct cakes *); int isFull(struct stack *); int isEmpty(struct stack *); void push(struct stack *,int); int pop(struct stack *); void init(struct Queue *); int isqueueFull(struct Queue*); void insert(struct Queue*,int); int isqueueEmpty(struct Queue *); int removes(struct Queue *); main() { struct cakes *head; head=(struct cakes *)malloc(sizeof(struct cakes)); conveyer_order(head); head-&gt;next=(struct cakes *)malloc(sizeof(struct cakes)); baking_order(head-&gt;next); } int isFull(struct stack *k) { if(k-&gt;top==10-1) { return TRUE; } else { return FALSE; } } int isEmpty(struct stack *k) { if(k-&gt;top==-1) { return TRUE; } else { return FALSE; } } int pop(struct stack *sptr) { int ret=NULL; if(!isEmpty(sptr)) { ret=sptr-&gt;cake[sptr-&gt;top]; sptr-&gt;top--; return ret; } } void push(struct stack *sptr,int x) { if(!isFull(sptr)) { sptr-&gt;top++; sptr-&gt;cake[sptr-&gt;top]=x; } } void init(struct Queue *q) { q-&gt;front=0; q-&gt;rear=10-1; q-&gt;count=0; } int isqueueFull(struct Queue *q) { if(q-&gt;count==10) { return 1; } else { return 0; } } void insert(struct Queue *q,int x) { if(!isqueueFull(q)) { q-&gt;rear=(q-&gt;rear+1)%10; q-&gt;cake[q-&gt;rear]=x; q-&gt;count++; } } int isqueueEmpty(struct Queue *q) { if(q-&gt;count==0) { return 1; } else { return 0; } } int removes(struct Queue *q) { int cakeempty=NULL; if(!isqueueEmpty(q)) { cakeempty=q-&gt;cake[q-&gt;front]; q-&gt;front=(q-&gt;front+1)%10; q-&gt;count--; return cakeempty; } } void baking_order(struct cakes *theorder) { int v=0; struct stack baking; struct Queue belt; baking.top=-1; int value1=0; int value2=0; theorder-&gt;spongecake=20; theorder-&gt;chocalate=40; theorder-&gt;red_velvet=30; theorder-&gt;meringue=75; init(&amp;belt); while(!isqueueFull(&amp;belt)) { insert(&amp;belt,theorder-&gt;meringue); insert(&amp;belt,theorder-&gt;chocalate); insert(&amp;belt,theorder-&gt;red_velvet); insert(&amp;belt,(theorder-&gt;spongecake)); } value1=removes(&amp;belt); while(!isqueueEmpty(&amp;belt)) { while(!isFull(&amp;baking)) { value2=removes(&amp;belt); if(value1&gt;=value2) { push(&amp;baking,value2); value1=value2; } else { insert(&amp;belt,value2); } } } while(!isEmpty(&amp;baking)) { printf("\n%d",pop(&amp;baking)); } } </code></pre> <p>I tried printing the values without passing in to the stack and it works , i think the problem is with the 2 while loops. </p> <p>How can i fix this error ?</p> <p>Thank you for your time.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload