資料結構(五) 雙鏈表 鏈式棧 鏈式佇列 及實現

2021-05-28 08:00:22 字數 3406 閱讀 7099

一、雙鏈表

在單鏈表的基礎上再增加乙個指向它前驅的指標,就構成了雙鏈表。

所以雙鏈表有三個變數:資料資訊info、前驅指標llink、後繼指標rlink。

二、雙鏈表操作和實現

由於雙鏈表也為單鏈表的一種變型,一些相似的操作就沒一一枚舉,可以參考資料結構(四)——單鏈表 、帶頭結點的單鏈表、迴圈鍊錶 及其實現

1、資料結構

2、在i位置插入結點

3、在y元素後插入結點

4、刪除值為x的結點

1、資料結構

typedef int datatype;

typedef struct dlink_nodednode;

2、在i位置插入結點

include"linklist.h"

node* insert_link_list_index(node *head,int index,datatype x)

if(index == 0)

head->llink = q; //非空鍊錶首插入

q->rlink = head;

q->llink = null;

head = q;

return head;

}else

ptr->rlink = p; //鍊錶尾插入

p->llink = ptr;

p->rlink = null;

return head;}}

3、在y元素之後插入

#include"linklist.h"

node* intsert_node_yx(node *head,datatype x,datatype y)

node *p = (node*)malloc(sizeof(node));

p->info = x;

if(!q->rlink)

p->rlink = q->rlink; //中間結點

q->rlink->llink = p;

q->rlink = p;

p->llink = q;

return head;

}

4、刪除值為x的結點

#include"linklist.h"

node* del_link_list_node(node* head,datatype x)

node* ptr=head;

while(!ptr && ptr->info != x)

if(!ptr)else if(ptr == head && ptr->rlink)else if(ptr == head && !ptr->rlink)

}else if(!ptr->rlink)else

free(ptr);

return head;

}

三、鏈式棧

棧的鏈式儲存稱為鏈式棧。它的插入和刪除規定在單鏈表的同一端進行,棧頂指標用top表示。

1、輸出各個結點的值

2、取得棧頂元素

3、入棧

4、出棧

1、輸出各個結點的值

#include"linklist.h"

void display_link_list(node *stack)else}}

2、取得棧頂元素

#include"linklist.h"

node* get_top(node *stack)

return stack->info;

}

3、入棧

#include"linklist.h"

node* push(node* stack,datatype x)

4、出棧

#include"linklist.h"

node* pop(node *stack,datatype *x)

node *p = stack;

stack=stack->next;

*x = p->info;

free(p);

return stack;

}

四、鏈式佇列

以佇列形式儲存的鏈式佇列,插入和刪除在單鏈表的不同端進行,隊首和隊尾指標存在乙個資料結構中。1、結構定義

2、建立乙個空佇列

3、判斷是否為空

4、取得首節點值

5、輸出各個結點

6、入列

7、出列

1、結構定義

typedef int datatype;

typedef struct link_queue_nodenode;

typedef struct queuequeue;

2、建立乙個空佇列

#include"linkqueue.h"

queue* init_link_queue()

3、判斷是否為空

#include"linkqueue.h"

int is_empty_link_queue(queue *q)

4、取得首節點值

#include"linkqueue.h"

datatype* get_head(queue *q)

return q->front->info;

}

5、輸出各個結點

#include"linkqueue.h"

void display_link_queue(queue *q)else}}

6、入列

#include"linkqueue.h"

queue* en_list_queue(queue *q,datatype x)else

return q;

}

7、出列

#include"linkqueue.h"

queue* del_link_queue(queue *q,datatype *x)

node *p = q->front;

if(q->front == q->rear)else

*x = p->info;

free(p);

return q;

}

雙鏈表 鏈式棧 鏈式佇列 及實現

一 雙鏈表 在單鏈表的基礎上再增加乙個指向它前驅的指標,就構成了雙鏈表。所以雙鏈表有三個變數 資料資訊info 前驅指標llink 後繼指標rlink。二 雙鏈表操作和實現 由於雙鏈表也為單鏈表的一種變型,一些相似的操作就沒一一枚舉,可以參考資料結構 四 單鏈表 帶頭結點的單鏈表 迴圈鍊錶 及其實現...

資料結構 線性表 鏈式表(單鏈表)

不要求邏輯上相鄰的元素在物理儲存上相鄰,使用指標來表示元素之間的邏輯關係。優點 對線性表進行插入刪除操作時不需要移動大量的元素,只需要修改對應元素的指標域即可,方便省時 不需要為整個線性鍊錶提前分配足夠的儲存空間 當節點不再使用時,可以將儲存空間進行及時的 template class listno...

線性表 鏈式儲存結構之雙鏈表

該文章主要介紹線性表的鏈式儲存運算以及相關運算 雙鏈表。標頭檔案 dlinklist.h template struct dlinklist 雙鏈表結點型別 template class dlinklistclass 雙鏈錶類 原始檔 dlinklist.cpp include include dl...