Pushing values to a Stack from a Queue - c

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 :
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
#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->next=(struct cakes *)malloc(sizeof(struct cakes));
baking_order(head->next);
}
int isFull(struct stack *k)
{
if(k->top==10-1)
{
return TRUE;
}
else
{
return FALSE;
}
}
int isEmpty(struct stack *k)
{
if(k->top==-1)
{
return TRUE;
}
else
{
return FALSE;
}
}
int pop(struct stack *sptr)
{
int ret=NULL;
if(!isEmpty(sptr))
{
ret=sptr->cake[sptr->top];
sptr->top--;
return ret;
}
}
void push(struct stack *sptr,int x)
{
if(!isFull(sptr))
{
sptr->top++;
sptr->cake[sptr->top]=x;
}
}
void init(struct Queue *q)
{
q->front=0;
q->rear=10-1;
q->count=0;
}
int isqueueFull(struct Queue *q)
{
if(q->count==10)
{
return 1;
}
else
{
return 0;
}
}
void insert(struct Queue *q,int x)
{
if(!isqueueFull(q))
{
q->rear=(q->rear+1)%10;
q->cake[q->rear]=x;
q->count++;
}
}
int isqueueEmpty(struct Queue *q)
{
if(q->count==0)
{
return 1;
}
else
{
return 0;
}
}
int removes(struct Queue *q)
{
int cakeempty=NULL;
if(!isqueueEmpty(q))
{
cakeempty=q->cake[q->front];
q->front=(q->front+1)%10;
q->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->spongecake=20;
theorder->chocalate=40;
theorder->red_velvet=30;
theorder->meringue=75;
init(&belt);
while(!isqueueFull(&belt))
{
insert(&belt,theorder->meringue);
insert(&belt,theorder->chocalate);
insert(&belt,theorder->red_velvet);
insert(&belt,(theorder->spongecake));
}
value1=removes(&belt);
while(!isqueueEmpty(&belt))
{
while(!isFull(&baking))
{
value2=removes(&belt);
if(value1>=value2)
{
push(&baking,value2);
value1=value2;
}
else
{
insert(&belt,value2);
}
}
}
while(!isEmpty(&baking))
{
printf("\n%d",pop(&baking));
}
}
I tried printing the values without passing in to the stack and it works , i think the problem is with the 2 while loops.
How can i fix this error ?
Thank you for your time.

The problem is you only ever push 4 items onto the stack, so the stack is never full and your while(!isFull(&baking)) becomes an infinite loop.
Let's see if I can explain why:
Before you start the while loop, value1 is 75.
Then you read value2 as 40.
Check if(value1>=value2)? Yes, so you push 40 and set value1 to 40.
Then restart the loop, read value2 as 30.
Check if(value1>=value2)? Yes, so you push 30 and set value1 to 30.
Then restart the loop, read value2 as 20.
Check if(value1>=value2)? Yes, so you push 20 and set value1 to 20.
Then restart the loop, read value2 as 75.
Check if(value1>=value2)? No, so you put 75 back in the queue.
Then restart the loop, read value2 as 40.
Check if(value1>=value2)? No, so you put 40 back in the queue.
Then restart the loop, read value2 as 30.
Check if(value1>=value2)? No, so you put 30 back in the queue.
Then restart the loop, read value2 as 20.
Check if(value1>=value2)? Yes, so you push 20 and set value1 to 20.
At this point, you have pushed 40, 30, 20, 20.
However, everything left in the queue is either 30, 40 or 75 and if(value1>=value2) will never evaluate to true again.
Therefore your stack is never filled, and you never get out of the while loop.

Related

Binary heap using queue

There are lots of C examples by using an array to represent the tree.
However, I'm trying to use a tree(Nodes) for the Binary heap.
I was trying to add the child left and then right. In order to insert the elements in Width-first-Search, I've used a Queue. Is this necessary?
My Queue is using a Node* not a int type. Because, if I put a int in the Queue I must search the position of the Node(more time needed). Is this an appropriate way to built a Binary heap?
During the insertion, there are bubbling up and downs. However, since i have a binary heap and also a Queue. Do I have to match the orders in Queue also?
4.Do I need to search for inserting position like a BST? or do I approach to the address directly by using Queue?
I've learned the theory. However, the codes and structures are unfamiliar.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int left(int a,int b)
{
return (int)(a-floor((float)a/(float)b)*b);
}
typedef struct Node
{
int data;
struct Node* leftNext;
struct Node* rightNext;
}Node;
struct queue
{
Node** data;
int top;
int bottom;
int size;
}typedef Queue;
int push(Queue* Q,Node* input)
{
printf("PUSH : %d\n",input);
//한칸은 비울 것임.
if(left((*Q).top-(*Q).bottom,(*Q).size)==(*Q).size-1)
{
printf("큐가 다 찼습니다.\n");
return 0;
}
else
{
//큐의 크기를 넘으면 아래로 이동
if((*Q).top==(*Q).size)
(*Q).top=0;
printf("TOP : %d\n",(*Q).top);
(*Q).data[(*Q).top++]=input;
return 1;
}
}
Node* pop(Queue* Q)
{
if(left((*Q).top-(*Q).bottom,(*Q).size)==0)
printf("큐가 비었습니다.\n");
else
{
if((*Q).bottom==(*Q).size)
(*Q).bottom=0;
printf("BOTTOM : %d\n",(*Q).bottom);
return (*Q).data[(*Q).bottom];
}
}
typedef struct Heap
{
Node* head;
}Heap;
int insert(Node** head,int data,Queue* Q)
{
if((*head)!=NULL)
{
if((*head)->leftNext==NULL)
{
(*head)->leftNext=(Node*)malloc(sizeof(Node));
(*head)->leftNext->data=data;
(*head)->leftNext->leftNext=NULL;
(*head)->leftNext->rightNext=NULL;
push(Q,&(*head)->leftNext);
}
else if((*head)->rightNext==NULL)
{
(*head)->rightNext=(Node*)malloc(sizeof(Node));
(*head)->rightNext->data=data;
(*head)->rightNext->leftNext=NULL;
(*head)->rightNext->rightNext=NULL;
push(Q,&(*head)->rightNext);
(*Q).bottom++;
}
else
{
if(insert((pop(Q)),data,Q)==1)
return 1;
else
return 0;
}
}
else
{
printf("헤드가 비었습니다.");
return 0;
}
return 1;
}
int main()
{
//입력 15 6 12 7 10 17
Heap h;
//큐 초기화
Queue Q;
//큐 크기
int temp=100;
Q.data=(Node**)malloc(sizeof(Node*)*temp);
Q.size=temp;
Q.top=0;
Q.bottom=0;
h.head=(Node*)malloc(sizeof(Node));
h.head->data=15;
h.head->leftNext=NULL;
h.head->rightNext=NULL;
insert(&(h.head),6,&Q);
insert(&(h.head),12,&Q);
insert(&(h.head),7,&Q);
insert(&(h.head),10,&Q);
insert(&(h.head),17,&Q);
return 0;
}

Queue insertion based on condition

I am trying to insert values to a queue based on a condition. Condition is the value entered previously should be larger than the value which is going to be inserted. My problem is according to my data i have been provided with, one value is small than all the other values, so other values are not inserted in to the queue.
I am stuck here the whole day, without any clue.
Here is what i have done so far :
#include<stdio.h>
#include<malloc.h>
#define MAX 180
struct cakes{
int spongecake;
int meringue;
int chocalate;
int red_velvet;
struct newcake *next;
};
struct Queue{
int front;
int rear;
int count;
int cake[10];
};
void order_out(struct cakes *);
void init(struct Queue *);
int isFull(struct Queue *);
void insert(struct Queue *,int);
int isEmpty(struct Queue *);
int removes(struct Queue *);
main()
{
struct cakes *head;
head=(struct cakes*)malloc(sizeof(struct cakes));
order_out(head);
}
void init(struct Queue *q)
{
q->front=0;
q->rear=10-1;
q->count=0;
}
int isFull(struct Queue *q)
{
if(q->count==10)
{
return 1;
}
else
{
return 0;
}
}
void insert(struct Queue *q,int x)
{
if(!isFull(q))
{
q->rear=(q->rear+1)%10;
q->cake[q->rear]=x;
q->count++;
}
}
int isEmpty(struct Queue *q)
{
if(q->count==0)
{
return 1;
}
else
{
return 0;
}
}
int removes(struct Queue *q)
{
int caked=NULL;
if(!isEmpty(q))
{
caked=q->cake[q->front];
q->front=(q->front+1)%10;
q->count--;
return caked;
}
}
void order_out(struct cakes *theorder)
{
struct Queue s;
int k=0;
int i=1;
int value1;
int value2;
int counter=1;
theorder->spongecake=20;
theorder->meringue=75;
theorder->chocalate=40;
theorder->red_velvet=30;
init(&s);
insert(&s,theorder->chocalate);
value1=theorder->chocalate;
for(;k<10;k++)
{
if(value1>theorder->spongecake)
{
insert(&s,theorder->spongecake);
value1=theorder->spongecake;
}
if(value1>theorder->meringue)
{
insert(&s,theorder->meringue);
value1=theorder->meringue;
}
if(value1>theorder->red_velvet)
{
insert(&s,theorder->red_velvet);
value1=theorder->red_velvet;
}
}
while(!isEmpty(&s))
{
printf("\n%d",removes(&s));
}
}
Any idea on how to make the other values entered in to the queue but also at the same time check whether the previous value is larger than the current value.
EDIT :
I tried adding a count meaning if the count % value1==0, then i tried to make the value1 into a bigger value by multiplying by 5 but still only those 2 values got inserted.
Thank you for your time.

Wrong output when popping from the stack

I have made a program using stacks, but the output which i get is kind of wrong. The output i get, has the answer i need and also has 2 values which are not expected.
Here is what i have done:
#include <stdio.h>
#include<malloc.h>
#define MAX 180
struct cakes{
int spongecake;
int meringue;
int chocalate;
int red_velvet;
struct cakes *next;
};
struct stack{
int top;
int cake[10];
};
int isFull(struct stack *);
int isEmpty(struct stack *);
void push(struct stack *,int);
int pop(struct stack *);
void order_out(struct cakes *);
main()
{
struct cakes *head;
head=(struct cakes *)malloc(sizeof(struct cakes));
cake_order(head); //this is a seperate function, it works perfectly.
head->next=(struct cakes *)malloc(sizeof(struct cakes));
order_out(head->next);
}
int isFull(struct stack *q)
{
if(q->top==10-1)
{
return 1;
}
else
{
return 0;
}
}
void push(struct stack *sptr,int x)
{
if(!isFull(sptr))
{
sptr->top++;
sptr->cake[sptr->top]=x;
}
}
int isEmpty(struct stack *q)
{
if(q->top==-1)
{
return 1;
}
else
{
return 0;
}
}
int pop(struct stack *sptr)
{
int ret=NULL;
if(!isEmpty(sptr))
{
ret=sptr->cake[sptr->top];
sptr->top--;
return ret;
}
}
void order_out(struct cakes *theorder)
{
struct stack s;
s.top=-1;
int k=0;
int i=0;
int p=0;
int r=0;
int value1,value2;
int items[10];
theorder->spongecake=1;
theorder->meringue=2;
theorder->chocalate=3;
theorder->red_velvet=4;
for(;i<10;i++)
{
push(&s,theorder->spongecake);
push(&s,theorder->meringue);
push(&s,theorder->chocalate);
push(&s,theorder->red_velvet);
}
while(!isEmpty(&s))
{
printf("\n%d",pop(&s));
}
}
The output which I get is as follows :
As you can see it prints 2 and 1 first. What seems to be the problem?
In my opinion your program is working as follows,
The following loop tries to insert 40 values,
for(;i<10;i++)
{
push(&s,theorder->spongecake);
push(&s,theorder->meringue);
push(&s,theorder->chocalate);
push(&s,theorder->red_velvet);
}
But only 10 values are inserted because of your statement if(q->top==10-1) in your isFull() function. i.e the counter counts 10 elements from 0 to 9. The order in which the elements are pushed is as follows 1, 2, 3, 4, 1, 2, 3, 4, 1, 2. These elements when popped gives you the sequence 2, 1, 4, 3, 2, 1, 4, 3, 2, 1. Thus the output you obtain is actually correct or at least not an aberration.
There are a few more issues I would like to point out,
The function pop() should be as follows,
int pop(struct stack *sptr)
{
int ret=NULL;
if(!isEmpty(sptr))
{
ret=sptr->cake[sptr->top];
sptr->top--;
return ret;
}
return 0;
}
Otherwise the function returns random values when sptr is empty.
The statement if(q->top==10-1) should be if(q->top==9).
There is no problem with stack except the pop implementation where return value NULL is returned when stack is empty. Better return an invalid number as convention(like 0, -1, -999) left to your choice.
As you are restricting the stack size to 10, only 10 values are being inserted. So while inserting only following are inserted 1 2 3 4 1 2 3 4 1 2 and are popped inorder.

Program produce Wrong output

I have written a program on queues and dynamic memory allocation. This is what my program needs to do - insert values in to the queue and remove it from the queue; that simple.
But my problem is that it just prints the names of the variables the values are assigned to and the program goes not responding.
Here is my program :
#include <stdio.h>
#define MAX 180
struct cakes{
int spongecake;
int meringue;
int chocalate;
int red_velvet;
struct newcake *next;
};
struct Queue{
int front;
int rear;
int count;
int cake[10];
};
void init(struct Queue *);
int isFull(struct Queue *);
void insert(struct Queue *,int);
int isEmpty(struct Queue *);
int removes(struct Queue *);
void cake_order(struct cakes *);
void order_out(struct cakes *);
main()
{
struct cakes *head;
head=(struct cakes *)malloc(sizeof(struct cakes ));
cake_order(&head); //this is a seperate function and it works perfectly
head->next=(struct cakes *)malloc(sizeof(struct cakes));
order_out(&head->next);
}
void init(struct Queue *q)
{
q->front=0;
q->rear=10-1;
q->count=0;
}
int isFull(struct Queue *q)
{
if(q->count==10)
{
return 1;
}
else
{
return 0;
}
}
void insert(struct Queue *q,int x)
{
if(!isFull(q))
{
q->rear=(q->rear+1)%10;
q->cake[q->rear]=x;
q->count++;
}
}
int isEmpty(struct Queue *q)
{
if(q->count==0)
{
return 1;
}
else
{
return 0;
}
}
int removes(struct Queue *q)
{
int caked=NULL;
if(!isEmpty(q))
{
caked=q->cake[q->front];
q->front=(q->front+1)%10;
q->count--;
return caked;
}
}
void order_out(struct cakes *order)
{
struct Queue s;
int i;
order->spongecake=20;
order->meringue=75;
order->chocalate=40;
order->red_velvet=30;
init(&s);
for(i=0;i<10;i++)
{
insert(&s,order->chocalate);
insert(&s,order->spongecake);
insert(&s,order->meringue);
insert(&s,order->red_velvet);
}
while(!isEmpty(&s))
{
printf("%d",removes(&s));
}
}
What seems to be the problem here?
I am new to C, so yea am a bit slow when debugging in this language.
Thank you for your time.
Here is the output:
Lots of problems here, first it would be better if main was declared properly as in int main() and then it returned a value at the end e.g. return 0; like:
int main()
{
.... // code
return 0; // normally 0 is returned if execution has been successful
}
There seem to be other problems with the code as I wasn't able to compile it, for example there's no closing brace at the end of order_out() (right after the while loop).
Also would be good if you provided the cake_order() function.
It's also missing the includes for say stdlib.h, and on line 45 (head=(struct cakes *)malloc(sizeof(struct cakes ));) I've noticed you cast the result of malloc, which is not necessary.
And if I may further add, don't remember to free() the memory you've allocated with malloc(). I didn't see a single free() statement in your code.

Unable to sort out an circular queue ? what is wrong?

I am trying to implement a circular queue that takes a struct
⟶ checks if the queue is full if not enqueues ⟶ else return 0
⟶ checks if the queue is empty if not dequeues ⟶ else return 0
The following is my code :
#ifndef_QUEUE_H_
#define QUEUE_H_
#define QUEUE_SIZE 32
typedef struct queue
{
int head;
int tail;
int buffer[QUEUE_SIZE];
} queue_t;
void init_queue(queue_t *);
int enqueue(queue_t *, int);
int isfull(queue_t *);
int dequeue(queue_t *);
int queue_empty(queue_t *);
#endif
/* queue.h ends here */
/* *************************queue.c starts here************************ */
#include<queue.h>
#include<stm32f30x.h>
#define QUEUE_SIZE 32
int head;
int tail;
void init_queue(queue_t *q)
{
q->head = 0;
q->tail = 0;
}
int enqueue(queue_t *q, int a )
{
if ((((q->head)+1)%QUEUE_SIZE)!=q->tail)
{
q->buffer[q->head]=a;
q->head=(((q->head)+1)%QUEUE_SIZE);
return 1 ;
}
else
{
return 0 ;
}
}
int dequeue(queue_t *q)
{
int temp ;
if (queue_empty(q))
{
return 0;
}
else
{
temp = q->buffer[q->tail];
q->tail=((q->tail)+1)%QUEUE_SIZE;
return temp;
}
}
int queue_empty(queue_t *q)
{
if (q->head == q->tail)
{
return 1;
}
return 0;
}
/* **************queue.c ends here********************** */
When I try to test the queue: example enter "hello" and try to print the content inside it it prints "ello hlo elo.." and such incorrect replies.
I am using the enqueue and dequeue function in my getchar and putchar functions.
Below are my getchar and putchar functions:
queue_t rxbuf;
queue_t txbuf;
int putchar(int c)
{
while(!(enqueue(&txbuf, c)));
}
int nonblockgetchar(void)
{
dequeue(&rxbuf);
}
int getchar(void)
{
int ch;
while (!(ch=dequeue(&rxbuf)));
return (ch);
}
Appreciate your time and help.

Resources