不显示c中的字符链表

  • 本文关键字:字符 链表 显示 c
  • 更新时间 :
  • 英文 :


程序是关于合并两个具有字符信息的排序链表。合并后的列表也应该进行排序。但是我的显示功能不是打印字符链表。函数在main中被调用,但没有打印字符。

struct node
{
    char info;
    struct node *link;
};
typedef struct node *NODE;

void Insert(NODE, char);
void Display(NODE);
NODE getNode();
int main()
{
    int num, i, c1, c2;
    char item;
    NODE head1, head2, head3;
    NODE temp1, temp2;
    head1=getNode();
    head2=getNode();
    head3=getNode();
    printf("First List:n");
    printf("Enter the number of elements: ");
    scanf ("%d", &num);
    printf ("Enter the elements in sorted order:n");
    for (i=0; i<num; i++)
    {
        scanf("%c", &item);
        getchar();
        Insert(head1, item);
    }
    printf("Second List:n");
    printf("Enter the number of elements: ");
    scanf ("%d", &num);
    printf ("Enter the elements in sorted order:n");
    for (i=0; i<num; i++)
    {
        scanf("%c", &item);
        getchar();
        Insert(head2, item);
    }
    temp1=head1->link;
    temp2=head2->link;
    while (temp1!=NULL&&temp2!=NULL)
    {
        c1=(int)temp1->info;
        c2=(int)temp2->info;
        if (c1<c2)
        {
            Insert(head3, temp1->info);
            temp1=temp1->link;
        }
        else
        {
            Insert (head3, temp2->info);
            temp2=temp2->link;
        }
    }
    while (temp1!=NULL)
    {
        Insert(head3, temp1->info);
        temp1=temp1->link;
    }
    while (temp2!=NULL)
    {
        Insert (head3, temp2->info);
        temp2=temp2->link;
    }
    Display(head3);
    return 0;
}
void Insert (NODE head, char item)
{
    NODE temp = getNode();
    NODE p;
    temp->info=item;
    temp->link=NULL;
    if (head->link==NULL)
        head->link=temp;
    else
    {
        p=head->link;
        while(p->link!=NULL)
            p=p->link;
        p->link=temp;
    }
}

void Display(NODE head)
{
    NODE temp;
    temp=head->link;
    if (head->link==NULL)
        printf("The list is empty.n");
    else
    {
        printf("The joined list is:n");
        while (temp!=NULL)
        {
            printf("%c t",temp->info);
            temp=temp->link;
        }
        printf("n");
    }
}
NODE getNode()
{
    NODE temp;
    temp=(NODE) malloc(sizeof(struct node));
    temp->link=NULL;
    return temp;
}

%c之前为您的两个scanf(...)放一个空格,用于读取字符

scanf(" %c", &item);

问题是%c不会像%f%d.等数字格式那样跳过空白

最新更新