【发布时间】:2018-06-15 04:05:53
【问题描述】:
Node类和SingleLinkedList类代码:
class Node():
'''represents a node as a building block of a single linked list'''
def __init__(self, element, next_node=None):
'''(Node, obj, Node) -> NoneType
construct a node as building block of a single linked list'''
self._element = element
self._next = next_node
def set_next(self, next_node):
'''(Node, Node) -> NoneType
set node to point to next_node'''
self._next = next_node
def set_element(self, element):
'''(Node, obj) ->NoneType
set the _element to a new value'''
self._element = element
def get_next(self):
'''(Node) -> Node
returns the reference to next node'''
return self._next
def get_element(self):
'''(Node) -> obj
returns the element of this node'''
return self._element
def __str__(self):
'''(Node) -> str
returns the element of this node and the reference to next node'''
return "(" + str(self._element) + ", " + str(hex(id(self._next))) + ")"
class SingleLinkedList():
''' represents a single linked list'''
def __init__(self):
'''(SingleLinkedList) ->NoneType
initializes the references of an empty SLL'''
self._size = 0
self._head = None
self._tail = None
self._value = None
self._next = None
def set_head(self, new_head):
'''(SingleLinkedList) -> None
updates the head'''
self._head = new_head
def set_tail(self, new_tail):
'''(SingleLinkedList) -> None
updates the tail'''
self._tail = new_tail
def get_head(self):
'''(SingleLinkedList) -> Node
Return the pointer to the head'''
return self._head
def get_tail(self):
'''(SingleLinkedList) -> Node
Return the pointer to the tail'''
return self._tail
def is_empty(self):
'''(SingleLinkedList) -> bool
returns true if no item is in this SLL'''
return self._size == 0
def size(self):
'''(SingleLinkedList) -> int
returns the number of items in this SLL'''
return self._size
def add_first(self, element):
'''(SingleLinkedList, obj) -> NoneType
adds a node to the first of the SLL'''
# create a node that point to the head
node = Node(element, self._head)
# let head point to the node
self._head = node
# if this node is the first node in this SLL, tail should point to this node too
if (self._size == 0):
self._tail = node
# increment the size
self._size += 1
def add_last(self, element):
'''(SingleLinkedList, obj) -> NoneType
adds a node to the end of this SLL'''
# create a node with the given element that points to None
node = Node(element, None)
# let the _next part of the tail to point to newly created node
self._tail.set_next(node)
#let tail to point to the added node
self._tail = node
# if this node is the first node in this SLL, let head to point to this node too
if (self._size == 0):
self._head = node
# increment the size
self._size += 1
def remove_first(self):
'''(SingleLinkedList, obj) -> obj
remvoe the node from the head of this SLL and returns the element stored in this node'''
# sset element to None in case SLL was empty
element = None
# if SLL is not empty
if (self._head is not None):
# get the first node
node = self._head
# let head point to the second node
self._head = self._head.get_next()
# decrement the size
self._size -= 1
#set the _next of previous head to point to None (for garbage collection purpose)
node.set_next(None)
# get the element stored in the node
element = node.get_element()
#return the element of the removed node
return element
def __str__(self):
'''(SingleLinkedList) -> str
returns the items in the SLL in a string form
'''
# define a node, which points to the head
cur = self._head
# define an empty string to be used as a container for the items in the SLL
result = ""
# loop over the SLL until you get to the end of the SLL
while (cur is not None):
# get the element that of the current node and attach it to the final result
result = result + str(cur.get_element()) + ", "
# proceed to next node
cur = cur.get_next()
#enclose the result in a parantheses
result = "(" + result[:-2] + ")"
#return the result
return result
如您所见,已经有在头部添加和在尾部添加的功能,但是我不知道如何在列表中间添加。我需要创建一个获取新数据的函数,并在单链表中间添加一个带有数据的节点。有人可以向我展示代码或如何修改这些功能之一或添加新功能吗?感谢您的帮助!
【问题讨论】:
-
这个问题只要在谷歌上搜索就可以解决
-
破坏您自己的问题违反了网站的 TOS。当您在这里询问时,您授予版权;见meta.stackexchange.com/questions/209436/…
-
请删除问题或恢复编辑
-
请不要通过破坏您的帖子为人们增加工作量。通过在 Stack Exchange (SE) 网络上发帖,您已在 CC BY-SA 3.0 license 下授予 SE 分发该内容的不可撤销权利(即无论您未来的选择如何)。根据 SE 政策,帖子的非破坏版本是分发的版本。因此,任何破坏行为都将被撤销。
-
(也就是说,这很可能因为缺少 minimal reproducible example 而被关闭——因为代码没有什么最小的——或者可能过于宽泛)
标签: python list singly-linked-list