数据结构 结构的声明 一个结构作为另一个结构的成员 单向链表的实现 双向链表的实现
*******************************什么是結構體********************************************************
*****************************************************************************************************
#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是一個數組,所以將數組的第一個元素的地址隱式傳送給函數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; }*******************************一個結構作為另一個結構的成員**************************************
****************************************************************************************************
#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 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;} //如果有下一個結構,就必須將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; }?
總結
以上是生活随笔為你收集整理的数据结构 结构的声明 一个结构作为另一个结构的成员 单向链表的实现 双向链表的实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: centos 7 安装 mantisbt
- 下一篇: 结构体的嵌套 自身嵌套 相互嵌套