亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

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

python/golang實現(xiàn)循環(huán)鏈表的示例代碼

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

循環(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)鏈表的資料請關注好吧啦網(wǎng)其它相關文章!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 欧美视频一区二区三区 | 国产v视频| 国产欧美一区二区成人影院 | 国产91在线视频 | 国内精品免费 | 日韩精品一区二区三区四区 | 6080欧美一区二区三区四区 | 日韩成人在线观看视频 | 手机看片中文字幕 | 精品国产一区二区三区成人 | 国产乱视频在线观看播放 | 亚洲精品成人一区 | www.黄色片| 免费网站看v片在线香蕉 | 很黄很黄视频 | 人人干视频在线观看 | 亚洲在线观看一区二区 | 狠狠综合欧美综合欧美色 | 激情在线观看视频免费的 | 三黄日本三级在线观看 | 91在线看| 成人性视屏 | 久久视频精品线视频在线网站 | 国产精品伦理一二三区伦理 | 国产98在线传媒在线视频 | 国产毛片在线高清视频 | 国产91精品新入口 | 人妖三人交69欧美 | 亚洲一区二区约美女探花 | 99久久综合狠狠综合久久 | 91一区二区三区四区五区 | 1024在线观看国产天堂 | 青草视频在线观看国产 | 国产一区免费观看 | 一级特黄aaa大片免费看 | 在线中文字幕第一页 | 亚洲精品一区国产二区 | 俄罗斯小屁孩cao大人免费 | 成人性视频在线三级 | chinese国产videoxx实拍 | 快使劲弄我视频在线播放 |