【文章內(nèi)容簡介】
ct node { int data。 struct node *next。 猴 子選大王 5 }linklist。 主函數(shù)的實現(xiàn)調(diào)用操作: 采用循環(huán)鏈表來實現(xiàn),可參考課本。 int main() { int i, n, k, m, total。 linklist *head, *p, *s, *q。 /* 讀入問題條件 */ printf(Please enter the number of monkeys:)。 scanf(%d, amp。n)。 printf(Please enter from the monkeys began to count off the first of several:)。 scanf(%d, amp。k)。 printf(Please enter the number out:)。 scanf(%d, amp。m)。 /* 創(chuàng)建循環(huán)鏈表,頭節(jié)點也存信息 */ head = (linklist*) malloc(sizeof(linklist))。 猴 子選大王 6 p = head。 pdata = 1。 pnext = p。 /* 初始化循環(huán)鏈表 */ for (i = 2。 i = n。 i++) { s = (linklist*) malloc(sizeof(linklist))。 sdata = i。 snext = pnext。 pnext = s。 p = pnext。 } /* 找到第 k 個節(jié)點 */ p = head。 for (i = 1。 i k。 i++) 猴 子選大王 7 { p = pnext。 } /* 保存節(jié)點總數(shù) */ total = n。 printf(\nOut of sequence:)。 q = head。 /* 只剩一個節(jié)點時停止循環(huán) */ while (total != 1) { /* 報數(shù)過程, p 指向要刪除的節(jié)點 */ for (i = 1。 i m。 i++) { p = pnext。 } 猴 子選大王 8 /* 打印要刪除的節(jié)點序號 */ printf([%d] , pdata)。 /* q 指向 p 節(jié)點的前驅(qū) */ while (qnext != p) { q = qnext。 } /* 刪除 p 節(jié)點 */ qnext = pnext。 /* 保存被刪除節(jié)點指針 */ s = p。 /* p 指向被刪除節(jié)點的后繼 */ p = pnext。 /* 釋放被刪除的節(jié)點 */ free(s)