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

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

python/golang 刪除鏈表中的元素

瀏覽:2日期:2022-07-11 11:34:50

先用使用常規方法,兩個指針:

golang實現:

type Node struct { value int next *Node}type Link struct { head *Node tail *Node lenth int}// 向鏈表中添加元素func (link *Link) add(v int) { if link.lenth == 0 { // 當前鏈表是空鏈表 link.head = &Node{v, nil} link.tail = link.head link.lenth = 1 } else { newNond := &Node{v, nil} link.tail.next = newNond link.tail = newNond link.lenth += 1 }}// 刪除鏈表中的元素(雙指針)func (link *Link) remove(v int) { if link.lenth == 0 { fmt.Println('空鏈表,不支持該操作') return } var previous *Node = nil for current := link.head; current != nil; current = current.next { if current.value == v { if current == link.head { // 要刪除的是頭節點link.head = current.next } else if current == link.tail { // 要刪除的是尾節點previous.next = nillink.tail = previous } else { // 要刪除的是中間的節點previous.next = current.next } link.lenth -= 1 break } previous = current }}// 打印鏈表func (link *Link) printList() { if link.lenth == 0 { fmt.Println('空鏈表') return } for cur := link.head; cur != nil; cur = cur.next { fmt.Printf('%d ', cur.value) } fmt.Println()}

python實現:

class Node: def __init__(self, value, next): self.value = value self.next = next def __str__(self): return str(self.value)class Link: def __init__(self): self.head = None self.tail = None self.lenth = 0 # 向鏈表中添加元素 def add(self, v): if self.lenth == 0: # 當前鏈表是空鏈表 self.head = Node(v, None) self.tail = self.head self.lenth = 1 else: new_node = Node(v, None) self.tail.next = new_node self.tail = new_node self.lenth += 1 # 打印鏈表 def print(self): if self.lenth == 0: print(’空鏈表’) return cur = self.head while True: if cur == None:print()break print(cur, end=’ ’) cur = cur.next # 刪除鏈表中的元素 def remove(self, v): if self.lenth == 0: return cur = self.head pre = None while True: if cur.value == v:if cur == self.head: # 要刪除的是頭節點 self.head = cur.nextelif cur == self.tail: # 要刪除的是尾節點 pre.next = None self.tail = preelse: # 要刪除的是中間的節點 pre.next = cur.nextself.lenth -= 1break pre = cur cur = cur.next if cur == None:print('未找到', v)break

只使用使用一個指針實現鏈表的刪除:

python/golang 刪除鏈表中的元素

golang實現:

func (link *Link) remove_with_one_pointer(v int) { if link.lenth == 0 { return } if link.tail.value == v { // 要刪除的節點是尾節點,需特殊處理 if link.lenth == 1 { // 如果鏈表只有一個節點 link.head = nil link.tail = nil } else { //大于一個節點 cur := link.head for ; cur.next.next != nil; cur = cur.next { } //找到尾節點的前一個節點 cur.next = nil link.tail = cur } link.lenth -= 1 return } //要刪除的節點在頭部/中間 的常規情況 for cur := link.head; cur != nil; cur = cur.next { if cur.value == v { cur.value = cur.next.value cur.next = cur.next.next link.lenth -= 1 return } } fmt.Println('未找到', v)}

python實現:

def remove_with_one_pointer(self, v): if self.lenth == 0: return if self.tail.value == v: # 要刪除的節點是尾節點,需特殊處理 if self.lenth == 1: # 如果鏈表只有一個節點 self.head = None self.tail = None else: # 大于一個節點 cur = self.head while True:if cur.next.next is None: # 找到尾節點的前一個節點 breakelse: cur = cur.next cur.next = None self.tail = cur self.lenth -= 1 return # 要刪除的節點在頭部/中間 的常規情況 cur = self.head while True: if cur.value == v: cur.value = cur.next.value cur.next = cur.next.next self.lenth -= 1 break cur = cur.next if cur is None: print(’未找到’, v) break

以上就是python/golang 刪除鏈表中的元素的詳細內容,更多關于python/golang 鏈表的資料請關注好吧啦網其它相關文章!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 日韩欧美中文字幕在线播放 | 狠狠亚洲婷婷综合色香五 | 国产成人亚洲精品乱码在线观看 | 成人亚洲欧美日韩在线观看 | 国产成人免费高清在线观看 | 亚洲国产一区二区三区青草影视 | 高清国产一区 | 特级毛片aaaa级毛片免费 | 黄色毛片小视频 | 亚洲天天在线日亚洲洲精 | 黄色aaaaa | 国产九九在线视频 | 91高清国产经典在线观看 | 香蕉视频在线免费 | 欧美成人精品福利在线视频 | 国产三级黄色 | 日本一级毛片私人影院 | 精品国产综合区久久久久99 | 超级碰碰碰免费视频播放 | 精品国产美女福到在线不卡f | 久久天天躁狠狠躁夜夜中文字幕 | 国产美女自拍视频 | 国内毛片 | 中文字幕第一页在线视频 | 日本高清不卡中文字幕 | 一级免费黄色片 | 99视频只有精品 | 东京一区二区三区高清视频 | 亚洲美女综合网 | 97se亚洲综合在线韩国专区福利 | 深爱激情四射 | 国产免费一区二区 | 国产偷v国产偷v亚洲偷v | 国内视频拍拍视频在线观看 | 国产成人精品久久综合 | 在线观看国产高清免费不卡黄 | a毛片免费视频 | 欧美一区二区三区国产精品 | 欧美亚洲黄色片 | 欧美成人精品福利在线视频 | 丝袜美腿国产精品视频一区 |