Example #1
0
class Queue:
    def __init__(self):
        self.items = UnorderedList()

    def enqueue(self, item):
        self.items.add(item)

    def dequeue(self):
        return self.items.pop()

    def isEmpty(self):
        return self.items.isEmpty()

    def size(self):
        return self.items.length()
Example #2
0
class Deque:
    def __init__(self):
        self.items = UnorderedList()

    def __str__(self):
        return self.items.__str__()

    def addFront(self, item):
        self.items.add(item)

    def addRear(self, item):
        self.items.append(item)

    def removeFront(self):
        return self.items.popPos(0)

    def removeRear(self):
        return self.items.pop()

    def isEmpty(self):
        return self.items.isEmpty()

    def size(self):
        return self.items.length()
Example #3
0
class Stack:
    def __init__(self):
        self.items = UnorderedList()

    def __str__(self):
        return self.items.__str__()

    def push(self, item):
        self.items.append(item)

    def pop(self):
        return self.items.pop()

    def peek(self):
        current = self.items.head
        for i in range(self.items.length() - 1):
            current = current.getNext()
        return current.getData()

    def size(self):
        return self.items.length()
# Design an experiment to compare the performance of the py list with the linked list.
import timeit
import random
from Chapter3_13to19 import UnorderedList

t1 = timeit.Timer("l1.append(random.random())", "from __main__ import random, l1")
t2 = timeit.Timer("l2.append(random.random())", "from __main__ import random, l2")

l1 = []
l2 = UnorderedList()
res1 = []
res2 = []
for i in range(1000, 100000, 10000):
    pt1 = t1.timeit(number=1000)
    pt2 = t2.timeit(number=1000)
    res1.append(pt1)
    res2.append(pt2)

#res1 =  [0.00028382599999998703, 0.0001261449999998554, 0.00012568100000009963, 0.00012521700000012181,
# 0.0001210440000001256, 0.0001247529999996999, 0.0001303189999997123, 0.00013124600000047337, 0.0001284639999994397,
# 0.0001228979999998714]

#res2 =[0.06751851400000008, 0.2005455759999999, 0.341864336, 0.48889856600000003, 0.6699139669999998, 0.753277561,
# 0.871266034, 1.0232602579999996, 1.1521120199999997, 1.2844981549999996]

#The performance is way lower in our implementation. Append with python lists is O(1) and in our implementation is
# O(n)
Example #5
0
 def __init__(self):
     self.items = UnorderedList()