資料結構 單鏈表,迴圈鍊錶,雙項鍊表的實現

2021-08-10 06:37:53 字數 2537 閱讀 6360

單鏈表:

單鏈表是一種鏈式訪問的資料結構,用一組位址任意的

儲存單元

存放線性表中的

資料元素

。鍊錶中的資料是以結點來表示的,每個結點的構成:

元素(資料元素的映象) + 指標(指示後繼元素儲存位置),

元素就是儲存資料的儲存單元,指標就是連線每個結點的位址資料。

②:每個結點只有乙個鏈域的鍊錶稱為單鏈表(single linked list)。

型別描述如下:

typedef struct node

node,*list;

//typedef struct node * list;

這裡我們為方便起見建立乙個帶頭結點的單鏈表,頭結點的資料域不存放資料。

單鏈表的實現:

//初始化

void initlist(list plist)

plist->next = null;//鍊錶的尾結點指標域為null,初始化無需考慮資料域

}//頭插

bool insert_head(list plist,int val)//o(1)

//尾插

bool insert_tail(list plist,int val)//o(n)

//查詢

node *srearch(list plist,int key)

} return null;

}//刪除

bool delete(list plist,int key)

} return false;

}//摧毀(清除可直接呼叫摧毀)

void destroy(list plist)

}//逆置(非常重要!!!)

void reverse(list plist)

//下面的時間複雜度為o(n),並且最好,利用頭插的思想

node *p = plist->next;

node *q;

plist->next = null;

while(p != null)

迴圈鍊錶:

迴圈鍊錶是另一種形式的鏈式存貯結構。它的特點是表中最後乙個結點的指標域指向頭結點,整個鍊錶形成乙個環。

型別描述:

typedef struct cnode

cnode,*clist;  //與單鏈表沒區別

//鍊錶初始化

void initlist(clist plist)

plist->next = plist;//plist->next = null;(單鏈表與迴圈鍊錶的區別)

}//頭插(與單鏈表無區別)

bool insert_head(clist plist,int val)

//尾插

bool insert_tail(clist plist,int val)

//查詢

cnode *srearch(clist plist,int key)

} return null;

}(內部函式,找到前乙個)

static cnode *searchpri(clist plist,int key)

} return null;

}//刪除

bool delete(clist plist,int key)

cnode *q = p->next;

p->next = q->next;

free(q);

return true;

}

雙向鍊錶:

雙向鍊錶也叫雙鏈表,是鍊錶的一種,它的每個資料結點中都有兩個指標,分別指向直接後繼和直接前驅。所以,從雙向鍊錶中的任意乙個結點開始,都可以很方便地訪問它的前驅結點和後繼結點。

型別描述:

typedef struct dnode

dnode,*dlist;

//鍊錶初始化

void initlist(dlist plist)

plist->next = null;

plist->prio = null;

}//頭插

bool insert_head(dlist plist,int val)

return true;

}//尾插

bool insert_tail(dlist plist,int val)

//查詢

dnode *search(dlist plist,int key)

} return null;

}//刪除

bool delete(dlist plist,int key)

p->prio->next = p->next;

if(p->next != null)

free(p);

return true;

}//摧毀

void destroy(dlist plist)

}分享:

C 資料結構 單鏈表 雙鏈表與迴圈鍊錶

注意 以下所有鍊錶均包含空的頭結點。include stdafx.h include using namespace std 節點類 class node 乙個引數的建構函式 node node nextvalue 單鏈錶類 class singlelinkedlist node p newnode...

迴圈鍊錶 迴圈雙鏈表 迴圈單鏈表

迴圈單 雙鏈表,建立 初始化 尾插 頭插 遍歷 插入 刪除 判空 部分函式採用過載 此處為c include include include using namespace std typedef struct lnodelnode,linklist typedef struct dnodednod...

單鏈表 雙鏈表 迴圈鍊錶總結

1.單鏈表 為公升序鍊錶,value按公升序排列 include include typedef struct node node 最好放在標頭檔案中 node sll creat int sll length node p node sll del node head,int value void...