約瑟夫問題
迴圈鍊錶的特點
判斷單鏈表中是否有環
例題雙向鍊錶
對於單鏈表,由於每個結點只儲存了向後的指標,到了尾部標識就停止了向後鏈的操作。也就是說,按照這樣的方式,只能索引後繼節點,不能索引前驅節點。不從頭結點出發就無法訪問到全部節點,故有了迴圈鍊錶
//鍊錶儲存結構定義
typedef
struct cliinklist
node;
//初始化迴圈鍊錶
void
ds_init
(node *
*pnode)
(*pnode)
->data = item;
(*pnode)
->next =
*pnode;
}else
}}
//插入節點
//引數:鍊錶的第乙個節點,插入位置,插入元素
status ds_insert
(node *
*pnode,
int i,
int e)
else
temp =
(node*
)malloc
(sizeof
(node));
temp->data = e;
p = target->next;
target->next = temp;
temp->next = p;
}return ok;
}
int
ds_delete
(node *
*pnode,
int i)
else
temp = target->next;
target->next = temp->next;
free
(temp);}
}
//返回節點的所在位置
intds_search
(node *pnode,
int e)
if(target->next == pnode)
//表中不存在該元素
else
}
//n個人圍圈報數,報m出列,最後剩下幾號?
#include
#include
typedef
struct node
node;
node *
create
(int n)
s->next = head->next;
}free
(head)
;return s->next;
}int
main()
printf
("%d->"
,p->next->data)
; temp = p->next;
//刪除第m個節點
例題:實現將兩個線性表(a1,a2,…an)和(b1,b2,…,bn)連線成乙個線性表(a1,…,an,b1,…bn)
//假設a、b為非空單迴圈鍊錶的尾指標
linklist connect
(linklist a,linklist b)
// 比較步數的方法
inthasloop1
(linklist l)
} cur2 = cur2->next;
//如果沒有環,繼續下乙個節點
pos2++
;//cur2步數自增
} cur1 = cur1->next;
//cur1繼續向後乙個節點
pos1++
;//cur1 步數自增
} retutn 0
;}
- 使用p、q兩個指標,p每次向前走一步,q每次向前走兩步,若在某個時候p == q,則存在環(快慢指標)
//快慢指標方法
inthasloop2
(linklist l)
return0;
}
//發牌順序計算
一定不要搞錯順序
四 迴圈鍊錶和雙向鍊錶
迴圈鍊錶 迴圈鍊錶是鏈式儲存結構的另一種形式,特點是單鏈表的最後乙個結點 終端結點 的指標域不為空,而是指向鍊錶的頭結點,使整個鍊錶形成乙個環。例子 將值為x的新結點插入到從大到小有序迴圈鍊錶的適當位置 int insertdata linklist head,int x p next s 插入s結...
迴圈鍊錶,雙向鍊錶
迴圈鍊錶 迴圈鍊錶與順序鍊錶之間的區別 迴圈鍊錶最後乙個資料的next指標域不為空,而是指向頭結點,其他基本操作大體相同,只是在判斷表結束的條件變為判斷節點的引用域是否為頭引用 雙向鍊錶 author neosong date oct 10,2017 4 43 01 pm program of in...
鍊錶 雙向迴圈鍊錶
雙向迴圈鍊錶與單鏈表一樣,都是邏輯連續 物理不連續的儲存方式,但它的效果要遠遠優於單鏈表,其結構如下 雙向迴圈鍊錶首先要有乙個頭節點,頭節點中不存放資料,真正的資料從頭節點的下乙個節點開始存放 然後每乙個節點都有兩個指標,分別指向前乙個節點和後乙個節點 最後頭尾相連,就成了雙向迴圈鍊錶。includ...