前面學習了鏈式結構鍊錶,鏈式結構鍊錶也有使用的侷限性。比如說我們經常碰到的迴圈關係,
最經典的當然是約瑟夫問題了,那麼我們怎麼去解決約瑟夫問題呢?相信你看完本文章就會了,
好了言歸正傳,我們生活中經常會遇到迴圈問題,例如,一年的12個月,春夏秋冬交替,
這些迴圈關係怎麼在我們的程式體現出來,少廢話,上**。
第四個例子,迴圈鍊錶:
標頭檔案
#ifndef _circlelist_h_
#define _circlelist_h_
typedef void circlelist;
typedef struct tag_circlelistnode circlelistnode;
struct tag_circlelistnode
;circlelist* circlelist_create();
void circlelist_destroy(circlelist* list);
void circlelist_clear(circlelist* list);
int circlelist_length(circlelist* list);
int circlelist_insert(circlelist* list, circlelistnode* node, int pos);
circlelistnode* circlelist_get(circlelist* list, int pos);
circlelistnode* circlelist_delete(circlelist*list, int pos);
circlelistnode* circlelist_current(circlelist* list);
circlelistnode* circlelist_next(circlelist* list);
circlelistnode* circlelist_reset(circlelist* list);
circlelistnode* circlelist_deletenode(circlelist* list, circlelistnode* node);
#endif
實現檔案
#include "circlelist.h"
#include typedef struct tag_circlelist
tcirclelist;
circlelist* circlelist_create()
return (circlelist*)ret;
}void circlelist_destroy(circlelist* list)
void circlelist_clear(circlelist* list)
}int circlelist_length(circlelist* list)
return ret;
}int circlelist_insert(circlelist* list, circlelistnode* node, int pos)
node->next = current->next;
current->next = node;
if (slist->length == 0)
slist->length++;
if (current == (circlelistnode*)slist)
}return ret;
}circlelistnode* circlelist_get(circlelist* list, int pos)
ret = current->next;
}return ret;
}circlelistnode* circlelist_delete(circlelist*list, int pos)
if (current == (circlelistnode*)slist)
ret = current->next;
current->next = ret->next;
slist->length--;
if (last)
if (slist->slider == ret)
if (slist->length == 0)
}return ret;
}circlelistnode* circlelist_current(circlelist* list)
return ret;
}circlelistnode* circlelist_next(circlelist* list)
return ret;
}circlelistnode* circlelist_reset(circlelist* list)
return ret;
}circlelistnode* circlelist_deletenode(circlelist* list, circlelistnode* node)
current = current->next;
} if (ret != null)
}return ret;
}
在實現檔案裡面,是不是發現多幾了函式呢?沒錯,那就是游標的操作,為什麼要引入游標呢?
主要是游標操作大大的節省了對鍊錶的操作時間。
測試檔案
#include #include #include "circlelist.h"
typedef struct tag_value
value;
int main(int argc, char *argv)
circlelist_clear(list);
printf("length = %d\n", circlelist_length(list));
//下面我們來解決乙個約瑟夫問題
//8個人圍成乙個圈報數,每次報到3的退出,求出列順序
circlelist_insert(list, (circlelistnode*)&a, circlelist_length(list));
circlelist_insert(list, (circlelistnode*)&b, circlelist_length(list));
circlelist_insert(list, (circlelistnode*)&c, circlelist_length(list));
circlelist_insert(list, (circlelistnode*)&d, circlelist_length(list));
circlelist_insert(list, (circlelistnode*)&e, circlelist_length(list));
circlelist_insert(list, (circlelistnode*)&f, circlelist_length(list));
circlelist_insert(list, (circlelistnode*)&g, circlelist_length(list));
circlelist_insert(list, (circlelistnode*)&h, circlelist_length(list));
for (i = 0; i < circlelist_length(list); i++)
printf("length = %d\n", circlelist_length(list));
while (circlelist_length(list) > 0)
p = (value*)circlelist_current(list);
printf("%d\n", p->v);
circlelist_deletenode(list, (circlelistnode*)p);
} printf("length = %d\n", circlelist_length(list));
circlelist_destroy(list);
system("pause");
return 0;
}
在測試檔案裡面,我已經把約瑟夫問題給解決了,是不是很簡單呢?相信你在很多面試題中肯定也接觸過,什麼報數刪除,什麼猴子取桃,這些問題都可以用上面方法解決,好了迴圈煉表演示完畢。 資料結構之迴圈鍊錶
迴圈鍊錶是資料結構中煉表的一種形式。相對於單向鍊錶,將單向鍊錶的尾結點的指標域指向該單向鍊錶的頭結點,就構成了迴圈鍊錶。可以這麼理解,單向鍊錶就是一張單程火車票,比如你要從北京坐火車去上海,路上經過濟南 南京,此時北京 濟南 南京 上海就構成了單向鍊錶的四個結點,如下圖所示。但是,單向鍊錶是有去無回...
資料結構之迴圈鍊錶
首先先了解如何判斷單鏈表中是否有環 環的定義 鍊錶中的尾節點指向了鍊錶中的某個節點。方法一 使用p q兩個指標,p一直往前走,q每次從頭往前走,當p等於q但是p q移動的步數不相等的時候,存在環。方法二 使用p q兩個指標,p每次往前走一步,q每次往前走兩步,當存在p q的時候,存在環。迴圈鍊錶,只...
資料結構之 迴圈鍊錶
迴圈鍊錶是另一種形式的鏈式存貯結構。它的特點是表中最後乙個結點的指標域指向頭結點,整個鍊錶形成乙個環。分類 1 單迴圈鍊錶 在單鏈表中,將終端結點的指標域null改為指向表頭結點或開始結點即可。2 多重鏈的迴圈鍊錶 將表中結點鏈在多個環上。空鏈判斷 判斷空鍊錶的條件是 head head next ...