matlab 双向链表,双向链表基本操作(C语言实现)
前面學(xué)習(xí)了如何創(chuàng)建一個(gè)雙向鏈表,本節(jié)學(xué)習(xí)有關(guān)雙向鏈表的一些基本操作,即如何在雙向鏈表中添加、刪除、查找或更改數(shù)據(jù)元素。
本節(jié)知識基于已熟練掌握雙向鏈表創(chuàng)建過程的基礎(chǔ)上,我們繼續(xù)上節(jié)所創(chuàng)建的雙向鏈表來學(xué)習(xí)本節(jié)內(nèi)容,創(chuàng)建好的雙向鏈表如圖 1 所示:
圖 1 雙向鏈表示意圖
雙向鏈表添加節(jié)點(diǎn)
根據(jù)數(shù)據(jù)添加到雙向鏈表中的位置不同,可細(xì)分為以下 3 種情況:
添加至表頭
將新數(shù)據(jù)元素添加到表頭,只需要將該元素與表頭元素建立雙層邏輯關(guān)系即可。
換句話說,假設(shè)新元素節(jié)點(diǎn)為 temp,表頭節(jié)點(diǎn)為 head,則需要做以下 2 步操作即可:
temp->next=head; head->prior=temp;
將 head 移至 temp,重新指向新的表頭;
例如,將新元素 7 添加至雙鏈表的表頭,則實(shí)現(xiàn)過程如圖 2 所示:
圖 2 添加元素至雙向鏈表的表頭
添加至表的中間位置
同單鏈表添加數(shù)據(jù)類似,雙向鏈表中間位置添加數(shù)據(jù)需要經(jīng)過以下 2 個(gè)步驟,如圖 3 所示:
新節(jié)點(diǎn)先與其直接后繼節(jié)點(diǎn)建立雙層邏輯關(guān)系;
新節(jié)點(diǎn)的直接前驅(qū)節(jié)點(diǎn)與之建立雙層邏輯關(guān)系;
圖 3 雙向鏈表中間位置添加數(shù)據(jù)元素
添加至表尾
與添加到表頭是一個(gè)道理,實(shí)現(xiàn)過程如下(如圖 4 所示):
找到雙鏈表中最后一個(gè)節(jié)點(diǎn);
讓新節(jié)點(diǎn)與最后一個(gè)節(jié)點(diǎn)進(jìn)行雙層邏輯關(guān)系;
圖 4 雙向鏈表尾部添加數(shù)據(jù)元素
因此,我們可以試著編寫雙向鏈表添加數(shù)據(jù)的 C 語言代碼,參考代碼如下:
line * insertLine(line * head,int data,int add){
//新建數(shù)據(jù)域?yàn)閐ata的結(jié)點(diǎn)
line * temp=(line*)malloc(sizeof(line));
temp->data=data;
temp->prior=NULL;
temp->next=NULL;
//插入到鏈表頭,要特殊考慮
if (add==1) {
temp->next=head;
head->prior=temp;
head=temp;
}else{
line * body=head;
//找到要插入位置的前一個(gè)結(jié)點(diǎn)
for (int i=1; i
body=body->next;
}
//判斷條件為真,說明插入位置為鏈表尾
if (body->next==NULL) {
body->next=temp;
temp->prior=body;
}else{
body->next->prior=temp;
temp->next=body->next;
body->next=temp;
temp->prior=body;
}
}
return head;
}
雙向鏈表刪除節(jié)點(diǎn)
雙鏈表刪除結(jié)點(diǎn)時(shí),只需遍歷鏈表找到要?jiǎng)h除的結(jié)點(diǎn),然后將該節(jié)點(diǎn)從表中摘除即可。
例如,從圖 1 基礎(chǔ)上刪除元素 2 的操作過程如圖 5 所示:
圖 5 雙鏈表刪除元素操作示意圖
雙向鏈表刪除節(jié)點(diǎn)的 C 語言實(shí)現(xiàn)代碼如下:
//刪除結(jié)點(diǎn)的函數(shù),data為要?jiǎng)h除結(jié)點(diǎn)的數(shù)據(jù)域的值
line * delLine(line * head,int data){
line * temp=head;
//遍歷鏈表
while (temp) {
//判斷當(dāng)前結(jié)點(diǎn)中數(shù)據(jù)域和data是否相等,若相等,摘除該結(jié)點(diǎn)
if (temp->data==data) {
temp->prior->next=temp->next;
temp->next->prior=temp->prior;
free(temp);
return head;
}
temp=temp->next;
}
printf("鏈表中無該數(shù)據(jù)元素");
return head;
}
雙向鏈表查找節(jié)點(diǎn)
通常,雙向鏈表同單鏈表一樣,都僅有一個(gè)頭指針。因此,雙鏈表查找指定元素的實(shí)現(xiàn)同單鏈表類似,都是從表頭依次遍歷表中元素。
C 語言實(shí)現(xiàn)代碼為:
//head為原雙鏈表,elem表示被查找元素
int selectElem(line * head,int elem){
//新建一個(gè)指針t,初始化為頭指針 head
line * t=head;
int i=1;
while (t) {
if (t->data==elem) {
return i;
}
i++;
t=t->next;
}
//程序執(zhí)行至此處,表示查找失敗
return -1;
}
雙向鏈表更改節(jié)點(diǎn)
更改雙鏈表中指定結(jié)點(diǎn)數(shù)據(jù)域的操作是在查找的基礎(chǔ)上完成的。實(shí)現(xiàn)過程是:通過遍歷找到存儲有該數(shù)據(jù)元素的結(jié)點(diǎn),直接更改其數(shù)據(jù)域即可。
實(shí)現(xiàn)此操作的 C 語言實(shí)現(xiàn)代碼如下:
//更新函數(shù),其中,add 表示更改結(jié)點(diǎn)在雙鏈表中的位置,newElem 為新數(shù)據(jù)的值
line *amendElem(line * p,int add,int newElem){
line * temp=p;
//遍歷到被刪除結(jié)點(diǎn)
for (int i=1; i
temp=temp->next;
}
temp->data=newElem;
return p;
}
總結(jié)
這里給出雙鏈表中對數(shù)據(jù)進(jìn)行 "增刪查改" 操作的完整實(shí)現(xiàn)代碼:
#include
#include
typedef struct line{
struct line * prior;
int data;
struct line * next;
}line;
//雙鏈表的創(chuàng)建
line* initLine(line * head);
//雙鏈表插入元素,add表示插入位置
line * insertLine(line * head,int data,int add);
//雙鏈表刪除指定元素
line * delLine(line * head,int data);
//雙鏈表中查找指定元素
int selectElem(line * head,int elem);
//雙鏈表中更改指定位置節(jié)點(diǎn)中存儲的數(shù)據(jù),add表示更改位置
line *amendElem(line * p,int add,int newElem);
//輸出雙鏈表的實(shí)現(xiàn)函數(shù)
void display(line * head);
int main() {
line * head=NULL;
//創(chuàng)建雙鏈表
head=initLine(head);
display(head);
//在表中第 3 的位置插入元素 7
head=insertLine(head, 7, 3);
display(head);
//表中刪除元素 2
head=delLine(head, 2);
display(head);
printf("元素 3 的位置是:%d\n",selectElem(head,3));
//表中第 3 個(gè)節(jié)點(diǎn)中的數(shù)據(jù)改為存儲 6
head = amendElem(head,3,6);
display(head);
return 0;
}
line* initLine(line * head){
head=(line*)malloc(sizeof(line));
head->prior=NULL;
head->next=NULL;
head->data=1;
line * list=head;
for (int i=2; i<=5; i++) {
line * body=(line*)malloc(sizeof(line));
body->prior=NULL;
body->next=NULL;
body->data=i;
list->next=body;
body->prior=list;
list=list->next;
}
return head;
}
line * insertLine(line * head,int data,int add){
//新建數(shù)據(jù)域?yàn)閐ata的結(jié)點(diǎn)
line * temp=(line*)malloc(sizeof(line));
temp->data=data;
temp->prior=NULL;
temp->next=NULL;
//插入到鏈表頭,要特殊考慮
if (add==1) {
temp->next=head;
head->prior=temp;
head=temp;
}else{
line * body=head;
//找到要插入位置的前一個(gè)結(jié)點(diǎn)
for (int i=1; i
body=body->next;
}
//判斷條件為真,說明插入位置為鏈表尾
if (body->next==NULL) {
body->next=temp;
temp->prior=body;
}else{
body->next->prior=temp;
temp->next=body->next;
body->next=temp;
temp->prior=body;
}
}
return head;
}
line * delLine(line * head,int data){
line * temp=head;
//遍歷鏈表
while (temp) {
//判斷當(dāng)前結(jié)點(diǎn)中數(shù)據(jù)域和data是否相等,若相等,摘除該結(jié)點(diǎn)
if (temp->data==data) {
temp->prior->next=temp->next;
temp->next->prior=temp->prior;
free(temp);
return head;
}
temp=temp->next;
}
printf("鏈表中無該數(shù)據(jù)元素");
return head;
}
//head為原雙鏈表,elem表示被查找元素
int selectElem(line * head,int elem){
//新建一個(gè)指針t,初始化為頭指針 head
line * t=head;
int i=1;
while (t) {
if (t->data==elem) {
return i;
}
i++;
t=t->next;
}
//程序執(zhí)行至此處,表示查找失敗
return -1;
}
//更新函數(shù),其中,add 表示更改結(jié)點(diǎn)在雙鏈表中的位置,newElem 為新數(shù)據(jù)的值
line *amendElem(line * p,int add,int newElem){
line * temp=p;
//遍歷到被刪除結(jié)點(diǎn)
for (int i=1; i
temp=temp->next;
}
temp->data=newElem;
return p;
}
//輸出鏈表的功能函數(shù)
void display(line * head){
line * temp=head;
while (temp) {
if (temp->next==NULL) {
printf("%d\n",temp->data);
}else{
printf("%d->",temp->data);
}
temp=temp->next;
}
}
程序執(zhí)行結(jié)果為:
1->2->3->4->5
1->2->7->3->4->5
1->7->3->4->5
元素 3 的位置是:3
1->7->6->4->5
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的matlab 双向链表,双向链表基本操作(C语言实现)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PHP判断标量,php中is_scala
- 下一篇: matlab无穷积分求解_从零开始的ma