教你如何運用python/golang實現迴圈連結串列
這篇文章主要介紹了python/golang如何實現迴圈連結串列,幫助大家更好的理解和學習迴圈連結串列的實現方法,感興趣的朋友可以瞭解下 |
迴圈連結串列就是將單連結串列的末尾指向其頭部,形成一個環。迴圈連結串列的增刪操作和單連結串列的增刪操作區別不大。只是增加時,需要考慮空連結串列增加第一個節點的特殊情況;刪除時需考慮刪除節點是頭/尾節點,和連結串列中只有一個節點的特殊情況。
golang實現:
type Node struct { value int next *Node } type Circle struct { tail *Node lenth int } // 增加節點: func (c *Circle) add(value int) { newNode := &Node{value, nil} if c.lenth == 0 { //空連結串列中新增節點 c.tail = newNode c.tail.next = newNode } else { newNode.next = c.tail.next c.tail.next = newNode c.tail = newNode } c.lenth += 1 c.printCircle() } // 刪除節點: func (c *Circle) remove(v int) { if c.lenth == 0 { fmt.Println("空環") return } else if c.lenth == 1 && c.tail.value == v { //連結串列中只有一個節點的特殊情況 c.tail = nil c.lenth = 0 c.printCircle() return } pre := c.tail cur := c.tail.next // 頭節點 for i := 0; i < c.lenth; i++ { if cur.value == v { if cur == c.tail { //如果刪除的節點是尾節點,需更新tail c.tail = pre } pre.next = cur.next c.lenth -= 1 c.printCircle() return } pre = cur cur = cur.next } fmt.Println(v, "不在環中") } //列印節點: func (c *Circle) printCircle() { if c.lenth == 0 { fmt.Println("空環") return } cur := c.tail.next // 頭節點 for i := 0; i < c.lenth; i++ { fmt.Printf("%d ", cur.value) cur = cur.next } fmt.Println() } func testCircle() { var circle *Circle = new(Circle) //for i := 1; i <=41; i++ { // circle.add(i) //} circle.add(1) circle.remove(10) circle.printCircle() }
python實現:
class Node: def __init__(self, value, next=None): self.value = value self.next = next def __str__(self): return str(self.value) class Circle: def __init__(self): self.tail = None self.lenth = 0 # 增加節點 def add(self, v): new_node = Node(v) if self.lenth == 0: # 空連結串列中新增節點 self.tail = new_node self.tail.next = new_node else: new_node.next = self.tail.next self.tail.next = new_node self.tail = new_node self.lenth += 1 # 刪除節點 def remove(self, v): if self.lenth == 0: print("空環") return elif self.lenth == 1 and self.tail.value == v: # 連結串列中只有一個節點的特殊情況 self.tail = None self.lenth = 0 return pre = self.tail cur = self.tail.next # 頭節點 for i in range(self.lenth): if cur.value == v: if cur == self.tail: # 如果刪除的節點是尾節點,需更新tail self.tail = pre pre.next = cur.next self.lenth -= 1 return pre = cur cur = cur.next print(v, "不在環中") # 列印連結串列 def print_circle(self): if self.lenth == 0: print('空環') return cur = self.tail.next # 頭節點 for i in range(self.lenth): print(cur, end=" ") cur = cur.next print() def test(): c = Circle() for i in range(10): c.add(i) c.print_circle() c.remove(0) c.print_circle() c.remove(10) c.print_circle() c.remove(9) c.print_circle() c.remove(4) c.print_circle()
以上就是python/golang實現迴圈連結串列的示例程式碼的詳細內容。
原文地址:
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31559985/viewspace-2733207/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 迴圈連結串列(約瑟夫問題)--python實現Python
- 連結串列4: 迴圈連結串列
- 【資料結構】實現迴圈連結串列(c++)資料結構C++
- 單向迴圈連結串列
- 連結串列以及golang介入式連結串列的實現Golang
- 教你如何運用golang實現陣列分割Golang陣列
- golang 實現連結串列爽不爽?Golang
- 單鏈迴圈連結串列(初版
- 複習下C 連結串列操作(雙向迴圈連結串列,查詢迴圈節點)
- javascript中使用迴圈連結串列實現約瑟夫環問題JavaScript
- 雙向迴圈連結串列基本操作的實現(C語言)C語言
- 資料結構之迴圈連結串列資料結構
- 單向迴圈連結串列大綱
- C 語言使用非迴圈雙向連結串列實現佇列佇列
- Golang從合併連結串列聊遞迴Golang遞迴
- 教你如何運用golang 實現陣列的隨機排序Golang陣列隨機排序
- 【LeetCode】 Rotate List 迴圈連結串列LeetCode
- 單向迴圈連結串列的介面程式
- Python實現單連結串列Python
- 【資料結構】雙迴圈連結串列(c++)資料結構C++
- 【資料結構】遞迴實現連結串列逆序資料結構遞迴
- 設計單向迴圈連結串列的介面
- 迴圈單連結串列建構函式、解構函式C++實現函式C++
- 連結串列找環(python實現)Python
- python3實現連結串列Python
- 資料結構與演算法--迴圈連結串列資料結構演算法
- 實戰資料結構(5)_雙向迴圈連結串列的基本操作資料結構
- [Golang]力扣LeetBook—初級演算法—連結串列—迴文連結串列(快慢指標)Golang力扣演算法指標
- 資料結構初階--雙向迴圈連結串列(講解+類别範本實現)資料結構
- 雙向迴圈連結串列的介面設計(初版
- 資料結構學習(C++)——迴圈連結串列 (轉)資料結構C++
- 單向迴圈連結串列——查詢、刪除、插入結點
- 雙向迴圈連結串列————遍歷、查詢、插入結點
- 利用單向迴圈連結串列結構實現如下記憶體頁置換策略CLOCK Algorithm記憶體Go
- Python資料結構——連結串列的實現Python資料結構
- 非迴圈單連結串列的建立、遍歷、排序等排序
- 單向迴圈連結串列介面設計(C語言)C語言
- 自定義雙向迴圈連結串列基本函式介面函式