C語言 雙鏈表常用函式

2021-08-05 20:24:59 字數 3586 閱讀 8634

/*

* 煉表演示

* 雙向

* */

#include

#include

"03link.h"

//鍊錶的初始化函式

void link_init(link

*p_link)

//鍊錶的清理函式

void link_deinit(link

*p_link)

}//計算有效數字個數的函式,正反一樣,要修改可以讓p_first,p_mid,p_last,從後面開始來

int link_size(const link

*p_link)

}return cnt;

}//判斷鍊錶是否空的函式

int link_empty(const link

*p_link)

//判斷鍊錶是否滿的函式

int link_full(const link

*p_link)

//在鍊錶前邊加入新節點的函式

void link_add_head(link

*p_link, int val)

p_tmp->num = val;

p_tmp->p_next =

null;//正向指標

p_tmp->p_pre =

null;//反向指標

p_first =

&(p_link->head);

p_mid = p_first->p_next;

p_last = p_mid->p_next;

p_first->p_next = p_tmp;

p_tmp->p_pre = p_first;//反向

p_tmp->p_next = p_mid;

p_mid->p_pre = p_tmp;//反向

}//在原有節點最後加入新節點的函式

*p_link, int val)

p_tmp->num = val;

p_tmp->p_next =

null;//正向

p_tmp->p_pre =

null;//反向

p_first = p_link->tail.p_pre;//反向新增最後乙個節點就是這裡不同,原來是first指向頭,現在是指向尾,相當於反著來了

p_mid = p_first->p_next;

p_last = p_mid->p_next;

//把新節點加入到p_first和p_mid中間

p_first->p_next = p_tmp;//正

p_tmp->p_pre = p_first;//反

p_tmp->p_next = p_mid;//正

p_mid->p_pre = p_tmp;//反

} node *p_tmp = (node *)malloc(sizeof(node));

node *p_node = null;

if (!p_tmp)

p_tmp->num = val;

p_tmp->p_next = null;

p_tmp->p_pre = null;

for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next)

}}*///按順序插入函式

void link_insert(link

*p_link, int val)

p_tmp->num = val;

p_tmp->p_next =

null;

p_tmp->p_pre =

null;

for (p_node =

&(p_link->head);p_node !=

&(p_link->tail);p_node = p_node->p_next)

}}//刪除最前面有效數字的函式

int link_remove_head(link

*p_link)

p_first =

&(p_link->head);

p_mid = p_first->p_next;

p_last = p_mid->p_next;

p_first->p_next = p_last;//正

p_last->p_pre = p_first;//反

free(p_mid);

p_mid =

null;

return1;}

//刪除最後乙個有效節點的函式

int link_remove_tail(link

*p_link)

p_first = p_link->tail.p_pre->p_pre;//尾節點->最後乙個有效節點->倒數第二個有效節點

p_mid = p_first->p_next;//最後乙個有效節點

p_last = p_mid->p_next;

//把p_mid指標**的節點從煉表裡

//摘出來

p_first->p_next = p_last;

p_last->p_pre = p_first;

free(p_mid);

p_mid =

null;

return1;}

/*int link_remove_tail(link *p_link)

for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next)

}return 1;

}*///刪除某個數字所在節點的函式

int link_remove(link

*p_link, int val)

}return0;}

//獲得鍊錶最前面數字的函式

int link_get_head(const link

*p_link, int *p_num)

p_first =

&(p_link->head);

p_mid = p_first->p_next;

p_last = p_mid->p_next;

*p_num = p_mid->num;

return1;}

//獲得最後有效數字的函式

int link_get_tail(const link

*p_link, int *p_num)

*p_num = p_link->tail.p_pre->num;

return1;}

/*int link_get_tail(const link *p_link, int *p_num)

for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next)

}return 1;

}*///根據編號獲得數字的函式

int link_get(const link

*p_link, int num, int *p_num)

cnt++;

}return

0;}

C語言 雙鏈表

還是 程式設計師面試寶典 上的 include include typedef struct student dnode 建立雙鏈表 dnode create else cycle 0 p next null p head head head next head pre null free p 釋放...

雙鏈表 C語言

雙向鍊錶也叫雙鏈表,是鍊錶的一種,它的每個資料結點中都有兩個指標,分別指向直接後繼和直接前驅。簡單圖示 這次寫的雙鏈表的頭結點是不儲存資料的,所有資料都是存於頭結點的下乙個結點,即所有判斷都以頭結點的下乙個結點為準。資料簡介typedef struct dnode link 函式簡介link cre...

雙鏈表(c語言版)

雙鏈表相對於單鏈表來說,每乙個節點還存了乙個指向上乙個節點的指標,提公升了便捷性,例如某些情境下我們需要找到當前節點的上乙個節點等問題,雙鏈表對於單鏈表有很大的優勢。雙鏈表尾節點的prev指標指向頭節點,故雙鏈表的頭節點為哨兵衛,不存資料,僅作為標記作用。當單鏈表為空時,僅存在乙個頭節點,next指...