回 帖 发 新 帖 刷新版面

主题:动态链表的插入问题

#include<stdio.h>
#include<malloc.h>
#define NULL 0
struct student{
    int   num;
    float score;
    struct student *next;
};
main()
{
    void print(struct student *head);
    struct student *del(struct student *head,int num);
    struct student *insert(struct student *head,struct student *stu);
    struct student *head,*p1,*p2,*stu;
    int num,n=0;
    printf("建立一个动态链表,并输出.\n");
    p1=p2=(struct student *)malloc(sizeof(struct student));
    head=NULL;
    printf("Enter the num,score:");
    scanf("%d%f",&p1->num,&p1->score);
    while(p1->num!=0)
    {
        n++;
        if(n==1)
            head=p1;
        else
            p2=p1;
        p1=(struct student *)malloc(sizeof(struct student));
        p2->next=p1;
        printf("Enter the num,score:");
        scanf("%d%f",&p1->num,&p1->score);
    }
    p2->next=NULL;
    print(head);
    printf("输入要删除的学生学号:");
    scanf("%d",&num);
    while(num!=0)
    {
        head=del(head,num);
        print(head);
        printf("输入要删除的学生学号:");
        scanf("%d",&num);
    }
    printf("插入一个节点:\n");
    stu=(struct student *)malloc(sizeof(struct student));
    printf("Enter num,score:");
    scanf("%d%f",&stu->num,&stu->score);
    while(stu->num!=0)
    {
        head=insert(head,stu);
        print(head);
        printf("插入一个节点:\n");
        stu=(struct student *)malloc(sizeof(struct student));
        printf("Enter num,score:");
        scanf("%d%f",&stu->num,&stu->score);
    }
    return 0;
}
void print(struct student *head)
{
    struct student *p;
    int n=0;
    p=head;
    printf("These records are:\n");
    if(p==NULL)return;
        while(p!=NULL)
        {
            n++;
            printf("NO.%d: %.2f\n",p->num,p->score);
            p=p->next;
        }
        printf("These are %d dates!\n",n);
}
struct student *del(struct student *head,int num)
{
    struct student *p1,*p2;
    p1=p2=head;
    if(p1==NULL)return head;
    while(p1->num!=num)
    {
        if(p1->next==NULL)return head;
        else
        {
        p2=p1;
        p1=p1->next;
        }
    }
    if(p1->num==num)
    {
        if(p1==head)
        {
            if(p1->next!=NULL)head=p1->next;
            else head=NULL;
        }
        else
            p2->next=p1->next;
    }
    else
        printf("%d not been found!\n",num);
    return head;
}
struct student *insert(struct student *head,struct student *stu)
{
    struct student *p0,*p1,*p2;
    p1=head;
    p0=stu;
    if(head==NULL)
    {
        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(p1==head)head=p0;
            else p2->next=p0;
            p0->next=p1;
            }
        else
            {
                p1->next=p0;
                p0->next=NULL;
            }
        }
        return head;
}


检查了很多遍,插入函数也没有错,逻辑也没问题,就是不知道为什么输入插入的号码之后就press anykey了

回复列表 (共9个回复)

沙发

执行了del函数之后就不能插入 如果跳过del函数是可以插入的 不知道为什么 请高手指点

板凳

能不能把你的del函数执行流程解释一下?尤其是第1个while里面。

3 楼

struct student *del(struct student *head,int num)
{
    struct student *p1,*p2;  //定义结构体指针;
    p1=p2=head;   //p1,p2指向表头;
    if(p1==NULL)return head;  //如果链表为空 返回表头值 跳出函数;
    while(p1->num!=num)  //如果p1指向结点的号码不等于要删除的号码num,循环;
    {
        if(p1->next==NULL)return head;//如果只有一个结点 返回第一个结点的地址;
        else   //如果后面还有结点 把p1赋给p2,p1跳向下一个结点;
        {
        p2=p1;    
        p1=p1->next;
        }
    }
    if(p1->num==num)//如果找到要删除结点;
    {
        if(p1==head) //如果要删除的是头结点
        {
            if(p1->next!=NULL)head=p1->next;//如果后面还有结点,将下一个结点作为头结点
            else head=NULL;//如果后面没有结点 将头结点置空
        }
        else
            p2->next=p1->next;//要删除的如果不是头结点,将要删除结点的下个结点连到要删除结点的上一个结点;
    }
    else //如果没有找到要删除的结点;
        printf("%d not been found!\n",num);
    return head;//返回头结点的地址;
}

4 楼

没有问题啊!很好啊!

5 楼

fhxy@fhxy:~/c$ gcc -Wall ins.c -o ins
ins.c:3:1: 警告: “NULL”重定义
在包含自 /usr/include/malloc.h:25 的文件中,
                 从 ins.c:2:
/usr/lib/gcc/x86_64-linux-gnu/4.2.4/include/stddef.h:403:1: 警告: 这是先前定义的位置
ins.c:11: 警告: 返回类型默认为‘int’
fhxy@fhxy:~/c$ vi ins.c
fhxy@fhxy:~/c$ vi ins.c
fhxy@fhxy:~/c$ gcc -Wall ins.c -o ins
fhxy@fhxy:~/c$ ./ins
建立一个动态链表,并输出.
Enter the num,score:23 56
Enter the num,score:24 36 
Enter the num,score:25 37
Enter the num,score:0 23
These records are:
NO.23: 56.00
NO.24: 36.00
NO.25: 37.00
These are 3 dates!
输入要删除的学生学号:23
These records are:
NO.24: 36.00
NO.25: 37.00
These are 2 dates!
输入要删除的学生学号:24
These records are:
NO.25: 37.00
These are 1 dates!
输入要删除的学生学号:25
These records are:
输入要删除的学生学号:


我是用GCC调试的  
把#define NULL 0  去掉了  
main 返回为int 型

6 楼

完全没问题啊,把你的宏去掉后,在vc完全可以运行,添加删除操作都可以

7 楼

谢谢 不过确实还是有问题 比如删除了结点2 再插入结点2 就会显示结束;

8 楼

为什么不用STL中的list,要自己编程序呢

9 楼

代码,我不能说有什么过错,为什么不实现一个抽象的链表类来完成删除,插入等一些操作。

ADT描述
template<typename T>
classs ChaingNode
{
   public :
    ChainNode();
    Insert();
    Delete();
    Empty();
    Size();
};

搞这样一个抽象链表,你只要定义好一个结点,然就可以随便怎么做都行。。。。
希望这些话能给你一些启示。

我来回复

您尚未登录,请登录后再回复。点此登录或注册