Example #1
0
 def append(self, value):
     newNode = TwoWayNode(value)
     if self.size == 0:
         self.tail = newNode
         self.head = newNode
     else:
         self.tail.next = newNode
         newNode.previous = self.tail
         self.tail = self.tail.next
     self.size += 1
 def append(self, data):
     node = TwoWayNode(data)
     if self.head == None:
         self.head = node
         self.head.next = self.head
         self.tail = self.head
         self.size += 1
     else:
         probe = self.head
         while probe.next != self.head:
             probe = probe.next
         probe.next = node
         node.next = self.head
         node.previous = probe
         self.tail = node
         self.size += 1
    def enqueue(self, data):
        new_node = TwoWayNode(data)

        if self.count == 0:
            self.head = new_node
            self.head.next = self.head
            self.tail = self.head
        else:
            probe = self.head
            while probe.next != self.head:
                probe = probe.next
            probe.next = new_node
            new_node.next = self.head
            new_node.previous = probe
            self.tail = new_node

        self.count += 1
Example #4
0
def makeTwoWay(listHead):
    '''
    This function takes in the "head" of a singly-linked-list, creates a
    doubly-linked-list with the same data, and returns the head and tail of the 
    created list. 
    
    @param listHead: the pointer to the first node of a singly-linked-list 
    '''

    probe = listHead
    newListHead = TwoWayNode(probe.data)  #creates first node and point to it
    tempPointer = newListHead  #pointer used to move forward while creating
    newListTail = None  #to point at the end of the list later
    while probe.next != None and probe.next != listHead:
        probe = probe.next
        tempPointer.next = TwoWayNode(probe.data, tempPointer, None)
        tempPointer = tempPointer.next
    newListTail = tempPointer  #assigns tail pointer to last node
    tempPointer.next = newListHead  #points the "next" pointer of last node to first node
    newListHead.previous = newListTail  #points the "previous" pointer of first node to last node
    print("Circular Doubly linked list created.")
    return (newListHead, newListTail)