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.
Related
why i am getting stack is empty every time?
I am trying to make a expression tree from postfix here.
what's the logical error here?? plus we are not allowed to declare any variable globally. so, i had to pass the stackarray and node every time in each time function calling.
i am posting the full code done by me please have a look, i know its might be a simple error but as a beginner please show some kindness.
please help. TIA :)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAXSIZE 10
struct ExpnTreeNode
{
struct ExpnTreeNode *lchild;
char data;
struct ExpnTreeNode *rchild;
};
struct stackarray
{
struct ExpnTreeNode *data[MAXSIZE];
int top;
};
void push(struct ExpnTreeNode *newnode, struct stackarray *s);
struct ExpnTreeNode* pop( struct stackarray *s);
int check(char c);
void operand(struct ExpnTreeNode *newnode,char m, struct stackarray *s);
void operators(struct ExpnTreeNode *newnode,char m, struct stackarray *s);
void printex(struct ExpnTreeNode *node);
int main(void)
{
struct ExpnTreeNode *p=NULL;
struct stackarray s={{NULL},-1} ;
int len;
int x;
int res;
int i;
char arr[30]={'\0'};
printf("\n\tEnter a postfix expression:");
scanf("%d",&arr);
struct ExpnTreeNode *newnode=NULL;
len=strlen(arr);
for(i=0;arr[i]!='\0';i++)
{
x=check(arr[i]);
if(x==1)
{
operand(newnode,arr[i],&s);
}
else if(x==2)
{
operators(newnode,arr[i],&s);
}
}
p=pop(&s);
printex(p);
return 0;
}
void push(struct ExpnTreeNode *newnode, struct stackarray *s)
{
if(s->top==MAXSIZE-1)
{
printf("\n\tStack is Full");//Stack is Full
}
else
{
s->top=s->top+1;
s->data[s->top]=newnode;
}
}
struct ExpnTreeNode* pop( struct stackarray *s)
{
if(s->top==-1)
{
printf("\n\tStack is Empty");
return;
}
s->top=s->top-1;
return(s->data[s->top]);
}
int check(char c)
{
if(c=='*' || c=='/' || c=='+' || c=='-')
{
return 2;
}
else
{
return 1;
}
}
void operand(struct ExpnTreeNode *newnode,char m, struct stackarray *s)
{
struct stack *fs=NULL;
fs=s;
newnode=(struct ExpnTreeNode *)calloc(1,sizeof(struct ExpnTreeNode));
newnode->data=m;
newnode->lchild=NULL;
newnode->rchild=NULL;
push(newnode,&fs);
}
void operators(struct ExpnTreeNode *newnode,char m, struct stackarray *s)
{
newnode=(struct ExpnTreeNode *)calloc(1,sizeof(struct ExpnTreeNode));
struct stack *fs=NULL;
fs=s;
newnode->data=m;
newnode->rchild=pop(&fs);
newnode->lchild=pop(&fs);
push(newnode,&fs);
}
void printex(struct ExpnTreeNode *node)
{
if(node!=NULL)
{
printf("\n\t%c",node->data);
printex(node->lchild);
printex(node->rchild);
}
}
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;
}
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.
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.
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.