示例#1
0
 def InsertAtBegin(self, data):
     new_node = Node(data)
     if self.start is None:
         self.start = new_node
         return
     else:
         new_node.next = self.start
         self.start.prev = new_node
         self.start = new_node
 def InserAtEnd(self, data):
     new_node = Node(data)
     if self.start is None:
         self.start = new_node
         return
     else:
         temp = self.start
         while (temp.next):
             temp = temp.next
         temp.next = new_node
         new_node.prev = temp
示例#3
0
 def InsertAtGivenPosition(self, pos, data):
     new_node = Node(data)
     if (pos == 0):
         self.InsertAtBegin(data)
     else:
         current = self.start
         previous = None
         current_position = 0
         while (current_position < pos):
             previous = current
             current = current.next
             current_position += 1
         new_node.next = current
         previous.next = new_node
         new_node.prev = previous
         current.prev = new_node
示例#4
0
 def append(self, data, *args):
     if isinstance(data, Node): data = data.data
     self.size += 1
     node = Node(data, previous=self.tail)
     if self.tail: self.tail.next = node
     else: self.head = node
     self.tail = node
     for arg in args:
         self.append(arg)
示例#5
0
 def add(self, data, *args):
     if isinstance(data, Node): data = data.data
     self.size += 1
     node = Node(data, self.head)
     if self.head: self.head.previous = node
     else: self.tail = node
     self.head = node
     for arg in args:
         self.add(arg)
示例#6
0
 def insert(self, data, index):
     if isinstance(data, Node): data = data.data
     if index == 0:
         self.add(data)
         return
     if index == self.size:
         self.append(data)
         return
     node = self.get(index)
     prev = node.previous
     new = Node(data, node, prev)
     prev.next, node.previous = new, new
     self.size += 1
示例#7
0
 def createNode(self, val):
     node = Node()
     node.setVal(val)
     node.setPrev(None)
     node.setNext(None)
     return node