Python 中循环链表的帮助
我正在尝试制作一个循环单链表。我希望能够修改我的代码以获得单一喜欢的列表,但我遇到了一些麻烦。
对于我的链接列表,我有:
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)
我的问题是如何将最后一个元素链接回第一个元素,以便我可以横向?
I'm trying to make a circular singly linked list. I'd like to be able to modify my code for a singly liked list but I'm have some trouble.
For my linked list I have:
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
So far for my circular list I have:
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)
My question is how do I link the last element back to the first so I can transverse?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Node 类将创建一个空节点,插入函数将通过调用 Node 类创建一个节点。然后我们检查头节点,如果为空,则创建新节点作为头节点。否则搜索节点指针为 Null 并插入新节点。
The class Node will create an empty node and the insert function will create a node by calling Node class. Then we check for the head node and in case empty , create the new node as head node.else search for the node pointer to be Null and insert the new node.
循环链表的要点是跳过所有“如果下一个不是 None”逻辑。一开始,头指向自身,表明链表为空。不需要创建一个空的“第一个” - 在开始时 do:
然后要在其他节点之后插入一个节点,只需执行以下操作:
要在列表的开头插入,请执行:
Insert before 需要迭代来查找“before”节点,因为列表只是单向链接:
要在列表末尾插入,请执行以下操作:
要获取列表的所有元素,请执行:
但循环列表中的真正功能是使其成为双向链接,因此您可以插入之前而不迭代。
The point of a circular linked list is to skip all of the "if next is not None" logic. At the beginning, the head points to itself, indicating that the list is empty. There is no need to create an empty "first" - at the very start do:
Then to insert a node after some other node, you just do:
To insert at the beginning of the list, do:
Insert before requires iterating to find the "before" node, since the list is only singly linked:
To insert at the end of the list, do:
To get all elements of the list do:
But the real power in a circular list is to make it doubly linked, so you can insert before without iterating.
last.next = 第一个创建时?
可能不是有效的代码。但既然你在创建时保证位于列表的最后一部分,那么你也可以。
last.next = first when created?
Might not be valid code. But since you're guaranteed to be at the last part of the list when creating then you might as well.
class Circular_Link(对象):
此示例从普通列表初始化循环列表。
class Circular_Link (object):
This example initializes the circular list from a normal list.