雙向鏈表

一、雙向鏈表的結(jié)構(gòu)及數(shù)據(jù)定義

#define ERROR0

#define TRUE1

#define FALSE0

#define OK1

#define MAXSIZE20/* 存儲空間初始分配量 */

//頭結(jié)點數(shù)據(jù)

#define HeadNodeData -1

typedef int Status;/* Status是函數(shù)的類型,其值是函數(shù)結(jié)果狀態(tài)代碼,如OK等 */

typedef int ElemType;/* ElemType類型根據(jù)實際情況而定,這里假設(shè)為int */

//定義結(jié)點

typedef struct Node{

? ? structNode*prior;

? ? ElemTypedata;

? ? structNode*next;

}Node;

typedef struct Node *LinkList;

二、創(chuàng)建雙向鏈表

Status CreateLinkList(LinkList *L){

? ? //創(chuàng)建一個頭結(jié)點

? ? *L = (LinkList)malloc(sizeof(Node));

? ? if(*L ==NULL) {

? ? ? ? returnERROR;

? ? }


? ? (*L)->data=HeadNodeData;//無效數(shù)據(jù)

? ? (*L)->next? =NULL;

? ? (*L)->prior=NULL;


? ? //新增數(shù)據(jù)

? ? LinkListp = *L;

? ? LinkListtemp;

? ? for(inti=0; i<10; i++) {

? ? ? ? //創(chuàng)建一個新結(jié)點

? ? ? ? temp = (LinkList)malloc(sizeof(Node));

? ? ? ? temp->data= i;

? ? ? ? temp->next=NULL;

? ? ? ? temp->prior=NULL;

? ? ? ? //為新增的結(jié)點建立雙向鏈表關(guān)系

? ? ? ? //temp 是p的后繼

? ? ? ? p->next= temp;

? ? ? ? //p 是temp的前驅(qū)

? ? ? ? temp->prior= p;

? ? ? ? //p指向最后一個結(jié)點

? ? ? ? p = temp;

? ? }

? ? returnOK;

}

三煎殷、打印循環(huán)鏈表的元素

void display(LinkList L){

? ? if(L ==NULL) {

? ? ? ? printf("打印的雙向鏈表為空!\n");

? ? ? ? return;

? ? }

? ? //指向第一個元素(非頭結(jié)點)

? ? LinkListtemp = L->next;

? ? while(temp) {

? ? ? ? printf("%d? ",temp->data);

? ? ? ? temp = temp->next;

? ? }

? ? printf("\n");

}

四、雙向鏈表插入元素

Status InsertList(LinkList *L,int index,ElemType data){

? ? //參數(shù)檢查

? ? if(index<1|| *L ==NULL) {

? ? ? ? returnERROR;

? ? }

? ? //新建結(jié)點

? ? LinkList temp = (LinkList)malloc(sizeof(Node));

? ? temp->data= data;

? ? temp->next=NULL;

? ? temp->prior=NULL;

? ? //指向頭結(jié)點

? ? LinkListp = *L;

? ? //找到插入位置的上一個結(jié)點

? ? inti=1;

? ? while(p && i

? ? ? ? p = p->next;

? ? ? ? i++;

? ? }

? ? //如果插入的位置超過鏈表本身的長度

? ? if(p ==NULL) {

? ? ? ? returnERROR;

? ? }

? ? //判斷插入位置是否為鏈表尾部;

? ? if(p->next==NULL) {

? ? ? ? p->next= temp;

? ? ? ? temp->prior= p;

? ? }else{

? ? ? ? //新結(jié)點的next指向p的next

? ? ? ? temp->next= p->next;

? ? ? ? //p的下一個結(jié)點的prior指向temp

? ? ? ? p->next->prior= temp;


? ? ? ? //新結(jié)點的prior指向p

? ? ? ? temp->prior= p;

? ? ? ? //p的next指向新結(jié)點

? ? ? ? p->next= temp;

? ? }

? ? returnOK;

}

五、刪除雙向鏈表指定位置上的結(jié)點

Status DeleteList(LinkList *L,int index,ElemType *data){

? ? //

? ? if(*L ==NULL|| index<1) {

? ? ? ? returnERROR;

? ? }

? ? //找到要刪除位置的上一個結(jié)點

? ? LinkListp = *L;

? ? for(inti=1; i

? ? ? ? p = p->next;

? ? }

? ? if(p==NULL) {

? ? ? ? returnERROR;

? ? }

? ? //指向要刪除的結(jié)點

? ? LinkListdelTemp = p->next;

? ? //p的next指向要刪除結(jié)點的下一個結(jié)點

? ? p->next= delTemp->next;


? ? //如果delTemp不是最后一個結(jié)點菠红,則delTemp的下一個結(jié)點的prior指向p

? ? if(delTemp->next!=NULL) {

? ? ? ? delTemp->next->prior= p;

? ? }

? ? //將值帶回去

? ? *data = delTemp->data;

? ? //將要刪除結(jié)點回收

? ? free(delTemp);

? ? returnOK;

}

六、刪除指定元素

Status DeleteListVAL(LinkList *L,ElemType data){

? ? if(*L ==NULL|| data ==HeadNodeData) {

? ? ? ? returnERROR;

? ? }

? ? LinkListp = (*L)->next;

? ? while(p) {

? ? ? ? if(p->data== data) {

? ? ? ? ? ? //找到對應(yīng)數(shù)據(jù),p是要被刪除的結(jié)點

? ? ? ? ? ? //p的前一個結(jié)點的next指向p的next

? ? ? ? ? ? p->prior->next= p->next;

? ? ? ? ? ? //不是最后一個結(jié)點,則p的下一個結(jié)點的prior指向p的前一個結(jié)點

? ? ? ? ? ? if(p->next!=NULL) {

? ? ? ? ? ? ? ? p->next->prior= p->prior;

? ? ? ? ? ? }

? ? ? ? ? ? //

? ? ? ? ? ? free(p);

//? ? ? ? ? ? break;//只刪除第一次出現(xiàn)的那個

? ? ? ? }

? ? ? ? //沒有找到該結(jié)點,則繼續(xù)移動指針p

? ? ? ? p = p->next;

? ? }

? ? returnOK;

}

七难菌、在雙向鏈表中查找元素

int SelectList(LinkList L,ElemType elem){

? ? if(L ==NULL) {

? ? ? ? return HeadNodeData;

? ? }

? ? //指向首元結(jié)點

? ? LinkListp = L->next;

? ? inti =1;

? ? while(p) {


? ? ? ? if(p->data== elem) {

? ? ? ? ? ? returni;

? ? ? ? }

? ? ? ? p = p->next;

? ? ? ? i++;

? ? }

? ? return HeadNodeData;

}

八试溯、在雙向鏈表中更新結(jié)點

StatusReplaceList(LinkListL,intindex,ElemTypenewData){

? ? if(L ==NULL|| index<1) {

? ? ? ? returnERROR;

? ? }

? ? LinkListp = L->next;

? ? inti=1;

? ? if(p && i

? ? ? ? p = p->next;

? ? ? ? i++;

? ? }

? ? if(p ==NULL) {

? ? ? ? returnERROR;

? ? }

? ? p->data= newData;

? ? returnOK;

}

九、調(diào)用

intmain(intargc,constchar* argv[]) {


? ? LinkList L;

? ? CreateLinkList(&L);

? ? display(L);


? ? intindex;

? ? ElemTypedata;

? ? printf("請輸入要插入的位置和值:\n");

? ? scanf("%d %d",&index,&data);


? ? InsertList(&L,index,data);

? ? display(L);


? ? printf("請輸入要刪除的位置1:\n");

? ? scanf("%d",&index);

? ? DeleteList(&L, index, &data);

? ? display(L);


? ? printf("請輸入要刪除的位置2:\n");

? ? scanf("%d",&index);

? ? DeleteList(&L, index, &data);

? ? display(L);


? ? printf("請輸入要刪除的位置3:\n");

? ? scanf("%d",&index);

? ? DeleteList(&L, index, &data);

? ? display(L);


? ? printf("請輸入要刪除的數(shù)據(jù)1:\n");

? ? scanf("%d",&data);

? ? DeleteListVAL(&L, data);

? ? display(L);


? ? printf("請輸入要刪除的數(shù)據(jù)2:\n");

? ? scanf("%d",&data);

? ? DeleteListVAL(&L, data);

? ? display(L);


? ? printf("請輸入要查詢的數(shù)據(jù)1:\n");

? ? scanf("%d",&data);

? ? index =SelectList(L, data);

? ? printf("數(shù)據(jù)在位置:%d \n",index);


? ? printf("請輸入要查詢的數(shù)據(jù)2:\n");

? ? scanf("%d",&data);

? ? index =SelectList(L, data);

? ? printf("數(shù)據(jù)在位置:%d \n",index);


? ? printf("請輸入要查詢的數(shù)據(jù)3:\n");

? ? scanf("%d",&data);

? ? index =SelectList(L, data);

? ? printf("數(shù)據(jù)在位置:%d \n",index);

? ? return0;

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末郊酒,一起剝皮案震驚了整個濱河市遇绞,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌燎窘,老刑警劉巖摹闽,帶你破解...
    沈念sama閱讀 218,607評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異褐健,居然都是意外死亡付鹿,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,239評論 3 395
  • 文/潘曉璐 我一進店門蚜迅,熙熙樓的掌柜王于貴愁眉苦臉地迎上來舵匾,“玉大人,你說我怎么就攤上這事谁不∽荩” “怎么了?”我有些...
    開封第一講書人閱讀 164,960評論 0 355
  • 文/不壞的土叔 我叫張陵刹帕,是天一觀的道長吵血。 經(jīng)常有香客問我谎替,道長,這世上最難降的妖魔是什么蹋辅? 我笑而不...
    開封第一講書人閱讀 58,750評論 1 294
  • 正文 為了忘掉前任钱贯,我火速辦了婚禮,結(jié)果婚禮上晕翠,老公的妹妹穿的比我還像新娘喷舀。我一直安慰自己,他們只是感情好淋肾,可當我...
    茶點故事閱讀 67,764評論 6 392
  • 文/花漫 我一把揭開白布硫麻。 她就那樣靜靜地躺著,像睡著了一般樊卓。 火紅的嫁衣襯著肌膚如雪拿愧。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,604評論 1 305
  • 那天碌尔,我揣著相機與錄音浇辜,去河邊找鬼。 笑死唾戚,一個胖子當著我的面吹牛柳洋,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播叹坦,決...
    沈念sama閱讀 40,347評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼熊镣,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了募书?” 一聲冷哼從身側(cè)響起绪囱,我...
    開封第一講書人閱讀 39,253評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎莹捡,沒想到半個月后鬼吵,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,702評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡篮赢,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,893評論 3 336
  • 正文 我和宋清朗相戀三年齿椅,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片启泣。...
    茶點故事閱讀 40,015評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡媒咳,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出种远,到底是詐尸還是另有隱情,我是刑警寧澤顽耳,帶...
    沈念sama閱讀 35,734評論 5 346
  • 正文 年R本政府宣布坠敷,位于F島的核電站妙同,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏膝迎。R本人自食惡果不足惜粥帚,卻給世界環(huán)境...
    茶點故事閱讀 41,352評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望限次。 院中可真熱鬧芒涡,春花似錦、人聲如沸卖漫。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,934評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽羊始。三九已至旱幼,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間突委,已是汗流浹背柏卤。 一陣腳步聲響...
    開封第一講書人閱讀 33,052評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留匀油,地道東北人缘缚。 一個月前我還...
    沈念sama閱讀 48,216評論 3 371
  • 正文 我出身青樓,卻偏偏與公主長得像敌蚜,于是被迫代替她去往敵國和親桥滨。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,969評論 2 355

推薦閱讀更多精彩內(nèi)容