資料結構之單向迴圈鍊錶

2021-09-02 23:38:01 字數 3066 閱讀 1012

基於單向鍊錶 將最後乙個節點 指向 頭結點

#ifndef  _cirlinklist_h_

#define _cirlinklist_h_

typedef

enum

bool;

typedef

int data;

typedef

struct _node

node;

typedef

struct _list

list;

//建立空鍊錶

list* creat ();

//摧毀鍊錶

//[in]list : 需要摧毀的鍊錶

void

destroy

(list*list)

;//插入資料:頭插

// [in]list : 要插入的單鏈表

// [in]data : 要插入的資料

bool insert_head

(list*list,data data)

;//插入資料:尾插

// [in]list : 要插入的單鏈表

// [in]data : 要插入的資料

bool insert_tail

(list*list,data data)

;//插入資料: 按位置插入

// [in]list : 要插入的單鏈表

// [in]pos : 要插入的位置

// [in]data : 要插入的資料

bool insert_pos

(list*list,

int pos,data data)

;//刪除資料: 按位置刪除

// [in]list : 要插入的單鏈表

// [in]pos : 要插入的位置

bool delete_pos

(list*list,

int pos)

;//刪除資料: 按資料刪除

// [in]list : 要插入的單鏈表

// [in]data : 要插入的資料

bool delete_data

(list*list,data data)

;//單向鍊錶逆序

bool reverse

(list *list)

;//列印

void

display

(list*list)

;#endif

// _cirlinklist_h_

#include

#include

#include

"cirlinklist.h"

//建立空鍊錶

list* creat (

) list->head =

(node*

)malloc

(sizeof

(node)

/sizeof

(char))

;if(null

== list->head )

list->head->next = list->head;

return list;

}//摧毀鍊錶

void

destroy

(list*list)

free

(list->head )

;free

(list);}

bool insert_head

(list*list,data data)

new_node ->data = data;

new_node ->next = list->head->next;

list->head->next = new_node;

return true;

}bool insert_tail

(list*list,data data)

new_node ->data = data;

new_node ->next = list->head;

//找到最後指向空的節點

node *tmp = list->head;

//指向頭結點

while

(tmp->next != list->head)

tmp->next =new_node;

return true;

}bool insert_pos

(list*list,

int pos,data data)

node *tmp =list->head;

//指向頭結點

//找到指點位置的插入的前乙個節點

int i;

for(i=

0;i1;i++)}

node->next = tmp->next;

tmp->next = node;

node->data = data;

return true;

}bool delete_pos

(list*list,

int pos)

} node *pos_node =tmp->next;

tmp->next =pos_node->next;

free

(pos_node)

;return true;

}bool delete_data

(list*list,data data)

tmp = tmp->next;

}return false;

}bool reverse

(list *list)

//逆序後的最後乙個節點指向頭結點

list->head->next->next = list->head;

//頭結點指向逆序後的第乙個節點

list->head->next = pre;

return true;

}void

display

(list*list)

printf

("\n");

}

資料結構 單向迴圈鍊錶

typedef struct node node,pnode pnode init link list void 單鏈表初始化 phead next phead return phead pnode new node int dat bool insert node tail pnode phead...

資料結構之單向鍊錶

結構體 指標 更強大的資料結構 分類 1 頭指標式鍊錶 不實用很麻煩 2 頭結點式鍊錶 常用 節點 typedef int data 常用操作 1建立 2摧毀3增加 4刪除5查詢 6修改7逆序 ifndef linklist h define linklist h typedef enum bool...

資料結構(三) 鍊錶3 單向迴圈鍊錶

孩子節點 class boypublic intgetno public void setno int no public boy getnext public void setnext boy next override public string tostring 建立乙個單向環形鍊錶的思路 1...