Я написал программу для очередей и распределения динамической памяти. Это то, что моя программа должна выполнить - вставьте значения в очередь и удалите их из очереди; так просто. Но моя проблема в том, что он просто печатает имена переменных, которым присваиваются значения, и программа не отвечает.Вывод программы Неверный вывод
Вот моя программа:
#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));
}
}
То, что кажется здесь проблема? Я новичок в C, поэтому при отладке на этом языке я немного медленнее.
Спасибо за ваше время.
Вот результат:
Является ли 'struct newcake'' typedef'? –
его указатель на новую структуру –
Тогда почему это так: 'head-> next = (struct cakes *) malloc (sizeof (struct cakes));' вместо 'head-> next = (struct newcake *) malloc (sizeof (struct newcake)); '?? Вы имели в виду 'struct cakes * next' вместо' struct newcake * next' ?? –