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

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

鏈表

2019-11-14 12:19:44
字體:
供稿:網(wǎng)友

一、鏈表特點(diǎn)

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

在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é)點(diǎn)單向鏈表的算法

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;}

三、遍歷單向鏈表

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

#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é)點(diǎn)

舉例如下:

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é)點(diǎn)*/ { InsertNode->next=head; return InsertNode; } else { if(ptr->next==NULL)/*插入最后一個節(jié)點(diǎn)*/ { ptr->next=InsertNode; } else /*插入中間節(jié)點(diǎn)*/ { InsertNode->next=ptr->next; ptr->next=InsertNode; } } return head;}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 比如县| 读书| 肃宁县| 沙田区| 苏尼特右旗| 香河县| 曲沃县| 水城县| 辛集市| 电白县| 股票| 监利县| 凤冈县| 临沂市| 惠来县| 共和县| 泸州市| 广安市| 武城县| 镇安县| 安仁县| 普兰店市| 津南区| 新绛县| 南康市| 庄河市| 伊吾县| 赣榆县| 陆川县| 绩溪县| 北票市| 根河市| 营山县| 濮阳县| 临沧市| 安顺市| 衡水市| 颍上县| 富民县| 灵山县| 桑植县|