def removeElements(head, val): if head == None: return None new = Node(0) new.next = head head = new while head.next != None: if head.next.data == val: head.next = head.next.next else: head = head.next return new.next
def add(self, item): """将item插入queue的队尾.""" newNode = Node(item, None) if self.isEmpty(): self._front = newNode else: self._rear.next = newNode self._rear = newNode self._size += 1
def append(linked, new): newNode = Node(new) if linked is None: linked = newNode else: probe = linked while probe.next != None: probe = probe.next probe.next = newNode
def add(self, item): """将item插入大于或等于优先级的items之后或者插入优先级较低的item之前. 如果A < B,则A优先于B.""" if self.isEmpty() or item >= self._rear.data: # 将item插入队尾 LinkedQueue.add(self, item) else: # 搜索比item优先级更低的item的位置 probe = self._front while item >= probe.data: trailer = probe probe = probe.next newNode = Node(item, probe) if probe == self._front: # 将item插入队头 self._front = newNode else: # 将item插入两个节点之间 trailer.next = newNode self._size += 1
def add(self, item): """将item添加进self.""" self._items = Node(item, self._items) self._size += 1
def add(linked, new): linked = Node(new, linked)
# encoding: utf-8 """ File: testnode.py Author: Rock Johnson """ from dataStructure.linkeds.node import Node head = None # 将5个节点添加到链接结构的开头 for count in range(1, 6): head = Node(count, head) # 遍历并打印链表结构的节点 def toStr(linked): probe = linked while probe != None: print(probe.data) probe = probe.next # 搜索链表结构 def search(linked, target): probe = linked while probe != None and target != probe.data: probe = probe.next if probe != None: print("Found") else: print("Not found")