日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2025/10/17 编程问答 9 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据结构 结构的声明 一个结构作为另一个结构的成员 单向链表的实现 双向链表的实现 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

*******************************什么是結(jié)構(gòu)體********************************************************

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

#include<stdlib.h> #include<stdio.h> //什么是數(shù)據(jù)結(jié)構(gòu) //這里聲明了一個結(jié)構(gòu) horse //horse 不是一個變量名,是一個新的類型。 //horse 通常稱為 結(jié)構(gòu)標(biāo)記符(struct tag) 或 標(biāo)記符名稱(tag name) //結(jié)構(gòu)標(biāo)記符的命名方式和我們熟悉的變量名相同 名字最好不要相同/*結(jié)構(gòu)的定義 用分號結(jié)*/ struct horse { //結(jié)構(gòu)內(nèi)的成員類型:可以是任何類型的變量,包含數(shù)組 //成員的聲明:與變量的聲明一樣 先聲明類型 然后聲明名稱 然后分號結(jié)束。int age; //age hieght 稱為數(shù)據(jù)成員int height;char name[20];char father[20];char mother[20]; }Dobbin = { //Dobbin 是結(jié)構(gòu)變量 名稱24,17,"Dobbin","Trigger","Flossie" };/*未命名的結(jié)構(gòu)*/ //以下結(jié)構(gòu)沒有指定標(biāo)記符名稱。用一條語句聲明結(jié)構(gòu)和該結(jié)構(gòu)的實(shí)例時,可以省略標(biāo)記符名字。 struct{int age;int height;char name[20]; //缺點(diǎn):不能在其他語句中定義這個結(jié)構(gòu)的其他實(shí)例, //這個結(jié)構(gòu)的所有變量必須在一行語句中定義。 }Hei;//結(jié)構(gòu)的聲明 與 結(jié)構(gòu)變量的聲明分開 //上面的結(jié)構(gòu)中去掉Dobbin后為,定義結(jié)構(gòu)標(biāo)記符horse;下面聲明該類型的變量Jack /*結(jié)構(gòu)變量聲明語句 用分號結(jié)束*/ struct horse Jack = {12,23,"Jack","Ack","Jac"};/*訪問數(shù)據(jù)成員*/ //結(jié)構(gòu)變量名稱 .(成員選擇運(yùn)算符) 數(shù)據(jù)成員名稱 //Dobbin.age = 20; /*使用結(jié)構(gòu)*/ int main() {struct horse my_first_horse;printf("Enter the name of the Dobbin\n"); //&(尋址運(yùn)算符) 結(jié)構(gòu)成員name是一個數(shù)組,所以將數(shù)組的第一個元素的地址隱式傳送給函數(shù)scanfscanf("%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; }

*******************************一個結(jié)構(gòu)作為另一個結(jié)構(gòu)的成員**************************************

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

#include<stdio.h> //保存日期的結(jié)構(gòu) struct Date {int day;int month;int year; }; //定義結(jié)構(gòu)horse,其中包含出生日期變量。 struct horse { //結(jié)構(gòu)中的結(jié)構(gòu)的聲明:若Data的定義在horse內(nèi)部,在結(jié)構(gòu)體外使用data會報錯struct Date dob; //將一個結(jié)構(gòu)作為另一個結(jié)構(gòu)的成員int height;char name[20];char father[20];char mother[20]; }; //接下來用通常的語句定義一個horse結(jié)構(gòu)的實(shí)例 struct horse Dobbin;Dobbin.height = 20; Dobbin.dob.day = 5; Dobbin.dob.month = 12; Dobbin.dob.year = 1889;

****************************************單向鏈表的實(shí)現(xiàn)*******************************

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

#include<stdio.h> #include<ctype.h> #include<stdlib.h> //鏈表的特點(diǎn):內(nèi)存的使用和便于處理,存儲和處理鏈表所占用的內(nèi)存量少,即使所使用的內(nèi)存比較分散,也可以從一個結(jié)構(gòu)進(jìn)入到下一個結(jié)構(gòu)。鏈表數(shù)據(jù)處理的速度比較慢 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()函數(shù)返回的是一個viod指針,因此必須用表達(dá)式(struct horse *)將它轉(zhuǎn)換成所需要的類型,這樣這個指針就可正確的遞增或者遞減了。 current = (struct horse*)malloc(sizeof(struct horse));if(first == NULL)first = current; //如果有下一個結(jié)構(gòu),就必須將next指針指向這個結(jié)構(gòu),但只要有下一個結(jié)構(gòu),就可以確定其地址。 //因此,在第二次后續(xù)的迭代中,應(yīng)該將當(dāng)前結(jié)構(gòu)的地址存儲到前一個結(jié)構(gòu)的next成員中,前一個節(jié)后的地址存放在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指向的結(jié)構(gòu)中,next指針指定成NULL,表示這是最后一個結(jié)構(gòu),沒有下一個結(jié)構(gòu)了。 //指針的previous設(shè)定成current,然后進(jìn)入下一次迭代,此時current指向的數(shù)據(jù)結(jié)構(gòu)就是previous指向的結(jié)構(gòu)了。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; }

****************************************************雙向鏈表的實(shí)現(xiàn)************************************************

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

#include<stdio.h> #include<ctype.h> #include<stdlib.h> //鏈表的特點(diǎn):內(nèi)存的使用和便于處理,存儲和處理鏈表所占用的內(nèi)存量少,即使所使用的內(nèi)存比較分散,也可以從一個結(jié)構(gòu)進(jìn)入到下一個結(jié)構(gòu)。鏈表數(shù)據(jù)處理的速度比較慢 struct horse {int age;int height;char name[20];char father[20];char mother[20];struct horse *next;//pointer to next structurestruct 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;} //如果有下一個結(jié)構(gòu),就必須將next指針指向這個結(jié)構(gòu),但只要有下一個結(jié)構(gòu),就可以確定其地址。 //因此,在第二次后續(xù)的迭代中,應(yīng)該將當(dāng)前結(jié)構(gòu)的地址存儲到前一個結(jié)構(gòu)的next成員中,前一個節(jié)后的地址存放在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指向的結(jié)構(gòu)中,next指針指定成NULL,表示這是最后一個結(jié)構(gòu),沒有下一個結(jié)構(gòu)了。 //指針的previous設(shè)定成current,然后進(jìn)入下一次迭代,此時current指向的數(shù)據(jù)結(jié)構(gòu)就是previous指向的結(jié)構(gòu)了。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; }

?

總結(jié)

以上是生活随笔為你收集整理的数据结构 结构的声明 一个结构作为另一个结构的成员 单向链表的实现 双向链表的实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。