qq_50827509 2021-08-30 22:45 采纳率: 91.5%
浏览 41
已结题

问题描述如图,想问下为什么这个代码不出结果啊?

img


#include <stdio.h>
#include<stdlib.h>
#include <string.h>
struct node
{
    int count;
    char data [20];
    struct node  *next;
};
struct node *create ()
{
    char str[20] ;
    struct node *head,*r, *s, *p;
    head=NULL;
    printf ("输入字符串(以stop标记结束) :\n") ;
    while(1)
    {
        scanf ("%s",str) ;
        if(strcmp(str, "stop")==0)
            break;
        p=head;
        while(p!=NULL&&strcmp(p->data,str)!=0)
            p=p->next;
        if(p!=NULL)
            p->count++;
        else
        {
            s=(struct node* )malloc(sizeof(node));
            s->count=1;
            strcpy(s->data,str);
            s->next=NULL;
            if(head=NULL)
            {
                head=s;
                r=s;
            }
            else
            {
                r->next=s;
                r=s;
            }
        }
    }
    return head;
}
void disp(struct node *p)
{
    if(p==NULL)
    printf("空表\n");
    else
        while(p!=NULL)
        {
            printf("%s(%d)  ",p->data,p->count);
            p=p->next;
        }
    printf("\n");
}
int main()
{
    struct node *p;
    p=create();
    printf("输出单链表:\n");
    disp(p);
    return 0;
}
  • 写回答

2条回答 默认 最新

  • qzjhjxj 2021-08-31 08:50
    关注

    题主的代码:#include<malloc.h>//包含  malloc.h 头文件

    第33行:if(head=NULL)  改为:if (head == NULL)  //少了 一个 ’=‘

     

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 9月8日
  • 已采纳回答 8月31日
  • 修改了问题 8月30日
  • 创建了问题 8月30日