單鏈表分析
對于單鏈表,由于每個結點只存儲了向后的指針,到了尾部標識就停止了向后鏈的操作。按照這樣的方式,只能索引后繼結點不能索引前驅結點。
帶來的問題
列如不從頭結點出發,就無法訪問到全部結點。
解決辦法
將單鏈表終端結點的指針端由空指針改為指向頭結點。
循環鏈表
定義
將單鏈表中終端結點的指針端有空指針改為指向頭結點,就使整個單鏈表形成一個環,這種頭尾相接的單鏈表成為單循環鏈表,簡稱單鏈表(circle linked list)
循環鏈表帶有頭結點的空鏈表:
循環鏈表帶有頭結點的空鏈表
循環鏈表帶有頭結點的非空鏈表:
循環鏈表帶有頭結點的非空鏈表
注意:并不是說循環鏈表一定要有頭結點!
循環單鏈表和單鏈表的主要差異就在于循環的判斷條件上:
- 原來是判斷
p->next
是否為null
- 現在是
p->next
是否等于頭結點
但是循環鏈表進過改造,不用頭指針,而是用指向終端結點的尾指針來表示循環鏈表,這樣查找開始結點和終端結點都很方便。
終端結點用尾指針
rear
表示,則查找終端結點是O(1)
,開始結點就是rear->next->next
,時間復雜度也為O(1)
;
合并兩個循環鏈表
-
圖示
- 代碼
LinkList Connect(LinkList A, LinkList B){
LinkList p = A->next; // 保存A表的頭結點
A->next = B->next->next; // 將B表的開始結點鏈接到A表尾部
free(B); // 釋放B表的頭結點
B->next = p;
return B; // 返回新循環鏈表的尾指針
}
循環鏈表操作
循環鏈表創建和初始化
#include <stdio.h>
#include <stdlib.h>
/*鏈式存儲結構的定義*/
typedef struct CLinkList{
int data;
struct CLinkList * next;
}node;
/**
* 1.初始化循環鏈表
*/
void ds_init(node **pNode){
int item;
node * temp;
node * target;
printf("輸入結點的值,輸入0完成初始化\n");
while (1) {
scanf("%d", &item);
fflush(stdin); // 清空輸入緩存區
if (item == 0) return;
if ((*pNode) == NULL) {
// 鏈表中只有一個結點
*pNode = (node *)malloc(sizeof(struct CLinkList));
if (!(*pNode)) exit(0);
(*pNode)->data = item;
(*pNode)->next = *pNode;
}else{
//找到next指向第一個結點的結點
for (target = (*pNode); target->next != (*pNode); target = target->next);
// 生成一個新的結點
temp = (node *)malloc(sizeof(struct CLinkList));
if (!temp) exit(0);
temp->data = item;
temp->next = *pNode;
target->next = temp;
}
}
}
插入結點操作
/**
* 2.插入結點
* @param pNode 鏈表的第一個結點
* @param i 插入的位置
*/
void ds_insert(node **pNode, int i){
node * temp;
node * target;
node * p;
int item;
int j = 1;
printf("輸入要插入加點的值:");
scanf("%d", &item);
if (i == 1) { // 插入到第一個位置
// 新插入的結點作為第一個結點
temp = (node *)malloc(sizeof(struct CLinkList));
if (!temp) exit(0);
temp->data = item;
//找到最后一個結點
for (target = (*pNode); target->next != (*pNode); target = target->next);
temp->next = (*pNode);
target->next = temp;
*pNode = temp;
}else{ // 插入到其他位置
target = *pNode;
for (; j<(i-1); j++) {
target = target->next;
}
temp = (node *)malloc(sizeof(struct CLinkList));
if (!temp) exit(0);
temp->data = item;
p = target->next;
target->next = temp;
temp->next = p;
}
}
刪除結點操作
/**
* 3.刪除結點
* @param pNode 鏈表的第一個結點
* @param i 刪除的位置
*/
void ds_delete(node **pNode, int i){
node * target;
node * temp;
int j = 1;
if (i ==1) { // 刪除的是第一個結點
// 找到最后一個結點
for (target = *pNode; target->next != *pNode; target = target->next);
temp = *pNode;
*pNode = (*pNode)->next;
target->next = *pNode;
free(temp);
}else{ // 刪除其他結點
target = *pNode;
for (; j<i-1 ; j++) {
target = target->next;
}
temp = target->next;
target->next = temp->next;
free(temp);
}
}
返回結點所在位置
/**
* 4.返回結點所在位置
* @param pNode 鏈表的第一個結點
* @param elem 結點所在位置
*/
int ds_search(node *pNode, int elem){
node * target;
int i = 1;
for (target = pNode; target->data != elem && target->next != pNode; i++) {
target = target->next;
}
if (target->next == pNode) return 0; // 表中不存在該元素
else
return i;
}
遍歷
/**
* 5.遍歷
*/
void ds_traverse(node *pNode){
node * temp;
temp = pNode;
printf("*************鏈表中的元素**********\n");
do {
printf("%4d ", temp->data);
} while ((temp = temp->next) != pNode);
printf("\n");
}
在main()
函數里面執行:
int main(int argc, const char * argv[]) {
node * pHead = NULL;
char opp = '\0';
int find;
printf("1.初始化鏈表\n \n2.插入結點 \n\n3.刪除結點 \n\n4.返回結點位置 \n\n5.遍歷鏈表 \n\n0.退出 \n\n選擇你的操作:");
while (opp != '0') {
scanf("%c", &opp);
switch (opp) {
case '1':
ds_init(&pHead);
printf("\n");
ds_traverse(pHead);
break;
case '2':
printf("輸入要插入結點的位置?");
scanf("%d", &find);
ds_insert(&pHead, find);
printf("在位置%d插入值后:\n",find);
ds_traverse(pHead);
printf("\n");
break;
case '3':
printf("輸入需要刪除的結點的位置?");
scanf("%d", &find);
ds_delete(&pHead, find);
printf("刪除第%d個結點后:\n",find);
ds_traverse(pHead);
printf("\n");
break;
case '4':
printf("輸入你要查找的結點的值?");
scanf("%d", &find);
printf("元素%d所在位置:%d\n",find, ds_search(pHead, find));
printf("\n");
break;
case '5':
ds_traverse(pHead);
printf("\n");
break;
case '0':
exit(0);
}
}
return 0;
}
運行效果
用循環鏈表解決約瑟夫問題
約瑟夫問題:
據說著名猶太歷史學家 Josephus有過以下的故事:在羅馬人占領喬塔帕特后,39 個猶太人與Josephus及他的朋友躲到一個洞中,39個猶太人決定寧愿死也不要被敵人抓到,于是決定了一個自殺方式,41個人排成一個圓圈,由第1個人開始報數,每報數到第3人該人就必須自殺,然后再由下一個重新報數,直到所有人都自殺身亡為止。然而Josephus 和他的朋友并不想遵從,Josephus要他的朋友先假裝遵從,他將朋友與自己安排在第16個與第31個位置,于是逃過了這場死亡游戲。
代碼:
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node * next;
}node;
node * create(int n);
int main(int argc, const char * argv[]) {
int n = 41;
int m = 3;
int i;
node *p = create(n);
node * temp;
while (p != p->next) {
for (i = 1; i < m-1; i++) {
p = p->next;
}
printf("%d->",p->next->data);
temp = p->next; // 刪除第m個結點
p->next = temp->next;
free(temp);
p = p->next;
}
printf("%d\n", p->data);
return 0;
}
/**
* 根據參數創建鏈表
*/
node * create(int n){
node * p = NULL;
node * head;
head = (node *)malloc(sizeof(node));
p = head; // p為指向當前結點的指針
node *s = NULL;
int i = 1;
if (n != 0) {
while (i <= n) {
s = (node *)malloc(sizeof(node));
s->data = i++; // 為循環鏈表初始化,第一個結點為1,第二個結點為2
p->next = s;
p = s;
}
s->next = head->next; // 最后一個結點指向第一個結點
}
free(head); // 刪除頭結點
return s->next;
}
打印結果