国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

鏈表

2019-11-14 11:47:34
字體:
供稿:網(wǎng)友

一、鏈表特點

鏈表是由許多相同數(shù)據(jù)類型的數(shù)據(jù)項按特定排列順序排列而成的線性表。特性是其各個數(shù)據(jù)項在內(nèi)存中的排列是不連續(xù)且隨機存放的,需要”動態(tài)分配內(nèi)存“時,最適合鏈表的結(jié)構(gòu)設(shè)計,可以讓內(nèi)存運用更具有彈性。

在C語言中,動態(tài)分配內(nèi)存主要使用malloc()與free()函數(shù),定義于頭文件stdlib.h文件中。舉例如下:

#include <stdio.h>#include <stdlib.h>#include <string.h> int main(){ char *str1="Hello World!"; char* str2=(char*)malloc(sizeof(char)*(strlen(str1))); /* 動態(tài)分配與str1相同大小的內(nèi)存空間 */ strcpy(str2,str1);/* 將str1字符串復(fù)制到str2字符串 */ C++ 中的動態(tài)分配變量,使用new等關(guān)鍵字獲取內(nèi)存地址,用delete釋放內(nèi)存。代碼片段舉例如下:

int* m=new int;*m=50;cout<<"當(dāng)前指針m所指向的地址:"<<m<<endl;delete m;cout<<"執(zhí)行delete m 后指針m指向的地址:"<<m<<endl;

二、單向鏈表

一個單向鏈表由兩個元素組成,數(shù)據(jù)字段和指針,指針則指向下一個元素在內(nèi)存中的地址。 接下來是一段建立學(xué)生節(jié)點單向鏈表的算法

typedef struct student s_data;s_data *ptr; //
access pointer s_data *head;//Chain table pointers_data *new_data;//Pointer to the location of the new elementhead=(s_data*)malloc(sizeof(s_data));ptr=head;ptr->next=NULL;do{ printf("name IDnumber score: "); scanf("%s %s %d",ptr->name,ptr->no,&ptr->score); new_data=(s_data*)malloc(sizeof(s_data));//Add new element ptr->next=new_data; new_data->next=NULL; ptr=ptr->next;}

三、遍歷單向鏈表

即使用指針運算訪問鏈表中的每個節(jié)點。

#include <stdio.h>#include <stdlib.h>int main(){ int select,student_no=0,num=0; float Msum=0,Esum=0; struct student { char name[20]; int Math; int Eng; char no[10]; struct student *next; }; typedef struct student s_data; s_data *ptr; /* 存取指針 */ s_data *head; /* 鏈表頭指針 */ s_data *new_data; /* 新增元素所在位置的指針 */ head = (s_data*) malloc(sizeof(s_data)); /* 建立鏈表頭 */ head->next=NULL; ptr = head; do { printf("(1)新增 (2)離開 =>"); scanf("%d", &select); if (select != 2) { printf("姓名 學(xué)號 數(shù)學(xué)成績 英語成績:"); new_data = (s_data*) malloc(sizeof(s_data)); /* 新增下一個元素 */ scanf("%s %s %d %d",new_data->name,new_data->no,&new_data->Math,&new_data->Eng); ptr->next=new_data; /*存取指針設(shè)置為新元素所在位置 */ new_data->next =NULL; /* 下一個元素的next先設(shè)置為null */ ptr=ptr->next; num++; } } while (select != 2); ptr = head->next; /* 設(shè)置存取指針從頭開始 */ putchar('/n'); while (ptr!= NULL) { printf("姓名:%s/t學(xué)號:%s/t數(shù)學(xué)成績:%d/t英語成績:%d/n", ptr->name,ptr->no,ptr->Math,ptr->Eng); Msum+=ptr->Math; Esum+=ptr->Eng; student_no++; ptr= ptr ->next; /* 將ptr移往下一個元素 */ } printf("---------------------------------------------------------/n"); printf("本鏈表學(xué)生數(shù)學(xué)平均成績:%.2f 英語平均成績:%.2f/n",Msum/student_no,Esum/student_no); system("pause"); return 0;}

四、單向鏈表插入新節(jié)點

舉例如下:

struct employee{ int num,score; char name[10]; struct employee *next;};typedef struct employee node;typedef node *link;link findnode(link head,int num){ link ptr; ptr=head; while(ptr!=NULL) { if(ptr->num==num) return ptr; ptr=ptr->next; } return ptr;}link insertnode(link head,link ptr,int num,int score,char name[10]) { link InsertNode; InsertNode=(link)malloc(sizeof(node)); if(!InsertNode) return NULL; InsertNode->num=num; InsertNode->score=score; strcpy(InsertNode->name,name); InsertNode->next=NULL; if(ptr==NULL) /*插入第一個節(jié)點*/ { InsertNode->next=head; return InsertNode; } else { if(ptr->next==NULL)/*插入最后一個節(jié)點*/ { ptr->next=InsertNode; } else /*插入中間節(jié)點*/ { InsertNode->next=ptr->next; ptr->next=InsertNode; } } return head;}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 扎鲁特旗| 五莲县| 大竹县| 锦州市| 宁晋县| 陕西省| 保山市| 台州市| 西宁市| 河津市| 诸暨市| 漳浦县| 焦作市| 明水县| 富阳市| 灌南县| 陈巴尔虎旗| 夏河县| 庆云县| 泽库县| 武乡县| 万源市| 禄丰县| 九台市| 利辛县| 金沙县| 长泰县| 慈溪市| 长岭县| 永济市| 永宁县| 富蕴县| 麦盖提县| 汝城县| 耿马| 明水县| 咸丰县| 阿克陶县| 南汇区| 宁乡县| 左贡县|