python/golang實現(xiàn)循環(huán)鏈表的示例代碼
循環(huán)鏈表就是將單鏈表的末尾指向其頭部,形成一個環(huán)。循環(huán)鏈表的增刪操作和單鏈表的增刪操作
區(qū)別不大。只是增加時,需要考慮空鏈表增加第一個節(jié)點的特殊情況;刪除時需考慮刪除節(jié)點是頭/尾節(jié)點,和鏈表中只有一個節(jié)點的特殊情況。
golang實現(xiàn):
type Node struct { value int next *Node } type Circle struct { tail *Node lenth int } // 增加節(jié)點: func (c *Circle) add(value int) { newNode := &Node{value, nil} if c.lenth == 0 { //空鏈表中添加節(jié)點 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() } // 刪除節(jié)點: func (c *Circle) remove(v int) { if c.lenth == 0 { fmt.Println("空環(huán)") return } else if c.lenth == 1 && c.tail.value == v { //鏈表中只有一個節(jié)點的特殊情況 c.tail = nil c.lenth = 0 c.printCircle() return } pre := c.tail cur := c.tail.next // 頭節(jié)點 for i := 0; i < c.lenth; i++ { if cur.value == v { if cur == c.tail { //如果刪除的節(jié)點是尾節(jié)點,需更新tail c.tail = pre } pre.next = cur.next c.lenth -= 1 c.printCircle() return } pre = cur cur = cur.next } fmt.Println(v, "不在環(huán)中") } //打印節(jié)點: func (c *Circle) printCircle() { if c.lenth == 0 { fmt.Println("空環(huán)") return } cur := c.tail.next // 頭節(jié)點 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實現(xiàn):
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 # 增加節(jié)點 def add(self, v): new_node = Node(v) if self.lenth == 0: # 空鏈表中添加節(jié)點 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 # 刪除節(jié)點 def remove(self, v): if self.lenth == 0: print("空環(huán)") return elif self.lenth == 1 and self.tail.value == v: # 鏈表中只有一個節(jié)點的特殊情況 self.tail = None self.lenth = 0 return pre = self.tail cur = self.tail.next # 頭節(jié)點 for i in range(self.lenth): if cur.value == v: if cur == self.tail: # 如果刪除的節(jié)點是尾節(jié)點,需更新tail self.tail = pre pre.next = cur.next self.lenth -= 1 return pre = cur cur = cur.next print(v, "不在環(huán)中") # 打印鏈表 def print_circle(self): if self.lenth == 0: print('空環(huán)') return cur = self.tail.next # 頭節(jié)點 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實現(xiàn)循環(huán)鏈表的示例代碼的詳細內(nèi)容,更多關于python/golang 循環(huán)鏈表的資料請關注腳本之家其它相關文章!
- python單向循環(huán)鏈表實例詳解
- Python數(shù)據(jù)結(jié)構之循環(huán)鏈表詳解
- python實現(xiàn)數(shù)據(jù)結(jié)構中雙向循環(huán)鏈表操作的示例
- python單向循環(huán)鏈表原理與實現(xiàn)方法示例
- Python雙向循環(huán)鏈表實現(xiàn)方法分析
- Python實現(xiàn)的單向循環(huán)鏈表功能示例
- Python數(shù)據(jù)結(jié)構與算法之鏈表定義與用法實例詳解【單鏈表、循環(huán)鏈表】
- python雙向鏈表實例詳解
- Python實現(xiàn)雙向鏈表基本操作
- python雙向循環(huán)鏈表實例詳解
相關文章
Python中創(chuàng)建相關系數(shù)矩陣的方法小結(jié)
相關系數(shù)矩陣是一種用于衡量變量之間關系的重要工具,本文將介紹在 Python 中創(chuàng)建相關系數(shù)矩陣的不同方法,感興趣的小伙伴可以跟隨小編一起學習一下2023-12-12Pytest單元測試框架如何實現(xiàn)參數(shù)化
這篇文章主要介紹了Pytest單元測試框架如何實現(xiàn)參數(shù)化,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-09-09Python深入06——python的內(nèi)存管理詳解
本篇文章主要介紹了python的內(nèi)存管理詳解,語言的內(nèi)存管理是語言設計的一個重要方面。它是決定語言性能的重要因素。有興趣的同學可以了解一下。2016-12-12conda虛擬環(huán)境下使用pyinstaller打包程序為exe文件全過程
pyinstaller是一個支持跨平臺使用的第三方庫,它可以將腳本執(zhí)行所需的模塊和庫,自動分析、收集并生成一個文件夾或者可執(zhí)行文件,這篇文章主要給大家介紹了關于conda虛擬環(huán)境下使用pyinstaller打包程序為exe文件的相關資料,需要的朋友可以參考下2023-12-12