m0_63508730 2021-11-24 22:06 采纳率: 100%
浏览 19
已结题

想用链表写,链表初始元素五个以上就老是输出不全,求解

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

typedef struct node
{
int data;
struct node* Pnext;
}NODE,*PNODE;

PNODE creat();
void sort_insert();
void traverse();

int main(int argc, char** argv)
{
PNODE p = NULL;
p = creat();
sort_insert(p);
traverse(p);
return 0;
}

PNODE creat(void)
{
int len;
int i;
int val;
PNODE Phead = (PNODE)malloc(sizeof(NODE));
if (Phead==NULL)
{
printf("头节点分配失败");
exit(-1);
}
PNODE Ptail = Phead;
Ptail->Pnext = NULL;
scanf("%d",&len);
for (i=0;i<len;++i)
{
PNODE Pnew = (PNODE)malloc(sizeof(NODE));
if (Pnew==NULL)
{
printf("新节点分配失败");
exit(-1);
}
scanf("%d",&val);
Pnew->data = val;
Ptail->Pnext = Pnew;
Ptail = Pnew;
Ptail->Pnext = NULL;
}
return Phead;
}

void traverse (PNODE p)
{
//PNODE pp = p->Pnext;
while(p->Pnext!=NULL)
{
p = p->Pnext;
printf("%d ",p->data);
}
return;
}

void sort_insert(PNODE p)
{
int val;
int i;
scanf("%d",&val);
while(p->Pnext->data < val)
{
p = p->Pnext;
}
PNODE Pnew = (PNODE)malloc(sizeof(NODE));
if (Pnew==NULL)
{
printf("新节点分配失败");
exit(-1);
}
Pnew->data = val;
PNODE tp;

  p->Pnext = tp;
  p->Pnext = Pnew;
  Pnew->Pnext = tp;
  return;

}

  • ```

![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/667717267736136.jpg "#left")
  • 写回答

2条回答 默认 最新

  • 关注

    sort_insert函数有问题,代码修改如下:

    #include <stdio.h>
    #include <malloc.h>
    #include <stdlib.h>
    
    typedef struct node
    {
        int data;
        struct node* Pnext;
    }NODE,*PNODE;
    
    PNODE creat();
    void sort_insert(PNODE p);
    void traverse(PNODE p);
    
    int main(int argc, char** argv)
    {
        PNODE p = NULL;
        p = creat();
        sort_insert(p);
        traverse(p);
        return 0;
    }
    
    PNODE creat(void)
    {
        int len;
        int i;
        int val;
        PNODE Phead = (PNODE)malloc(sizeof(NODE));
        if (Phead==NULL)
        {
            printf("头节点分配失败");
            exit(-1);
        }
        PNODE Ptail = Phead;
        Ptail->Pnext = NULL;
        scanf("%d",&len);
        for (i=0;i<len;++i)
        {
            PNODE Pnew = (PNODE)malloc(sizeof(NODE));
            if (Pnew==NULL)
            {
                printf("新节点分配失败");
                exit(-1);
            }
            scanf("%d",&val);
            Pnew->data = val;
            Ptail->Pnext = Pnew;
            Ptail = Pnew;
            Ptail->Pnext = NULL;
        }
        return Phead;
    }
    
    void traverse (PNODE p)
    {
        //PNODE pp = p->Pnext;
        while(p->Pnext!=NULL)
        {
            p = p->Pnext;
            printf("%d ",p->data);
        }
        return;
    }
    
    void sort_insert(PNODE p)
    {
        int val;
        int i;
        PNODE pre = p;
        PNODE q = pre->Pnext;
        scanf("%d",&val);
    
        while(q && q->data < val)
        {
            pre = q;
            q = q->Pnext;
        }
        PNODE Pnew = (PNODE)malloc(sizeof(NODE));
        if (Pnew==NULL)
        {
            printf("新节点分配失败");
            exit(-1);
        }
        Pnew->data = val;
        pre->Pnext = Pnew;
        Pnew->Pnext = q;
        /*PNODE tp;
    
        p->Pnext = tp;
        p->Pnext = Pnew;
        Pnew->Pnext = tp;*/
        return;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月3日
  • 已采纳回答 11月25日
  • 创建了问题 11月24日

悬赏问题

  • ¥15 OCS2安装出现问题,请大家给点意见
  • ¥15 有没有大能能帮我出个适应度函数图,T_T
  • ¥60 爬虫登录易班工作站后台失败(相关搜索:登录页面)
  • ¥15 机器人关节变量求解问题
  • ¥15 ros小车启动launch文件报错
  • ¥15 vs2015到期想登陆但是登陆不上
  • ¥20 Intellij实现web登录界面
  • ¥15 IPQ5018制作烧录固件,boot运行失败(操作系统-linux)(相关搜索:操作系统)(相关搜索:操作系统)
  • ¥20 icefall在librispeech基础上加入个人数据集
  • ¥30 keepalive高可用故障运维配置询问