gpt4 book ai didi

python - 帮助使用 Python 中的循环链表

转载 作者:行者123 更新时间:2023-11-28 20:10:58 32 4
gpt4 key购买 nike

我正在尝试制作一个循环单向链表。我希望能够针对单个喜欢的列表修改我的代码,但我遇到了一些麻烦。

对于我的链接列表,我有:

class Link (object):
def __init__ (self, data, next = None):
self.data = data
self.next = next


class LinkedList(object):
def __init__(self):
self.first = None

def __str__(self):
a = "["
current = self.first
while current != None:
a += str(current.data) + ', '
current = current.next
a = a[:-2] + ']'
return a

def __iter__(self):
current = self.first
a = []
while current != None:
a += [current.data]
current = current.next
return iter(a)

def __len__ (self):
current = self.first
a = []
while current != None:
a += [current.data]
current = current.next
return len(a)

def InsertFirst(self, item):
NewLink = Link(item, self.first)
self.first = NewLink

def InsertLast(self, item):
NewLink = Link(item)
current = self.first

if current == None:
self.first = NewLink
return

while current.next != None:
current = current.next
current.next = NewLink

def Search(self, item):
count = 0
current = self.first
while current != None:
count += 1
if current.data == item:
return count
else:
pass
current = current.next
return -1

def Delete(self, item):
current = self.first
previous = self.first

if (current == None):
return None

while (current.data != item):
if (current.next == None):
return None
else:
previous = current
current = current.next

if (current == self.first):
self.first = self.first.next
else:
previous.next = current.next

return current

到目前为止,对于我的循环列表,我有:

class Link (object):
def __init__ (self, data, next = None):
self.data = data
self.next = next


class CircularList(object):
def __init__(self):
self.first = Link(None, None)
self.head = Link(None, self.first)

def __str__(self):
a = "["
current = self.first
while current != None:
a += str(current.data) + ', '
current = current.next
a = a[:-2] + ']'
return a

def InsertLast(self, item):
NewLink = Link(item)
current = self.first

if current == None:
self.first = NewLink
return

while current.next != None:
current = current.next
current.next = Link(item)

我的问题是如何将最后一个元素链接回第一个元素以便横向移动?

最佳答案

循环链表的要点是跳过所有“if next is not None”逻辑。一开始,head指向自己,说明链表是空的。无需创建一个空的“第一个”——在一开始就做:

self.head = Link(None, None)
self.head.next = self.head

然后要在其他节点之后插入一个节点,您只需执行以下操作:

def insert_after(insert_node, after_node):
insert_node.next = after_node.next
after_node.next = insert_node

要在列表的开头插入,请执行:

insert_after(node, head)

插入之前需要迭代以找到“之前”节点,因为列表仅是单链接的:

def insert_before(node, before_node):
loc = head
while loc.next is not before_node:
loc = loc.next
insert_after(insert_node, loc)

要在列表末尾插入,请执行:

insert_before(node, head)

要获取列表中的所有元素,请执行以下操作:

current = self.head.next
while current is not self.head:
# do something with current.data

# advance to next element
current = current.next

但循环列表的真正威力在于使其具有双向链接,因此您可以在不迭代的情况下插入。

关于python - 帮助使用 Python 中的循环链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5451548/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com