标题:求指教 链表插入数据的问题
只看楼主
dzdzdzd
Rank: 1
等 级:新手上路
帖 子:6
专家分:7
注 册:2017-11-28
结帖率:0
 问题点数:0 回复次数:0 
求指教 链表插入数据的问题
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include<malloc.h>
#define LEN sizeof(struct student)
struct student *creat();
struct student *del(struct student *head,int num);
struct student *insert(struct student *head,struct student *stu_2);
void print (struct student *head);
struct student
{
    int num;
    float score;
    struct student *next;
};
int n;
void main()
{
    struct student *stu ,*p,*stu_2;
    int n;
    stu=creat();
    stu_2=(struct student *)malloc(LEN);
    p=stu;
    print(p);
    printf("please enter the num to delete:");//删除
    scanf("%d",&n);
    print(del(p,n));
    printf("input the num to insert:");//添加
    scanf("%d",&stu_2->num);
    printf("input score:");
    scanf("%f",&stu_2->score);
    p=insert(stu,stu_2);
    print(p);
    printf("\n\n");
    system("pause");
}
struct student *creat()
{
    struct student *head;
    struct student *p1,*p2;
    p1=p2=(struct student *)malloc(LEN);
    printf("please enter the num:");
    scanf("%d",&p1->num);
    printf("please enter the score:");
    scanf("%f",&p1->score);
    head=NULL;
    n=0;
    while(p1->num)
    {
        n++;
        if(n==1)
        {
            head=p1;
        }
        else
        {
            p2->next=p1;
        }
        p2=p1;
        p1=(struct student *)malloc(LEN);
        printf("please enter the num:");
    scanf("%d",&p1->num);
    printf("please enter the score:");
    scanf("%f",&p1->score);
    }
    p2->next=NULL;
    return head;
}
void print(struct student *head)
{
    struct student *p;
    printf("\nthere are %d records\n",n);
    p=head;
    if(head)
    {
        do
        {
            printf("学号为%d的成绩是:%f\n",p->num,p->score);
            p=p->next;
        }while(p);
    }
}
struct student *del(struct student *head,int num)
{
    struct student *p1,*p2;
    if(NULL==head)
    {
        printf("this is a null\n");
            goto END;
    }
    p1=head;
    while(p1->num!=num&&p1->next!=NULL)
    {
        p2=p1;
        p1=p1->next;
    }
    if(num==p1->num)
    {
        if(p1==head)
        {
            head=p1->next;
        }
        else
        {
            p2->next=p1->next;
        }
        printf("\ndelete No:%d succeed!\n",num);
        n=n-1;
    }
    else
    {
        printf("%d 没有",num);
    }
END: return head;
}
struct student *insert(struct student *head,struct student *stu_2)
{
    struct student *p0,*p1,*p2;
    p1=head;
    p0=stu_2;
    if(NULL==head)
    {
        head=p0;
        p0->next=NULL;
    }
    else
    {
        while((p0->num>p1->num)&&(p1->next!=NULL))
        {
            p2=p1;
            p1=p1->next;
        }
        if(p0->num<=p1->num)
        {
            if(head==p1)
            {
                head=p0;
                p0->next=p1;
            }
            else
            {
                p2->next=p0;
                p0->next=p1;
            }
        }
        else
        {
            p1->next=p0;
            p0->next=NULL;
        }

    }
            n=n+1;
        return head;
}

功能实现为输出删除和插入数据,问题在删除一号数据后,然后重新录入一号的数据,此时 被删除的那个数据又会重新出现,求指教
搜索更多相关主题的帖子: struct student head num next 
2018-04-15 08:59



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-486071-1-1.html




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

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