- C語言資料結構教程
- C語言資料結構 - 首頁
- C語言資料結構 - 概覽
- C語言資料結構 - 環境搭建
- C語言資料結構 - 演算法
- C語言資料結構 - 概念
- C語言資料結構 - 陣列
- C語言資料結構 - 連結串列
- C語言實現資料結構 - 雙向連結串列
- C語言資料結構 - 迴圈連結串列
- C語言資料結構 - 棧
- C語言資料結構 - 表示式解析
- C語言資料結構 - 佇列
- C語言資料結構 - 優先佇列
- C語言資料結構 - 樹
- C語言資料結構 - 雜湊表
- C語言資料結構 - 堆
- C語言資料結構 - 圖
- C語言資料結構 - 搜尋技術
- C語言資料結構 - 排序技術
- C語言資料結構 - 遞迴
- C語言資料結構 - 有用資源
- C語言資料結構 - 快速指南
- C語言資料結構 - 有用資源
- C語言資料結構 - 討論
C語言實現資料結構 - 雙向連結串列
概覽
雙向連結串列是連結串列的一種變體,與單向連結串列相比,它可以方便地向前和向後導航。以下是一些理解雙向連結串列概念的重要術語。
節點 − 連結串列的每個節點可以儲存一個稱為元素的資料。
Next − 連結串列的每個節點都包含一個指向下一個節點的連結,稱為 Next。
Prev − 連結串列的每個節點都包含一個指向前一個節點的連結,稱為 Prev。
連結串列 − 連結串列包含指向第一個節點(稱為 First)和最後一個節點(稱為 Last)的連線連結。
雙向連結串列表示
根據上面所示的插圖,以下是要考慮的重要事項。
雙向連結串列包含一個稱為 first 和 last 的節點元素。
每個節點都包含一個或多個數據欄位和一個稱為 next 的連結欄位。
每個節點都使用其 next 連結與其下一個節點連結。
每個節點都使用其 prev 連結與其前一個節點連結。
最後一個節點包含一個空連結,以標記列表的結尾。
基本操作
以下是列表支援的基本操作。
插入− 在列表開頭新增一個元素。
刪除 − 刪除列表開頭的元素。
插入到末尾− 在列表末尾新增一個元素。
刪除末尾元素 − 刪除列表末尾的元素。
插入到指定元素之後 − 在列表中的某個元素之後新增一個元素。
刪除 − 使用鍵從列表中刪除一個元素。
正向顯示 − 以正向方式顯示整個列表。
反向顯示 − 以反向方式顯示整個列表。
插入操作
以下程式碼演示了在雙向連結串列開頭進行插入操作。
//insert link at the first location
void insertFirst(int key, int data){
//create a link
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data = data;
if(isEmpty()){
//make it the last link
last = link;
} else {
//update first prev link
head->prev = link;
}
//point it to old first link
link->next = head;
//point first to new first link
head = link;
}
刪除操作
以下程式碼演示了在雙向連結串列開頭進行刪除操作。
//delete first item
struct node* deleteFirst(){
//save reference to first link
struct node *tempLink = head;
//if only one link
if(head->next == NULL){
last = NULL;
} else {
head->next->prev = NULL;
}
head = head->next;
//return the deleted link
return tempLink;
}
在末尾插入操作
以下程式碼演示了在雙向連結串列末尾進行插入操作。
//insert link at the last location
void insertLast(int key, int data){
//create a link
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data = data;
if(isEmpty()){
//make it the last link
last = link;
} else {
//make link a new last link
last->next = link;
//mark old last node as prev of new link
link->prev = last;
}
//point last to new last node
last = link;
}
示例
DoublyLinkedListDemo.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct node {
int data;
int key;
struct node *next;
struct node *prev;
};
//this link always point to first Link
struct node *head = NULL;
//this link always point to last Link
struct node *last = NULL;
struct node *current = NULL;
//is list empty
bool isEmpty(){
return head == NULL;
}
int length(){
int length = 0;
struct node *current;
for(current = head; current!=NULL;
current = current->next){
length++;
}
return length;
}
//display the list in from first to last
void displayForward(){
//start from the beginning
struct node *ptr = head;
//navigate till the end of the list
printf("\n[ ");
while(ptr != NULL){
printf("(%d,%d) ",ptr->key,ptr->data);
ptr = ptr->next;
}
printf(" ]");
}
//display the list from last to first
void displayBackward(){
//start from the last
struct node *ptr = last;
//navigate till the start of the list
printf("\n[ ");
while(ptr != NULL){
//print data
printf("(%d,%d) ",ptr->key,ptr->data);
//move to next item
ptr = ptr ->prev;
printf(" ");
}
printf(" ]");
}
//insert link at the first location
void insertFirst(int key, int data){
//create a link
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data = data;
if(isEmpty()){
//make it the last link
last = link;
} else {
//update first prev link
head->prev = link;
}
//point it to old first link
link->next = head;
//point first to new first link
head = link;
}
//insert link at the last location
void insertLast(int key, int data){
//create a link
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data = data;
if(isEmpty()){
//make it the last link
last = link;
} else {
//make link a new last link
last->next = link;
//mark old last node as prev of new link
link->prev = last;
}
//point last to new last node
last = link;
}
//delete first item
struct node* deleteFirst(){
//save reference to first link
struct node *tempLink = head;
//if only one link
if(head->next == NULL){
last = NULL;
} else {
head->next->prev = NULL;
}
head = head->next;
//return the deleted link
return tempLink;
}
//delete link at the last location
struct node* deleteLast(){
//save reference to last link
struct node *tempLink = last;
//if only one link
if(head->next == NULL){
head = NULL;
} else {
last->prev->next = NULL;
}
last = last->prev;
//return the deleted link
return tempLink;
}
//delete a link with given key
struct node* delete(int key){
//start from the first link
struct node* current = head;
struct node* previous = NULL;
//if list is empty
if(head == NULL){
return NULL;
}
//navigate through list
while(current->key != key){
//if it is last node
if(current->next == NULL){
return NULL;
} else {
//store reference to current link
previous = current;
//move to next link
current = current->next;
}
}
//found a match, update the link
if(current == head) {
//change first to point to next link
head = head->next;
} else {
//bypass the current link
current->prev->next = current->next;
}
if(current == last){
//change last to point to prev link
last = current->prev;
} else {
current->next->prev = current->prev;
}
return current;
}
bool insertAfter(int key, int newKey, int data){
//start from the first link
struct node *current = head;
//if list is empty
if(head == NULL){
return false;
}
//navigate through list
while(current->key != key){
//if it is last node
if(current->next == NULL){
return false;
} else {
//move to next link
current = current->next;
}
}
//create a link
struct node *newLink = (struct node*) malloc(sizeof(struct node));
newLink->key = key;
newLink->data = data;
if(current==last) {
newLink->next = NULL;
last = newLink;
} else {
newLink->next = current->next;
current->next->prev = newLink;
}
newLink->prev = current;
current->next = newLink;
return true;
}
main() {
insertFirst(1,10);
insertFirst(2,20);
insertFirst(3,30);
insertFirst(4,1);
insertFirst(5,40);
insertFirst(6,56);
printf("\nList (First to Last): ");
displayForward();
printf("\n");
printf("\nList (Last to first): ");
displayBackward();
printf("\nList , after deleting first record: ");
deleteFirst();
displayForward();
printf("\nList , after deleting last record: ");
deleteLast();
displayForward();
printf("\nList , insert after key(4) : ");
insertAfter(4,7, 13);
displayForward();
printf("\nList , after delete key(4) : ");
delete(4);
displayForward();
}
輸出
如果我們編譯並執行以上程式,它將產生以下輸出:
List (First to Last): [ (6,56) (5,40) (4,1) (3,30) (2,20) (1,10) ] List (Last to first): [ (1,10) (2,20) (3,30) (4,1) (5,40) (6,56) ] List , after deleting first record: [ (5,40) (4,1) (3,30) (2,20) (1,10) ] List , after deleting last record: [ (5,40) (4,1) (3,30) (2,20) ] List , insert after key(4) : [ (5,40) (4,1) (4,13) (3,30) (2,20) ] List , after delete key(4) : [ (5,40) (4,13) (3,30) (2,20) ]
廣告