明仔小弟 发表于 2012-11-22 15:06:32

链表的问题,小甲鱼视频上的

# include<stdio.h>
# include<stdlib.h>
# define LEN sizeof(struct student)

struct student
{
        int num;
        int age;
        struct student *next;
};
int n;

struct student *creat()
{
        struct student *p1,*p2,*head;
        p1=p2=(struct student *)malloc(LEN);
        head=NULL;n=0;

        printf("input the num: ");
        scanf("%d",&p1->num);
        printf("input the age: ");
        scanf("%d",&p1->age);

        while(p1->num)
        {
                n++;
                if(n==1)
                {
                        head=p1;
                }
                else
                {
                        p2->next=p1;         
                }

                p2=p1;
                p1=(struct student *)malloc(LEN);

                printf("input the num: ");
                scanf("%d",&p1->num);
                printf("input the age: ");
                scanf("%d",&p1->age);
        }
       
        p2->next=NULL;
        return head;
}

void print(struct student *head)
{
        struct student *p;
        p=head;

        if(head)                     
        {
                do
                {
                        printf("%d,%d\n",p->num,p->age);
                        p=p->next;
                }while(p);
        }
}

struct student *del(struct student *head,int num)
{
        struct student *p1,*p2;
        p1=head;p2=NULL;
        while(p1->num!=num&&p1->next!=NULL)
        {
                p2=p1;
                p1=p1->next;
        }
        if(p1->num==num)
        {
                if(p1==head)         
                {
                        head=p1->next;
                }
                else
                {
                        p2->next=p1->next;
                }
        }
        else
        {
                printf("没找到");
        }
        return head;
}

struct student *insert(struct student *head,struct student *stu2)
{
        struct student *p1,*p2,*p0;
        p1=head;p2=NULL;p0=stu2;

        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(p1==head)   
                        {
                                head=p0;
                        }
                        else
                        {
                                p2->next=p0;
                        }
                        p0->next=p1;
                }
                else
                {
                        p1->next=p0;
                        p0->next=NULL;
                }
        }

        return head;
}

void main()
{
        struct student *stu,*p,stu2;
        stu=creat();
        p=stu;
        print(p);
        printf("\n");

        printf("你想删除哪一个呢:");
        scanf("%d",&n);
        print(del(p,n));
        printf("\n");

        printf("插入num:");
        scanf("%d",&stu2.num);
        printf("插入age:");
        scanf("%d",&stu2.age);

        p=insert(stu,&stu2);
        print(p);

        printf("\n");
       
}



这个程序运行时,删第一个节点再加上第一个就多出个内容,帮帮忙!!

明仔小弟 发表于 2012-11-22 15:33:51

我找到原因了,呵呵,好像小甲鱼上的视频有错误呢

紫竹人士 发表于 2012-11-23 09:56:26

正常,错误很正常,找出来就好

泳爸 发表于 2016-7-22 23:23:43

你找到啥错误啦

m9128213 发表于 2016-7-23 11:44:54

页: [1]
查看完整版本: 链表的问题,小甲鱼视频上的