| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 599 人关注过本帖
标题:请教高手关于链表插入节点的问题
只看楼主 加入收藏
zd1505675319
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:139
专家分:178
注 册:2011-11-4
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:6 
请教高手关于链表插入节点的问题
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct Student)
struct Student
{    long num;
    float score;
    struct Student *next;
};
int n;
int main()
{    struct Student *creat();                                        //声明建立链表函数
    struct Student *del(struct Student *,long );                //声明删除节点函数
    struct Student *insert(struct Student *,struct Student *);    //声明插入节点函数
    void print(struct Student *);                                //声明输出链表函数
    struct Student *head,stu;            //定义头指针和 需要插入的节点变量
    long del_num;                        //定义要删除的 学号变量
    printf("input records:\n");            //输入记录   
    head=creat();                        //调用建立链表函数
    print(head);                        //调用输出链表函数 输出链表
    printf("input the deleted number:");//输入要删除的数;
    scanf("%ld",&del_num);
    head=del(head,del_num);                //调用删除学号函数 将头指针和 学号传给函数
    print(head);                        //输出删除数据后的链表
    printf("input the inserted record:");//输入要插入的记录
    scanf("%ld%f",&stu.num,&stu.score);
    head=insert(head,&stu);                //调用插入节点函数 注意形参是指针 实参也必须把地址传给函数
    print(head);                    //输出插入节点后的链表
    printf("input the inserted record:");//再输入一个要插入的记录   【【【【【】看这为什么如果再插入一个节点就会输出错误求高手指点】】】】】
    scanf("%ld%f",&stu.num,&stu.score);
    head=insert(head,&stu);                //调用插入节点函数 注意形参是指针 实参也必须把地址传给函数
    print(head);
    return 0;                        //主函数完成
}
// 定义建立链表函数
struct Student *creat()
{    struct Student *head,*p1,*p2;
    n=0;
    p1=p2=(struct Student *)malloc(LEN);        //先使p1和p2 指向新开辟的空间
    scanf("%ld%f",&p1->num,&p1->score);            //然后输入数据给p1
    head=NULL;                                    //是头指针为空
    while(p1->num!=0)                            //循环终止条件 p1==0
    {    n=n+1;                                       
        if(n==1)                                //判断n是否等于1 如果等于1 就说明链表刚开始
        {   
            head=p1;                            //将head指向 p1
        }
        else
        {   
            p2->next=p1;                        //如果n不等于1 p2->next指向p1
        }
        p2=p1;                                    //将p1的节点给p2
        p1=(struct Student *)malloc(LEN);        //p1继续开辟新节点
        scanf("%ld%f",&p1->num,&p1->score);        
    }
    p2->next=NULL;                    //输入为0时 跳出循环 使p2的下一节点为空
    return head;                    //返回头指针
}
//定义删除节点的函数
struct Student *del(struct Student *head,long num)
{    struct Student *p1,*p2;
    if(head==NULL)                    //判断头指针是否为空
    {
        printf("\nlise null!\n");
        return head;
    }
    p1=head;                        //如果不为空 使p1指向 head
    while(num!=p1->num&&p1->next!=NULL)//循环终止条件 找到 并且p1下个节点为空
    {   
        p2=p1;                        //p2指向p1
        p1=p1->next;                //p1指向p1的下个节点
    }
    if(num==p1->num)                //如果找到节点
    {
        if(p1==head)                //如果节点为头指针
        {   
            head=p1->next;            //使head 指向p1的下个节点(跳过p1)
        }
        else
        {
            p2->next=p1->next;        //跳过p1节点 指向p1 的下个节点
        }
        printf("delete:%ld\n",num);    //输出删除的节点的学号
        n=n-1;                        //全局变量 n-1
    }
    else                            //如果p1 为空 则没找到
    {
        printf("%ld not been found!\n",num);
    }
    return head;                //返回头指针
}
//定义插入节点函数
struct Student *insert(struct Student *head,struct Student *stud)
{    struct Student *p0,*p1,*p2;
    p1=head;
    p0=stud;
    if(head==NULL)
    {
        head=p0;
        p0->next=NULL;
    }
    else
    {
        while((p0->num>p1->num)&&(p1->next!=NULL))    //循环终止条件 p0->num 小于等于p1->num 或p1 下个节点为空
        {    p2=p1;            //p2指向p1
            p1=p1->next;    //p1指向p1 的下个节点
        }
        if(p0->num<=p1->num)
        {
            if(head==p1)    //判断如果是头指针
            {head=p0;}        //head指向p0        
            else
            {p2->next=p0;}    //如果不是则使 p2 的下个节点为p0
            p0->next=p1;    //p0 的下个节点指向p1
        }
        else
        {
            p1->next=p0;        //插到链表最后
            p0->next=NULL;
        }
    }
    n++;
    return head;
}
//定义输出链表函数
void print(struct Student *head)
{    struct Student *p1;
    printf("\nNow,These %d records are:\n",n);
    p1=head;
    if(head!=NULL)
    {    do
        {
            printf("%ld%5.1f\n",p1->num,p1->score);
            p1=p1->next;
        }
        while(p1!=NULL);
    }
}
搜索更多相关主题的帖子: 声明 函数 include insert 
2012-02-17 13:07
zd1505675319
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:139
专家分:178
注 册:2011-11-4
收藏
得分:0 
难道 真的没人回答么??急急急急!!!!!
2012-02-17 17:12
hnuhsg1226
Rank: 9Rank: 9Rank: 9
来 自:中国
等 级:蜘蛛侠
威 望:2
帖 子:314
专家分:1314
注 册:2011-3-27
收藏
得分:7 
https://bbs.bccn.net/thread-360862-2-1.html
参照下这个贴我贴的代码吧,不是不想改,确实要去改别人的代码确实需要时间,还是链表指针的问题

我的地盘
2012-02-17 17:18
zd1505675319
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:139
专家分:178
注 册:2011-11-4
收藏
得分:0 
    我想问的是这几段代码 为什么再次输入一个数据后 就会出错    (我重新输入一个数据后 就会死循环输出这个数据 这是为什么?)
   
    printf("input the inserted record:");//输入要插入的记录
    scanf("%ld%f",&stu.num,&stu.score);
    head=insert(head,&stu);                //调用插入节点函数 注意形参是指针 实参也必须把地址传给函数
    print(head);                    //输出插入节点后的链表
    printf("input the inserted record:");//再输入一个要插入的记录   【【【【【】看这为什么如果再插入一个节点就会输出错误求高手指点】】】】】
    scanf("%ld%f",&stu.num,&stu.score);
    head=insert(head,&stu);                //调用插入节点函数 注意形参是指针 实参也必须把地址传给函数
    print(head);
2012-02-17 17:38
zd1505675319
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:139
专家分:178
注 册:2011-11-4
收藏
得分:0 
没人啊。。。
2012-02-17 21:14
爱德华
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:2
帖 子:183
专家分:536
注 册:2011-5-29
收藏
得分:7 
  你插入的一个新节点没给它分配存储空间.

算法,数据结构,windows核心编程.
2012-02-18 18:53
snakelazixp
Rank: 2
等 级:论坛游民
帖 子:51
专家分:94
注 册:2012-2-13
收藏
得分:7 
printf("input the inserted record:");//再输入一个要插入的记录   【【【【【】看这为什么如果再插入一个节点就会输出错误求高手指点】】】】】
    scanf("%ld%f",&stu.num,&stu.score);
    head=insert(head,&stu);                //调用插入节点函数 注意形参是指针 实参也必须把地址传给函数
你此处的stu没有开辟内存空间。。。
2012-02-18 23:01
快速回复:请教高手关于链表插入节点的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.037566 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved