Drof 2016-12-09 02:26 采纳率: 50%
浏览 1072
已采纳

大家帮忙看一下为什么会出错?

做的是一个带有头结点的单链表,思路是先在main中定义头结点,然后在create函数中构造首元结点,接着在insert函数中判断输出是否结束(以$作为结束标志)。

#include <stdio.h>
#include <malloc.h>
#define ElemType int
#define OVERFLOW 0
#define OK 1
typedef struct node{
    ElemType data;
    struct node *next;
}list;
int create(list *head){
    list *t;
    t=(list*)malloc(sizeof(list));
    if(!t){
        printf("\n分配空间失败!");
        return (OVERFLOW);
    }
    head = t;
    int x;
    printf("\nenter a number(end with $):");
    scanf("%d",&x);
    if(x!='$'){
        t=(list*)malloc(sizeof(list));
        t->data = x;
        t->next = NULL;
        head->next = t;
    }
    return OK;
}
int insert(list *head){
    list *t,*m,*n;//m和n表示相邻的两个节点,用于与节点比较大小,然后插入 
    int x;
    scanf("%d",&x);
    while(x!='$'){
        t=(list*)malloc(sizeof(list));
        if(!t) return(OVERFLOW);
        t->data = x;
        t->next = NULL;
        m = head->next;//这里调试出现错误 
        n = head; 
        if(t->data <= m->data){ 
            n->next = t;
            t->next = m;
        }
        else{
            while(1){
                m = m->next;
                n = n->next;
                if(t->data<=m->data){
                    n->next = t;
                    t->next = m;
                    break;
                }
                if(m->next==NULL) break;
            }
            if(m->next==NULL)
                m->next = t;
        }
        scanf("%d",x); 
    }
    return (OK);
}
int main(void){
    //创建一个有头结点的链表 
    printf("create a list");
    int i;
    list *head = NULL;
    i = create(head);
    if(i) insert(head);
    return 0; 
} 

然后我把create的返回值类型改成list*,返回head,然后再把main改一下就不出错了,这是为什么呢?
求大神解答,刚入门,轻喷- -
谢谢!

  • 写回答

7条回答 默认 最新

  • YXTS122 2016-12-11 05:03
    关注

    帮你又改了一下:

     #include <stdio.h>
    #include <malloc.h>
    #define ElemType int
    #define OVERFLOW 0
    #define OK 1
    typedef struct node
    { 
    ElemType data; 
    struct node *next;
    }list;
    list* create(list *head)
    {
        list *t; 
        t=(list*)malloc(sizeof(list));
        if(!t)
        { 
        printf("\n分配空间失败!"); 
        return (OVERFLOW); 
        }
        head = t; 
        int x; 
        printf("\nenter a number(end with $):"); 
        scanf("%d",&x);
         if(x!='$')
         {
            t=(list*)malloc(sizeof(list)); 
            t->data = x;
            t->next = NULL;
             head->next = t;
         }
         return head;
    }
    void insert(list *head)
    {
        list *t,*m,*n;
    //m和n表示相邻的两个节点,用于与节点比较大小,然后插入 
        int x;
        scanf("%d",&x); 
        while(x!=0)
        {
            t=(list*)malloc(sizeof(list)); 
            if(!t)
            return;
            t->data = x; 
            t->next = NULL; 
            m = head->next;
            //这里调试出现错误 
            n = head;
            if(t->data <= m->data)
            {
                n->next = t;
                t->next = m;
            }
            else
            {
                while(1)
                {
                    //你这里注意先判断一下就好了
                    if (m->next==NULL)
                         break;
                    m = m->next;
                    n = n->next; 
                    if(t->data<=m->data)
                    {
                        n->next = t;
                        t->next = m;
                        break; 
                    }
                }
            /*  if(m->next==NULL)
                     m->next=t;*/
                if(m->data<t->data) 
                   m->next = t; 
            }
            scanf("%d",&x);
       }
    }
    void output(list *head)
    {
        list *p;
        p=head->next;
        while (p)
        {
            printf("%d ",p->data);
            p=p->next;
        }
    }
    int main(void)
    {
         //创建一个有头结点的链表 
         printf("create a list");
         list *head = NULL;
         head = create(head);
         insert(head);
         output(head);
         return 0; 
    } 
    

    图片说明

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

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决