YXTS122 2016-01-21 10:04 采纳率: 100%
浏览 1512
已采纳

这输出结果不对,怎么办?

 #include <stdio.h>
#include <stdlib.h>
typedef struct node
{
    int data;
    struct node *next;
}Node;

Node *createLink()
{
    Node *head=(struct node*)malloc(sizeof(struct node));
    Node *temp=head;
    int i=0;
    int a[10] = {1,2,3,5,7,9,10,14,15,20};
    while (i<10)
    {
        Node *n=(struct node*)malloc(sizeof(struct node));
        n->data = a[i];
        n->next = NULL;
        if(i==0)
        {
            head=temp=n;
        }
        else
        {
            temp->next = n;
            temp = n;
        }
        i++;
    }
    return head;
}

Node *insert(Node *head,int data)//就是这个函数
{
    Node *p=head,*pr=NULL,*pb=head;
    pr=(Node *)malloc(sizeof(struct node));
    if(pr==NULL)
    {
        printf("NO enough memory!\n");
        return head;
    }
    pr->data=data;
    pr->next=NULL;
    while(p->data<data&&p->next!=NULL)
    {
        pb=p;
        p=p->next;
    }
    if(p->data>=data)
    {
        if(p==head)
        {
            pr->next=head;
            head=pr;
        }
        else
        {
            p=pb;
            pr->next=p->next;
            p->next=pr;
        }

    }
    else
    {
        p->next=pr;
    }
    return head;
}


void Print(Node *head)
{
    head=head->next;
    while (head!=NULL)
    {
        printf("%d ", head->data);
        head=head->next;
    }
}

int main()
{
    Node *h1=NULL;
    h1=createLink();
    int d;
    scanf_s("%d", &d);
    h1=insert(h1,d);
    Print(h1);
    system("pause");
    return 0;
}

图片说明

  • 写回答

4条回答 默认 最新

  • 91program 博客专家认证 2016-01-21 10:12
    关注

    第一个问题:

    void Print(Node *head)
    {
        head=head->next;     // 会导致第一个元素不会输出
        while (head!=NULL)
        {
            printf("%d ", head->data);
            head=head->next;
        }
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建