数据结构 结构的声明 一个结构作为另一个结构的成员 单向链表的实现 双向链表的实现

本文详细介绍了C语言中结构体的基本概念、定义与使用方法,并通过实例展示了如何利用结构体构建单向链表和双向链表,以及链表的操作过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

*******************************什么是结构体********************************************************

*****************************************************************************************************

#include<stdlib.h>
#include<stdio.h>
//什么是数据结构
//这里声明了一个结构 horse
//horse 不是一个变量名,是一个新的类型。
//horse 通常称为 结构标记符(struct tag) 或 标记符名称(tag name)
//结构标记符的命名方式和我们熟悉的变量名相同 名字最好不要相同

/*结构的定义 用分号结*/
struct horse
{
//结构内的成员类型:可以是任何类型的变量,包含数组
//成员的声明:与变量的声明一样 先声明类型 然后声明名称 然后分号结束。
        int age;  //age hieght 称为数据成员
        int height;
        char name[20];
        char father[20];
        char mother[20];
}Dobbin = {      //Dobbin 是结构变量 名称
        24,17,"Dobbin","Trigger","Flossie"
};

/*未命名的结构*/
//以下结构没有指定标记符名称。用一条语句声明结构和该结构的实例时,可以省略标记符名字。
struct{
        int age;
        int height;
        char name[20];
//缺点:不能在其他语句中定义这个结构的其他实例,
//这个结构的所有变量必须在一行语句中定义。
}Hei;

//结构的声明 与 结构变量的声明分开
//上面的结构中去掉Dobbin后为,定义结构标记符horse;下面声明该类型的变量Jack
/*结构变量声明语句 用分号结束*/
struct horse Jack = {12,23,"Jack","Ack","Jac"};

/*访问数据成员*/
//结构变量名称 .(成员选择运算符) 数据成员名称
//Dobbin.age = 20; 

/*使用结构*/
int main()
{
        struct horse my_first_horse;
        printf("Enter the name of the Dobbin\n");
//&(寻址运算符) 结构成员name是一个数组,所以将数组的第一个元素的地址隐式传送给函数scanf
        scanf("%s",my_first_horse.name);
        printf("Enter the name of the Dobbin\n");
        scanf("%d",&my_first_horse.age);
        printf("%s is %d years old.\n",my_first_horse.name,my_first_horse.age);
        return 0;
}

*******************************一个结构作为另一个结构的成员**************************************

****************************************************************************************************

#include<stdio.h>
//保存日期的结构
struct Date
{
        int day;
        int month;
        int year;
};
//定义结构horse,其中包含出生日期变量。
struct horse
{
//结构中的结构的声明:若Data的定义在horse内部,在结构体外使用data会报错
        struct Date dob; //将一个结构作为另一个结构的成员
        int height;
        char name[20];
        char father[20];
        char mother[20];
};
//接下来用通常的语句定义一个horse结构的实例
struct horse Dobbin;

Dobbin.height  = 20;
Dobbin.dob.day = 5;
Dobbin.dob.month = 12;
Dobbin.dob.year = 1889;

****************************************单向链表的实现*******************************

***************************************************************************************

#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
//链表的特点:内存的使用和便于处理,存储和处理链表所占用的内存量少,即使所使用的内存比较分散,也可以从一个结构进入到下一个结构。链表数据处理的速度比较慢
struct horse
{
        int age;
        int height;
        char name[20];
        char father[20];
        char mother[20];
        struct horse *next;
};
struct horse *first = NULL;
struct horse *current = NULL;
struct horse *previous = NULL;
int main()
{
        char test = '\0';
        for( ; ; )
        {
                printf("Do you want to enter datails of a%s horse(Y or N)?",first != NULL?"nother":"");
                scanf(" %c",&test);
                if(tolower(test) == 'n')
                        break;
//malloc()函数返回的是一个viod指针,因此必须用表达式(struct horse *)将它转换成所需要的类型,这样这个指针就可正确的递增或者递减了。    
                current = (struct horse*)malloc(sizeof(struct horse));
                if(first == NULL)
                        first = current;
//如果有下一个结构,就必须将next指针指向这个结构,但只要有下一个结构,就可以确定其地址。
//因此,在第二次后续的迭代中,应该将当前结构的地址存储到前一个结构的next成员中,前一个节后的地址存放在previous指针中。
                if(previous != NULL)
                        previous->next = current;
                printf("\n Enter the name of the horse:");
                scanf("%s",current->name);

                printf("\nHow old is %s",current->name);
                scanf("%d",¤t->age);

                printf("\nHow height is %s (in hands)?",current->name);
                scanf("%d",¤t->height);

                printf("\nWho is %s`s father?",current->name);
                scanf("%s",current->father);

                printf("\nWho is %s`s mather?",current->name);
                scanf("%s",current->mother);
//在current指向的结构中,next指针指定成NULL,表示这是最后一个结构,没有下一个结构了。
//指针的previous设定成current,然后进入下一次迭代,此时current指向的数据结构就是previous指向的结构了。
                current->next = NULL;
                previous = current;
        }
        current = first;
        while(current != NULL)
        {
                printf("\n\n%s is %d years old,%d hands high,",current->name,current->age,current->height);
                printf(" and has %s and %s as parents.\n",current->father,current->mother);
                previous  = current;
                current = current->next;
                free(previous);
        }
        return 0;
}

****************************************************双向链表的实现************************************************

********************************************************************************************************************

#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
//链表的特点:内存的使用和便于处理,存储和处理链表所占用的内存量少,即使所使用的内存比较分散,也可以从一个结构进入到下一个结构。链表数据处理的速度比较慢
struct horse
{
        int age;
        int height;
        char name[20];
        char father[20];
        char mother[20];
        struct horse *next;//pointer to next structure
        struct horse *previous;//pointer to previous structure
};
struct horse *first = NULL;
struct horse *current = NULL;
struct horse *last = NULL;
int main()
{
        char test = '\0';
        for( ; ; )
        {
                printf("Do you want to enter datails of a%s horse(Y or N)?",first != NULL?"nother":"");
                scanf(" %c",&test);
                if(tolower(test) == 'n')
                        break;
                current = (struct horse*)malloc(sizeof(struct horse));
                if(first == NULL)
                {
                        first = current;
                        current->previous=NULL;
                }else{
                        last->next = NULL;
                        current->previous = last;
                }
//如果有下一个结构,就必须将next指针指向这个结构,但只要有下一个结构,就可以确定其地址。
//因此,在第二次后续的迭代中,应该将当前结构的地址存储到前一个结构的next成员中,前一个节后的地址存放在previous指针中。
                printf("\n Enter the name of the horse:");
                scanf("%s",current->name);

                printf("\nHow old is %s",current->name);
                scanf("%d",¤t->age);

                printf("\nHow height is %s (in hands)?",current->name);
                scanf("%d",¤t->height);

                printf("\nWho is %s`s father?",current->name);
                scanf("%s",current->father);

                printf("\nWho is %s`s mather?",current->name);
                scanf("%s",current->mother);
//在current指向的结构中,next指针指定成NULL,表示这是最后一个结构,没有下一个结构了。
//指针的previous设定成current,然后进入下一次迭代,此时current指向的数据结构就是previous指向的结构了。
                current->next = NULL;
                last = current;
        }
        while(current != NULL)
        {
                printf("\n\n%s is %d years old,%d hands high,",current->name,current->age,current->height);
                printf(" and has %s and %s as parents.\n",current->father,current->mother);
                last = current;
                current = current->previous;
                free(last);
        }
        return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值