本文整理汇总了Python中Node.Node.setNext方法的典型用法代码示例。如果您正苦于以下问题:Python Node.setNext方法的具体用法?Python Node.setNext怎么用?Python Node.setNext使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Node.Node
的用法示例。
在下文中一共展示了Node.setNext方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: addAtIndex
# 需要导入模块: from Node import Node [as 别名]
# 或者: from Node.Node import setNext [as 别名]
def addAtIndex(self, index, data):
# Continue if index is valid
if index >= 0 and index <= self.__numberNodes:
if index == 0: # Push element to front of list if index is 0
self.push(data)
elif index == self.__numberNodes: # Add element to end of list if index is last element
self.add(data)
else:
# Continue if list is not empty
if self.__head is not None and self.__tail is not None:
addNode = Node(data)
currentNode = self.__head.getNext()
previousNode = self.__head
count = 1
while currentNode is not None: # Traverse list to find element at index
# Insert element when index is found
if count == index:
previousNode.setNext(addNode)
addNode.setNext(currentNode)
self.__numberNodes = self.__numberNodes + 1
break
# Prepare for next iteration
previousNode = currentNode
currentNode = currentNode.getNext()
count = count + 1
示例2: push
# 需要导入模块: from Node import Node [as 别名]
# 或者: from Node.Node import setNext [as 别名]
def push(self, data):
addNode = Node(data)
# Set head and tail to new pointer if list is empty
if self.__head is None:
self.__head = addNode
self.__tail = addNode
else:
addNode.setNext(self.__head) # Set new node's next pointer to the current head
self.__head = addNode # Set head to new node
self.__numberNodes = self.__numberNodes + 1
示例3: add
# 需要导入模块: from Node import Node [as 别名]
# 或者: from Node.Node import setNext [as 别名]
def add(self, item):
"""
Adds an item to the start of the list
:param item: Any object
:return: Linked list with new object added
"""
tempNode = Node(item)
tempNode.setNext(self.head)
self.head = tempNode
self.lastElem = tempNode
示例4: insert
# 需要导入模块: from Node import Node [as 别名]
# 或者: from Node.Node import setNext [as 别名]
def insert(self,item,idx):
current = self.head
n = 0
while n < idx:
current = current.getNext()
n += 1
insertee = Node(item)
nxt = current.getNext()
current.setNext(insertee)
insertee.setNext(nxt)
示例5: add
# 需要导入模块: from Node import Node [as 别名]
# 或者: from Node.Node import setNext [as 别名]
def add(self, data):
current = self.head
prev = None
while not (current is None):
if current.getData() > data:
break
else:
prev = current
current = current.getNext()
temp = Node(data)
if prev is None:
temp.setNext(self.head)
self.head = temp
else:
temp.setNext(current)
prev.setNext(temp)
示例6: add
# 需要导入模块: from Node import Node [as 别名]
# 或者: from Node.Node import setNext [as 别名]
def add(self, item):
current = self.head
previous = None
stop = False
while current is not None and stop is False:
if current.getData() > item:
stop = True
else:
previous = current
current = current.getNext()
temp = Node(item)
if previous is None:
temp.setNext(self.head)
self.head = temp
else:
previous.setNext(temp)
temp.setNext(current)
示例7: add
# 需要导入模块: from Node import Node [as 别名]
# 或者: from Node.Node import setNext [as 别名]
def add(self,item):
''' this method is different from linked list '''
current = self.head
previous = None
stop = False
while current != None and not stop:
if current.getData() > item:
stop = True
else:
previous = current
current = current.getNext()
temp = Node(item)
if previous == None:
temp.setNext(self.head)
self.head = temp
else:
temp.setNext(current)
previous.setNext(temp)
示例8: setUp
# 需要导入模块: from Node import Node [as 别名]
# 或者: from Node.Node import setNext [as 别名]
def setUp(self):
self.palindrome = PalindromCheck()
node1 = Node()
node2 = Node()
node3 = Node()
node4 = Node()
node5 = Node()
node6 = Node()
node1.setValue(1)
node1.setNext(node2)
node2.setValue(2)
node2.setNext(node3)
node3.setValue(3)
node3.setNext(node4)
node4.setValue(3)
node4.setNext(node5)
node5.setValue(2)
node5.setNext(node6)
node6.setValue(1)
self.palindrome.setValue(node1)
示例9: insert
# 需要导入模块: from Node import Node [as 别名]
# 或者: from Node.Node import setNext [as 别名]
def insert(self, item, index):
"""
Adds an element at the index
:param item: Any object
:param index: An integer
:return:
"""
temp = Node(item)
current = self.head
count = 1
if index == count or self.isEmpty():
self.add(item)
return
while count < index:
current = current.getNext()
count += 1
temp.setNext(current.getNext().getNext())
current.setNext(temp)
return
示例10: addNode
# 需要导入模块: from Node import Node [as 别名]
# 或者: from Node.Node import setNext [as 别名]
def addNode(self, item):
temp = Node(item)
temp.setNext(self.head)
self.head = temp
示例11: add
# 需要导入模块: from Node import Node [as 别名]
# 或者: from Node.Node import setNext [as 别名]
def add(self, data):
temp = Node(data)
temp.setNext(self.head)
self.head = temp
示例12: LinkedList
# 需要导入模块: from Node import Node [as 别名]
# 或者: from Node.Node import setNext [as 别名]
class LinkedList():
def __init__ (self):
self.firstNode = None
def append(self, value):
if self.firstNode is None:
self.firstNode = Node(value)
else:
previous_node = self.firstNode
while True:
if previous_node.getNext() is None:
newNode = Node(value)
previous_node.setNext(newNode)
break
previous_node = previous_node.getNext()
def pop(self):
if self.firstNode is None:
raise IndexError('Out of range')
elif self.firstNode.getNext() is None:
lastNode = self.firstNode
self.firstNode = None
return lastNode.getValue()
else:
lastNode = self.firstNode
while True:
if lastNode.getNext() is None:
currentNode.setNext(None)
break
currentNode = lastNode
lastNode = lastNode.getNext()
return lastNode.getValue()
def prepend(self, value):
if self.firstNode is None:
self.firstNode = Node(value)
else:
previous_node = self.firstNode
self.firstNode = Node(value)
self.firstNode.setNext(previous_node)
def set(self):
pass
def get(self, index):
previous_node = self.firstNode
while True:
if previous_node is None:
raise IndexError()
if index == 0:
return previous_node.getValue()
index -= 1
previous_node = previous_node.getNext()
def length(self):
return len(self)
def __str__ (self):
if self.firstNode is None:
return '[]'
string = '['
previous_node = self.firstNode
while True:
if previous_node.getNext() is None:
string += str(previous_node)
break
else:
string += str(previous_node) + ', '
previous_node = previous_node.getNext()
string += ']'
return string
#.........这里部分代码省略.........