鍊錶的基本操作

2021-07-29 10:40:39 字數 3287 閱讀 2574

/*

c++學習筆記02——鍊錶的基本操作

2017.03.25

*/

#define _crt_secure_no_warnings

#include

"stdlib.h"

#include

"stdio.h"

#include

"string.h"

typedef struct node

slist;

slist *creat_slist();

int slist_print(slist *phead);

//在結點數值為x的前面插入y

int slist_nodeinsert(slist *phead, int x, int y);

//刪除結點為y的鍊錶結點

int slist_nodedel(slist *phead, int y);

int slist_destory(slist *phead);

int slist_revse(slist *phead);

slist *creat_slist()

phead->

data=0;

phead->next =

null;

//2迴圈建立結點,結點資料域中的數值從鍵盤輸入,

//以-1作為輸入結束標誌

printf("\nplease enter the data of node(-1:quit) ");

scanf("%d", &

data);

//準備環境 讓pcur指向phead

pcur = phead;

while (data!=-

1)

pm->

data

=data;

pm->next =

null;

//1新節點入鍊錶

pcur->next = pm;

//2 當前結點下移(新結點變成當前結點)

pcur = pm; // (pcur = pcur->next)

printf("\nplease enter the data of node(-1:quit) ");

scanf("%d", &

data);

}return phead;

}int creat_slist2(slist **head)

phead->

data=0;

phead->next =

null;

//2迴圈建立結點,結點資料域中的數值從鍵盤輸入,

//以-1作為輸入結束標誌

printf("\nplease enter the data of node(-1:quit) ");

scanf("%d", &

data);

//準備環境 讓pcur指向phead

pcur = phead;

while (data!=-

1)

pm->

data

=data;

pm->next =

null;

//1新節點入鍊錶

pcur->next = pm;

//2 當前結點下移(新結點變成當前結點)

pcur = pm; // (pcur = pcur->next)

printf("\nplease enter the data of node(-1:quit) ");

scanf("%d", &

data);

}*head = phead;

return ret;

}int slist_print(slist *phead)

//準備環境

p = phead->next;

printf("\nbegin ");

while (p)

printf("end ");

return0;}

//在結點數值為x的前面插入y

int slist_nodeinsert(slist *phead, int x, int y)

//環境準備

ppre = phead;

pcur = phead->next;

//不斷的malloc新節點 並且資料域 賦值

pm = (slist *)malloc(sizeof(slist));

pm->

data

= y;

pm->next =

null;

while (pcur)

//讓ppre下移

ppre = pcur;

//讓當前節點下移

pcur = pcur->next;

}//讓新節點鏈結後繼結點

pm->next = ppre->next; //(pcur)

//讓前驅結點連線pm

ppre->next = pm;

return0;}

//刪除結點為y的鍊錶結點

int slist_nodedel(slist *phead, int y)

//環境準備

ppre = phead;

pcur = phead->next;

while (pcur)

//讓ppre下移

ppre = pcur;

//讓當前節點下移

pcur = pcur->next;

}if (pcur ==

null)

//執行操作

ppre->next = pcur->next;

free(pcur);

return0;}

int slist_destory(slist *phead)

while (pcur)

return0;}

int slist_revse(slist *phead)

if (phead->next ==

null

|| phead->next->next ==

null)

//準備環境

p = phead->next;

q = phead->next->next;

while (q !=

null)

//讓第乙個業務結點賦值null

phead->next->next =

null;

//讓煉表頭指向最後乙個節點

phead->next = p;

return0;}

void main()

鍊錶的基本操作

include include include include using namespace std struct listnode void initnode listnode node bool isempty listnode head void pushfront listnode hea...

鍊錶的基本操作

鍊錶操作是最基本的 必須掌握的知識點,最好滾瓜爛熟,透徹理解。工作時間短用的也不夠頻繁,還是總結一下比較好,以加強鞏固。1.單鏈表 結點形式 區分幾個概念 首節點 第乙個元素所在節點。頭指標 指向首節點的指標。頭結點 為了操作方便,在第乙個節點之前附設的乙個結點,此時指向頭結點的為頭指標。基本操作 ...

鍊錶的基本操作。。。

include node.h 列印鍊錶 void print node head printf n 從尾部插入 void insert tail node head,const int d while t next null t next p p next null 從頭部插入 void inser...