最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2020年09月14日 10:55:29   作者:NothingLeft了  
這篇文章主要介紹了python/golang如何實(shí)現(xiàn)循環(huán)鏈表,幫助大家更好的理解和學(xué)習(xí)循環(huán)鏈表的實(shí)現(xiàn)方法,感興趣的朋友可以了解下

循環(huán)鏈表就是將單鏈表的末尾指向其頭部,形成一個(gè)環(huán)。循環(huán)鏈表的增刪操作和單鏈表的增刪操作
區(qū)別不大。只是增加時(shí),需要考慮空鏈表增加第一個(gè)節(jié)點(diǎn)的特殊情況;刪除時(shí)需考慮刪除節(jié)點(diǎn)是頭/尾節(jié)點(diǎn),和鏈表中只有一個(gè)節(jié)點(diǎn)的特殊情況。

golang實(shí)現(xiàn):

type Node struct {
 value int
 next *Node
}

type Circle struct {
 tail *Node
 lenth int
}

// 增加節(jié)點(diǎn):
func (c *Circle) add(value int) {
 newNode := &Node{value, nil}
 if c.lenth == 0 { //空鏈表中添加節(jié)點(diǎn)
 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é)點(diǎn):
func (c *Circle) remove(v int) {
 if c.lenth == 0 {
 fmt.Println("空環(huán)")
 return
 } else if c.lenth == 1 && c.tail.value == v { //鏈表中只有一個(gè)節(jié)點(diǎn)的特殊情況
 c.tail = nil
 c.lenth = 0
 c.printCircle()
 return
 }
 pre := c.tail
 cur := c.tail.next // 頭節(jié)點(diǎn)
 for i := 0; i < c.lenth; i++ {
 if cur.value == v {
  if cur == c.tail { //如果刪除的節(jié)點(diǎn)是尾節(jié)點(diǎn),需更新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é)點(diǎn):
func (c *Circle) printCircle() {
 if c.lenth == 0 {
 fmt.Println("空環(huán)")
 return
 }
 cur := c.tail.next // 頭節(jié)點(diǎn)
 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實(shí)現(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é)點(diǎn)
 def add(self, v):
 new_node = Node(v)
 if self.lenth == 0: # 空鏈表中添加節(jié)點(diǎn)
  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é)點(diǎn)
 def remove(self, v):
 if self.lenth == 0:
  print("空環(huán)")
  return
 elif self.lenth == 1 and self.tail.value == v: # 鏈表中只有一個(gè)節(jié)點(diǎn)的特殊情況
  self.tail = None
  self.lenth = 0
  return
 pre = self.tail
 cur = self.tail.next # 頭節(jié)點(diǎn)
 for i in range(self.lenth):
  if cur.value == v:
  if cur == self.tail: # 如果刪除的節(jié)點(diǎn)是尾節(jié)點(diǎn),需更新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é)點(diǎn)
 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實(shí)現(xiàn)循環(huán)鏈表的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于python/golang 循環(huán)鏈表的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

辽宁省| 淮滨县| 英吉沙县| 格尔木市| 曲麻莱县| 石嘴山市| 龙川县| 安陆市| 大邑县| 进贤县| 咸丰县| 内丘县| 黄大仙区| 大田县| 英吉沙县| 灵宝市| 宜城市| 三穗县| 凤翔县| 赤城县| 贡山| 伊金霍洛旗| 马龙县| 仪征市| 霍林郭勒市| 奉贤区| 徐州市| 绿春县| 榆林市| 萍乡市| 锦屏县| 汤阴县| 内乡县| 志丹县| 尖扎县| 黄平县| 亳州市| 红原县| 临潭县| 津市市| 大渡口区|