本篇內(nèi)容介紹了“l(fā)inux單向循環(huán)鏈表源碼分析”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)公司!專(zhuān)注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、成都小程序開(kāi)發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶(hù)創(chuàng)新互聯(lián)還提供了單縣免費(fèi)建站歡迎大家使用!
extern inline void add_wait_queue(struct wait_queue ** p, struct wait_queue * wait)
{
unsigned long flags;
#ifdef DEBUG
if (wait->next) {
unsigned long pc;
__asm__ __volatile__("call 1f\n"
"1:\tpopl %0":"=r" (pc));
printk("add_wait_queue (%08x): wait->next = %08x\n",pc,(unsigned long) wait->next);
}
#endif
save_flags(flags);
cli();
// 隊(duì)列為空,頭指針指向待插入的節(jié)點(diǎn)wait,末節(jié)點(diǎn)的next指針指向自己
if (!*p) {
wait->next = wait;
*p = wait;
} else {
/*
在第一個(gè)節(jié)點(diǎn)后面插入節(jié)點(diǎn),形成單向循環(huán)鏈表 thanks to zym.
插入第二個(gè)節(jié)點(diǎn)的時(shí)候,是在第一個(gè)節(jié)點(diǎn)后面插入,后面在插入的時(shí)候,
是在第一個(gè)第二個(gè)節(jié)點(diǎn)中間插入,然后是從第一第三個(gè)直接插入,如此類(lèi)推
*p指向第一個(gè)節(jié)點(diǎn),(*p)->next指向第一個(gè)節(jié)點(diǎn)的下一個(gè),插入第二個(gè)節(jié)點(diǎn)的時(shí)候,
第一個(gè)節(jié)點(diǎn)的下一個(gè)節(jié)點(diǎn)是自己。wait->next即新節(jié)點(diǎn)的next指向第一個(gè)節(jié)點(diǎn)的下一個(gè)節(jié)點(diǎn),
(*p)->next = wait;即第一個(gè)節(jié)點(diǎn)的next指針指向新加入的節(jié)點(diǎn)。
傳統(tǒng)的頭插法只能形成單鏈表,不能循環(huán),因?yàn)檠h(huán)需要拿尾指針的next指向第一個(gè)
節(jié)點(diǎn),但是隨著鏈表的變成,無(wú)法找到尾節(jié)點(diǎn)。
p -> head -> null
p -> head -> node1
next
|------->
p -> head -> node1 node2
<-------
next
next next
|-------> |------->
p -> head -> node1 node3 node2
<------------------
next
測(cè)試代碼
#include <stdio.h>
struct wait_queue {
int task;
struct wait_queue * next;
};
void add_wait_queue(struct wait_queue ** p, struct wait_queue * wait)
{
if (!*p) {
//printf("%d", 1);
wait->next = wait;
*p = wait;
} else {
// 頭插法,形成單向鏈表
wait->next = (*p)->next;
(*p)->next = wait;
//printf("%d", wait->next == *p);
}
}
int main()
{
struct wait_queue wait = { 1, NULL };
struct wait_queue wait1 = { 2, NULL };
struct wait_queue wait2 = { 3, NULL };
struct wait_queue * head = NULL;
add_wait_queue(&head, &wait);
add_wait_queue(&head, &wait1);
add_wait_queue(&head, &wait2);
int c = 5;
while(c--) {
printf("%d", head->task);
head = head->next;
}
}
*/
wait->next = (*p)->next;
(*p)->next = wait;
}
restore_flags(flags);
}
extern inline void remove_wait_queue(struct wait_queue ** p, struct wait_queue * wait)
{
unsigned long flags;
struct wait_queue * tmp;
#ifdef DEBUG
unsigned long ok = 0;
#endif
save_flags(flags);
cli();
// 刪除的是第一個(gè)節(jié)點(diǎn)并且只有一個(gè)節(jié)點(diǎn)了則頭指針指向NULL
if ((*p == wait) &&
#ifdef DEBUG
(ok = 1) &&
#endif
((*p = wait->next) == wait)) {
*p = NULL;
} else {
// 從自己開(kāi)始遍歷單向循環(huán)鏈表,找到next指向自己的,然后更新指針
tmp = wait;
while (tmp->next != wait) {
tmp = tmp->next;
#ifdef DEBUG
if (tmp == *p)
ok = 1;
#endif
}
tmp->next = wait->next;
}
wait->next = NULL;
restore_flags(flags);
#ifdef DEBUG
if (!ok) {
printk("removed wait_queue not on list.\n");
printk("list = %08x, queue = %08x\n",(unsigned long) p, (unsigned long) wait);
__asm__("call 1f\n1:\tpopl %0":"=r" (ok));
printk("eip = %08x\n",ok);
}
#endif
}
“l(fā)inux單向循環(huán)鏈表源碼分析”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
網(wǎng)頁(yè)標(biāo)題:linux單向循環(huán)鏈表源碼分析
網(wǎng)頁(yè)路徑:http://chinadenli.net/article14/gjsdde.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、手機(jī)網(wǎng)站建設(shè)、ChatGPT、網(wǎng)站維護(hù)、動(dòng)態(tài)網(wǎng)站、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)