Пример #1
0
class LinkListStack(object):
    def __init__(self, size):
        self.top = 0
        self._size = size
        self._items = LinkList()

    def __len__(self):
        return self.top

    def push(self, value):
        if self.top >= self._size:
            raise Exception("stack is full")

        self._items.append(value)
        self.top += 1

    def pop(self):
        if self.top <= 0:
            raise Exception("stack is empty")

        v = self._items.pop()
        self.top -= 1
        return v

    def is_empty(self):
        return self.top == 0
def test_append():
    ll = LinkList()
    ll.insert('2')
    ll.insert('3')
    ll.insert('1')
    ll.append('5')
    expected = '1,3,2,5'
    actual = ll.toStr()
    assert expected == actual
Пример #3
0
class Queue():
    def __init__(self):
        self._data = LinkList()

    def count(self) -> int:
        return self._data.count()

    def toStr(self) -> str:
        return self._data.toStr()

    def enqueue(self, val) -> bool:
        # Add a value to the queue
        return self._data.append(val)

    def dequeue(self, val) -> bool:
        # Remove entry from queue with a given value
        # NOTE: will only remove the first element found with val
        return self._data.remove(val)

    def peek(self) -> [bool, object]:
        # Get value from the head of the queue (without removing it)
        return self._data.peekHead()
def test_ll_merge():

    # @TODO: TEST: Merge two unequal
    # @TODO: TEST: Merge one empty list
    # @TODO: TEST: Merge two empty lists
    # @TODO: TEST: Merge a list with just 1 item

    listA = LinkList()
    listA.append('apple')
    listA.append('bannana')
    listA.append('orange')

    listB = LinkList()
    listB.append('cheerios')
    listB.append('frosted flakes')
    listB.append('wheaties')

    listA.mergeList(listA, listB)

    expected = 'apple,cheerios,bannana,frosted flakes,orange,wheaties'
    actual = listA.toStr()

    assert expected == actual