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 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序