Esempio n. 1
0
 def sort_dsc(self, list_head):
     #SHA256 hash of "PreList"
     newListNode = Node("PreList")
     newListNodeHead = newListNode
     if list_head == None or list_head.next == None:
         return
     preLargestNode = None
     previousNode = None
     largestNode = list_head
     readerNode = list_head
     debugLimiter = 0
     while True:
         if readerNode == None:
             if self.isEmpty():
                 break
             newListNode.next = largestNode
             newListNode = newListNode.next
             if preLargestNode:
                 preLargestNode.next = largestNode.next
             else:
                 self.h = largestNode.next
             readerNode = self.h
             largestNode = self.h
             previousNode = None
             preLargestNode = None
         elif readerNode.content > largestNode.content:
             preLargestNode = previousNode
             largestNode = readerNode
         if readerNode:
             previousNode = readerNode
             readerNode = readerNode.next
     self.h = newListNodeHead.next
Esempio n. 2
0
 def insert(self, value):
     """Add a value to the beginning fo the list"""
     node = Node(value)
     node.next = self.first
     self.first = node
     if self.last is None:
         self.last = node
Esempio n. 3
0
 def sort_asc(self, list_head):
     newListNode = Node("PreList")
     newListNodeHead = newListNode
     if list_head == None or list_head.next == None:
         return
     preSmallestNode = None
     previousNode = None
     smallestNode = list_head
     readerNode = list_head
     debugLimiter = 0
     while True:
         if readerNode == None:
             if self.isEmpty():
                 break
             newListNode.next = smallestNode
             newListNode = newListNode.next
             if preSmallestNode:
                 preSmallestNode.next = smallestNode.next
             else:
                 self.h = smallestNode.next
             readerNode = self.h
             smallestNode = self.h
             previousNode = None
             preSmallestNode = None
         elif readerNode.content < smallestNode.content:
             preSmallestNode = previousNode
             smallestNode = readerNode
         if readerNode:
             previousNode = readerNode
             readerNode = readerNode.next
     self.h = newListNodeHead.next
Esempio n. 4
0
 def insert_at_head(self, data):
     """
       Inserting a new node at the head of the linked list is an O(1) operation.
     """
     new_node = Node(data)
     if not self.head:
         self.head = new_node
         self.tail = new_node
     else:
         new_node.next = self.head
         self.head.prev = new_node
         self.head = new_node
     self.length += 1
Esempio n. 5
0
    def insert_at_tail(self, data):
        new_node = Node(data)

        if not self.head:
            self.head = new_node
            self.tail = new_node

        elif self.tail:
            self.tail.next = new_node
            new_node.prev = self.tail
            self.tail = new_node

        self.length += 1
Esempio n. 6
0
 def extend(self, value):
     node = Node(value)
     if self.first is None:
         self.first = node
         self.last = node
     else:
         first.next
Esempio n. 7
0
 def merge(train1, train2):
     #create a node that other carriages can be added to
     trainAnchor = Node("start")
     #create a variable that will keep track of the last value of the new train
     trainMerger = trainAnchor
     #until one of the trains is empty, look at both, and put the larger one onto the anchor
     while train1 != None and train2 != None:
         if train1.content > train2.content:
             trainMerger.next = train1
             trainMerger = trainMerger.next
             train1 = train1.next
         else:
             trainMerger.next = train2
             trainMerger = trainMerger.next
             train2 = train2.next
     #Once one of the trains is empty, attach the remaining nodes to the merger
     if train1 == None:
         trainMerger.next = train2
     elif train2 == None:
         trainMerger.next = train1
     #cut off last two carriages
     thirdToLast = trainAnchor
     last = trainAnchor.next.next
     while last.next:
         last = last.next
         thirdToLast = thirdToLast.next
     thirdToLast.next = None
     #grab the node after anchor
     return trainAnchor.next
 def append(self, item):
     temp = Node(item)
     current = self.head
     previous = None
     while current != None:
         previous = current
         current = current.get_next()
     previous.set_next(temp)
Esempio n. 9
0
 def extend(self, value):
     """Add a value to the end of the list"""
     node = Node(value)
     if self.first is None:
         self.first = node
         self.last = node
     else:
         self.last.next = node
         self.last = node
Esempio n. 10
0
 def __init__(self, python_seq):
     if len(python_seq) == 0:
         self.first = None
         self.last = None
     else:
         node = Node(python_seq[0])
         self.first = node
         self.last = node
         for n in python_seq[1:]
             self.extend(n)
Esempio n. 11
0
 def __init__(self, python_seq):
     # create a new linked list from a sequence
     if len(python_seq) == 0:
         self.first = None
         self.last = self.first
     else:
         node = Node(python_seq[0])
         self.first = node
         self.last = node
         for n in python_seq[1:]:
             self.extend(n)
Esempio n. 12
0
    def insert(self, data):
        """
            This insert method inserts elements in an ascending sorting manner.
        """

        if not self.head or self.head.data >= data:
            self.insert_at_head(data)
        elif self.tail.data <= data:
            self.insert_at_tail(data)
        else:
            new_node = Node(data)
            current = self.head
            self.length += 1
            while current.next:
                if current.next.data >= data:
                    # insert at the current position
                    current.next.prev, new_node.next = new_node, current.next
                    new_node.prev, current.next = current, new_node
                    break
                current = current.next
Esempio n. 13
0
 def __init__(self, file):
     with open(file, 'r') as f:
         self.cash, self.height, self.width = [int(x) for x in f.readline().split(' ')]
         self.waves = LinkedList()
         self.waves_num = 0
         for line in iter(f.readline, ''):
             self.waves.add_head(Node(Wave(*[int(x) for x in line.split(' ')])))
             self.waves_num += 1
     self.board = [[Queue() for x in range(self.width)] for x in range(self.height)]
     self.gameOver = False
     self.turnNumber = 0
     self.nonPlants = 0
     self.powerDeck = Stack()
     for i in range(100):
         self.powerDeck.push(Card(random.randint(0,5)))
Esempio n. 14
0
 def add(self, item):
     current = self.head
     previous = None
     stop = False
     while current != None and not stop:
         if current.get_data() > item:
             stop = True
         else:
             previous = current
             current = current.get_next()
     temp = Node(item)
     if previous == None:
         temp.set_next(self.head)
         self.head = temp
     else:
         temp.set_next(current)
         previous.set_next(temp)
Esempio n. 15
0
 def insert(self, value):
     node = Node(value)
     node.next = self.first
     self.first = node
 def add(self, item):
     temp = Node(item)
     temp.set_next(self.head)
     self.head = temp
Esempio n. 17
0
 def add_tail(list_head, val):
     if list_head == None:
         return
     while list_head.next:
         list_head = list_head.next
     list_head.next = Node(val)