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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘