亚洲免费在线视频-亚洲啊v-久久免费精品视频-国产精品va-看片地址-成人在线视频网

您的位置:首頁技術文章
文章詳情頁

python/golang實現循環鏈表的示例代碼

瀏覽:2日期:2022-07-11 11:42:27

循環鏈表就是將單鏈表的末尾指向其頭部,形成一個環。循環鏈表的增刪操作和單鏈表的增刪操作區別不大。只是增加時,需要考慮空鏈表增加第一個節點的特殊情況;刪除時需考慮刪除節點是頭/尾節點,和鏈表中只有一個節點的特殊情況。

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實現循環鏈表的示例代碼的詳細內容,更多關于python/golang 循環鏈表的資料請關注好吧啦網其它相關文章!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 欧美一级毛片免费高清aa | 欧美成人爽毛片在线视频 | 毛片1级| 狠狠做久久深爱婷婷97动漫 | 2021国产精品系列一区二区 | 国产三级精品播放 | 中文国产成人精品久久96 | 成人看的一级毛片 | 性欧美videos精品 | 欧美在线黄色 | 日韩午夜在线观看 | 免费高清欧美一区二区视频 | 国产精品爱久久久久久久小 | 丝袜一级片 | 草草影院ccyy免费看片 | 亚洲国产成人精品久久 | 黄色影院在线观看视频 | 性刺激免费视频观看在线观看 | 视频偷拍一级视频在线观看 | 午夜性爽快免费视频播放 | 亚洲成人中文字幕 | 中文在线最新版天堂 | 午夜在线亚洲男人午在线 | 美女张开腿让男人操 | 91一区| 日韩欧美高清在线 | 午夜在线播放免费人成无 | 国产精品亚洲一区二区三区 | 99久久香蕉 | 久久久国产精品免费看 | 成人手机视频在线观看 | 久久国产情侣 | 末成年娇小性色xxxxx | 久久久久久综合对白国产 | 男女超猛烈啪啦啦的免费视频 | 欧美成人免费一级人片 | 久久久9视频在线观看 | 亚洲福利精品一区二区三区 | 久久狠狠躁免费观看2020 | 国产精品久久久久久久毛片 | 亚洲精品播放 |