鱼C论坛

 找回密码
 立即注册
查看: 3280|回复: 8

C语言教程中的结构体与共用体 05

[复制链接]
发表于 2018-2-22 13:51:51 From FishC Mobile | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 ??????? 于 2018-2-22 14:01 编辑

小甲鱼老师在这里写的是不是有点问题呀,已经删除的节点,为什么又出现了?求解答
6f3d131c17e494d0.png
-3234067e43e66fad.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-2-22 15:59:07 | 显示全部楼层
把代码发完整
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-2-22 19:35:59 From FishC Mobile | 显示全部楼层
人造人 发表于 2018-2-22 15:59
把代码发完整

#include<stdio.h>
#include<malloc.h>
#include<stdlib.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;

int main()
{
        struct student *stu, *p, *stu_2;
        int del_num;
        printf("input records!\n");
        stu=creat();
        p = stu;
        print(p);

        printf("\n");

        printf("please input the num to delete:  ");
        scanf("%d", &del_num);

    while(del_num!=0)        
        {
                p=del(p,del_num);
                print(p);
                printf("please input the num to delete:  ");
            scanf("%d", &del_num);

        }
        

    printf("\nplease input the record to insert:   \n");  
        stu_2=(struct student * )malloc(LEN);
        printf("please input the num : ");
        scanf("%d",&stu_2->num);
        printf("please input the score : ");
        scanf("%f",&stu_2->score);

    while(stu_2->num!=0)
        {
                p=insert(stu,stu_2);
            print(p);
        printf("\nplease input the record to insert:   \n");
                stu_2=(struct student * )malloc(LEN);
                printf("please input the num : ");
                scanf("%d",&stu_2->num);
                printf("please input the score : ");
                scanf("%f",&stu_2->score);
        }        
   
        printf("\n");
        system("pause");
}

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

        printf("please input the num : ");
        scanf("%d",&p1->num);
        printf("please input the score : ");
        scanf("%f",&p1->score);
    printf("\n");
        
        head=NULL;
        n=0;
        while(p1->num!=0)
        {
                n=n+1;
                if(n==1)
                {
                        head=p2;            
                }
                else
                {
                        p2->next=p1;                              
                }
                p2=p1;                                
                p1=(struct student * )malloc(LEN);
                printf("please input the num : ");
                scanf("%d",&p1->num);
                printf("please input the score : ");
                scanf("%f",&p1->score);
                printf("\n");
        }
        p2->next=NULL;

        return head;
}


void print(struct student *head)                    
{
        struct student *p;
        printf("\nThere are %d records!\n\n",n);

        p=head;
        if(head!=NULL)
        {
                do
                {
                        printf("学号: %d 分数: %f \n", p->num, p->score);
                        p=p->next;
                }while(p!=NULL);
        }

}


struct student *del(struct student *head, int num)      
  {
                struct student *p1, *p2;
        if(head==NULL)
        {
                printf("\nList null\n");
                goto end;
        }
    p1=head;
        
    while(num!=p1->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("delete: No. %d succeed!\n", num);
                n=n-1;                       
        }
        else
        {
                printf("%d not been found! \n", 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(head==NULL)
        {
                head=p0;
                p0->next=NULL;
        }
        else
        {  
                while( (p0->num > p1->num) && (p1->next!=NULL) )     //   两种情况退出 while
                {
                        p2=p1;
                        p1=p1->next;
                }
                if(p0->num <= p1->num)
                {
                        if(head==p1)
                        {
                                head=p0;
                        }
                        else
                        {
                                p2->next=p0;
                        }
                        p0->next=p1;
                }
                else              //p0的num最大,插在尾部
                {
                        p1->next=p0;
                        p0->next=NULL;
                }
                n=n+1;
                return head;
        }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-2-22 20:56:41 | 显示全部楼层
这代码是小甲鱼老师写的?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-2-22 21:53:15 From FishC Mobile | 显示全部楼层
人造人 发表于 2018-2-22 20:56
这代码是小甲鱼老师写的?

对呢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-2-22 21:54:52 | 显示全部楼层

哪一集,我去看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-2-23 16:13:04 From FishC Mobile | 显示全部楼层
人造人 发表于 2018-2-22 21:54
哪一集,我去看看

C语言教程 结构体与共用体05
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-2-23 18:25:22 | 显示全部楼层
??????? 发表于 2018-2-23 16:13
C语言教程 结构体与共用体05

这是旧版吧?
不要看旧版了,旧版有好多问题,看新版吧

http://blog.fishc.com/category/c
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-2-23 20:16:16 From FishC Mobile | 显示全部楼层
人造人 发表于 2018-2-23 18:25
这是旧版吧?
不要看旧版了,旧版有好多问题,看新版吧


谢谢哈
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-1 15:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表