从队列打印的程序会打印一个数字

时间:2015-05-30 13:49:35

标签: c linked-list queue structure

所以这是我目前的代码。

#include<stdio.h>
#include<stdlib.h>
#include <string.h>

struct cake {
    char name;
    int waitTime;
    int prepTime;
    int bakeTime;
    int turnTime;
};

struct cake redVelvet(){
    struct cake c;
    c.name = "R";
    c.waitTime = 0;
    c.prepTime = 60;
    c.bakeTime = 30;
    c.turnTime = 0;
    return c;
};
struct Node {
    struct cake cake;
    struct Node* next;
};
// Two glboal variables to store address of front and rear nodes. 
struct Node* front = NULL;
struct Node* rear = NULL;

// To Enqueue an integer
void Enqueue(struct cake x) {
    struct Node* temp =
        (struct Node*)malloc(sizeof(struct Node));
    temp->cake = x;
    temp->next = NULL;
    if (front == NULL && rear == NULL){
        front = rear = temp;
        return;
    }
    rear->next = temp;
    rear = temp;
}

// To Dequeue an integer.
void Dequeue() {
    struct Node* temp = front;
    if (front == NULL) {
        printf("Queue is Empty\n");
        return;
    }
    if (front == rear) {
        front = rear = NULL;
    }
    else {
        front = front->next;
    }
    free(temp);
}

struct cake Front() {
    if (front == NULL) {
        printf("Queue is empty\n");
        return;
    }
    return front->cake;
}

void Print() {
    struct Node* temp = front;
    while (temp != NULL) {
        printf("%d ", temp->cake.name);
        temp = temp->next;
    }
    printf("\n");
}


int main(void){

    Enqueue(redVelvet());
    Enqueue(redVelvet());
    Print();
    getchar();

}

所以实际上我会有很多不同的蛋糕,如果某些条件意味着它们将被输入到LinkedList的队列中。然而,作为一个样本我创建了一种类型的蛋糕(红色天鹅绒)并通过我的排队功能将其添加到队列中然后尝试打印它,但是我的输出是&#34; 88 88&#34;

我希望它能打印出Cake的名称。

1 个答案:

答案 0 :(得分:2)

需要更改2行代码

c.name = 'R';

内部打印功能格式化程序类型应与变量类型

匹配
 printf("%c ", temp->cake.name);