qq_34378013 2016-03-21 14:26 采纳率: 14.3%
浏览 1456

关于C语言链表插入的问题

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


struct node
{
    int num;
    int L;
    struct node *next;
};
typedef struct node LN;


LN *Creat(int n);
void print(LN *h);
void Revise(LN *p);


void main()
{
    LN *h;
    int a;
    printf("Input the sum of the number:");
    scanf("%d",&a);
    h=Creat(a);
    system("CLS");
    Revise(h);
    print(h);
}


LN *Creat(int n)
{
    LN *head;
    LN *p;
    LN *b;
    head=(LN *)malloc(sizeof(LN));
    head->next=NULL;
    b=head;
    for(int i=1;i<=n;i++)
    {
        printf("Input the %d number:",i);
        p=(LN *)malloc(sizeof(LN));
        scanf("%d",&p->num);
        b->next=p;
        b=p;
        p->L++;
    }
    system("CLS");
    p->next=NULL;
    return head;
}


void print(LN *h)
{
    LN *p;
    p=h->next;
    while(p)
    {
        printf("%d\n",p->num);
        p=p->next;
    }
}


void Revise(LN *p)
{
    LN *a;
    LN *b;
    int n;
    int i=0;
    int data;
    printf("Input which node you want to insert:");
    scanf("%d",&n);
    printf("Please input the data:");
    scanf("%d",&data);                                 
    while (i<n-1)
    {
        p=p->next;
        i++;
    }
    a=(LN *)malloc(sizeof(LN));
    b=(LN *)malloc(sizeof(LN));
    p->num=data;
    a=p->next;
    p->next=b;
    b->next=a;      
}


其中在链表插入当中插入错误,插入节点是对的,但是数就是错的。

  • 写回答

2条回答 默认 最新

  • threenewbee 2016-03-21 14:36
    关注
    
    void Revise(LN *p)
    {
        LN *a;
        LN *b;
        int n;
        int i=0;
        int data;
        printf("Input which node you want to insert:");
        scanf("%d",&n);
        printf("Please input the data:");
        scanf("%d",&data);                                 
        while (i<n)
        {
            p=p->next;
            i++;
        }
        a=(LN *)malloc(sizeof(LN));
        a->num=data;
        a->next=p->next;
        p->next=a;
    }
    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog