输出打印两次而不是一次

时间:2017-10-18 19:55:16

标签: c linked-list

我的代码是: -

/ *程序将数据存储在链接列表中,然后打印所有数据* /

#include <stdio.h>
#include <malloc.h>
struct value
{
    char data;
    struct value *next;
};

void main(void)
{
    struct value *head, *temp;
    char i;

    head = (struct value *) malloc (sizeof(struct value));

    temp = head;
    while (temp != NULL)
    {
        printf("Enter value to store in link list Enter z to stop\n");
        scanf("%c", &i);

        temp->data = i;
        if (i == 'z')
        {
            temp->next = NULL;
        }
        else
        {
            temp->next = (struct value *) malloc (sizeof(struct value));
        }
        temp = temp->next; 
    }

    temp = head;
    while (temp != NULL)
    {
        printf("Data is %c\n", temp->data);
        temp = temp->next;
    }

}

输出: -

输入要存储在链接列表中的值输入z以停止
一个
输入要存储在链接列表中的值输入z以停止
输入要存储在链接列表中的值输入z以停止
b
输入要存储在链接列表中的值输入z以停止
输入要存储在链接列表中的值输入z以停止
ç
输入要存储在链接列表中的值输入z以停止
输入要存储在链接列表中的值输入z以停止
ž

数据是一个 数据为

数据是b
数据为

数据是c 数据为

数据是z

4.95秒后退出流程,返回值为0 按任意键继续 。 。

请告诉我为什么要两次打印一份声明。代码有什么问题 感谢

0 个答案:

没有答案
相关问题