Java數(shù)據(jù)結構與算法學習之循環(huán)鏈表
存儲結構示意圖
優(yōu)點 : 能夠通過任意結點遍歷整個鏈表結構
初始化循環(huán)鏈表?
1.循環(huán)鏈表的結點
typedef struct CircularNode { ElementType date; //數(shù)據(jù)域 struct CircularNode* next; //指向下一個結點的指針域 }CircularNode;
2.循環(huán)鏈表結構
typedef struct CircularLinkList { CircularNode* next; //指向第一個結點的頭指針,頭指針 int length; }CircularLinkList;
循環(huán)鏈表的插入
需要考慮兩種情況
1.插入的鏈表長度為 0? ?node -> next = node;
2.插入的鏈表長度不為0 node -> next = clList -> next? ?lastNode -> next = node?
首位置
?
代碼實現(xiàn)
其他位置
代碼實現(xiàn)(總)
void InsertCircularLinkList(CircularLinkList* clList, int pos, ElementType element) { //創(chuàng)建一個空節(jié)點 CircularLinkList* node = (CircularLinkList*)malloc(sizeof(CircularLinkList)); node->date = element; node->next = NULL; if (pos == 1) { node->next = clList->next; if (!node->next) { //如果插入的鏈表長度為0 node->next = node; } else { //如果長度不為0,就要找到鏈表的最后一個結點并改變其指針域 CircularLinkList* lastNode = clList->next; for (int i = 1; i < clList->length; i++) { lastNode = lastNode->next; } clList->next = node; clList->length++; } return; } //插入的不是第一個結點 CircularLinkList* currNode = clList->next; for (int i = 1; i < pos - 1; i++) currNode = currNode->next; if (currNode) { node->next = currNode->next; currNode->next = node; clList->length++; if (pos == clList->length) { node->next = clList->next; } } }
循環(huán)鏈表的刪除
1.操作的為第一個元素
代碼實現(xiàn)
2.操作元素不為第一個元素
代碼實現(xiàn)
代碼實現(xiàn)(總)
ElementType DeleteCircularLinkList(CircularLinkList* clList, int pos) { ElementType element; element.id = -999; if (pos == 1) { CircularLinkList* node = clList->next; if (node) { //找到最后一個結點,改變其指針域的指向 CircularLinkList* lastNode = clList->next; for (int i = 1; i < clList->length; i++) { lastNode = lastNode->next; } clList->next = node->next; lastNode->next = clList->next; free(node); clList->length--; } return; } CircularLinkList* preNode; CircularLinkList* node = clList->next; for (int i = 1; node && i < pos; i++) { preNode = node; node = node->next; } if (node) { element = node->date; preNode->next = node->next; free(node); clList->length--; } return element; }
循環(huán)鏈表的常見操作?
已知 P 結點是循環(huán)鏈表的中間結點,通過該節(jié)點遍歷循環(huán)鏈表
代碼實現(xiàn)
CircularNode* GetCircularLinkListNode(CircularLinkList* clList, ELementType element) { CircularNode* node = clList->next; if (!node) return NULL; do { if (element.id == node->date.id && strcmp(element.name, node->date.name) == 0) { return node; } } while (node == clList->next); return NULL; }
以上就是Java數(shù)據(jù)結構與算法學習之循環(huán)鏈表的詳細內容,更多關于Java數(shù)據(jù)結構 循環(huán)鏈表的資料請關注腳本之家其它相關文章!
相關文章
利用java監(jiān)聽器實現(xiàn)在線人數(shù)統(tǒng)計
過去使用ASP和ASP.NET兩種編程的時候,都寫過在線人數(shù)統(tǒng)計能,實現(xiàn)功能挺簡單的!今天使用java來實現(xiàn)在線人數(shù)統(tǒng)計有點另類,是通過Java監(jiān)聽器實現(xiàn)的,需要的朋友可以參考下2015-09-09Java多條件判斷場景中規(guī)則執(zhí)行器的設計
近日在公司領到一個小需求,需要對之前已有的試用用戶申請規(guī)則進行拓展。本文去掉if 判斷,試試用一個規(guī)則執(zhí)行器來替代它,感興趣的可以了解一下2021-06-06springboot集成nacos無法動態(tài)獲取nacos配置的問題
這篇文章主要介紹了springboot集成nacos無法動態(tài)獲取nacos配置的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09