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