コード例 #1
0
def test_pop(llist):
    assert repr(llist) == 'LinkedList()'
    with pytest.raises(Exception):
        llist.pop(0)
    llist = LinkedList([-10.1, 2, 'car', 4.4, 0])
    assert repr(llist) == 'LinkedList(-10.1, 2, car, 4.4, 0)'
    item = llist.pop(0)
    assert item == -10.1
    assert repr(llist) == 'LinkedList(2, car, 4.4, 0)'
    item = llist.pop()
    assert item == 0
    assert repr(llist) == 'LinkedList(2, car, 4.4)'
    item = llist.pop(1)
    assert item == 'car'
    assert repr(llist) == 'LinkedList(2, 4.4)'
    item = llist.pop()
    assert item == 4.4
    assert repr(llist) == 'LinkedList(2)'
    item = llist.pop()
    assert item == 2
    assert repr(llist) == 'LinkedList()'
    llist.append(-7)
    with pytest.raises(IndexError):
        llist.pop(100)
    with pytest.raises(IndexError):
        llist.pop(-1)
コード例 #2
0
def test_negative_integer_k():
    make_list = LinkedList()
    make_list.append("a")
    make_list.append("b")
    make_list.append("c")
    with pytest.raises(ValueError):
        make_list.k_fromthe_end(-1)
def test_insert_after_last_node():
    object = LinkedList()
    object.append(6)
    object.append(7)
    object.append(8)
    object.insert_after(8, 9)
    assert str(object) == '6=>7=>8=>9=>NULL'
コード例 #4
0
class StackList:
    def __init__(self):
        self._stack = LinkedList()
        self._length = 0

    def show(self):
        if self.is_empty():
            return None
        return self._stack.tail

    def put(self, item):
        self._stack.append(item)
        self._length += 1

    def pop(self):
        if self.is_empty():
            raise Exception("stack underflow")
        self._length -= 1
        return self._stack.pop()

    def is_empty(self):
        return self._length == 0

    @property
    def stack(self):
        return self._stack

    @property
    def length(self):
        return self._length
コード例 #5
0
def test_insertAfter():
    ll = LinkedList()
    ll.append(2)
    ll.append(5)
    ll.append(9)
    ll.insertAfter(9, 3)
    assert ll.head.next.next.next.value == 3
コード例 #6
0
def test_insertAfter():
    ll = LinkedList()
    ll.append(1)
    ll.append(2)
    ll.append(3)
    ll.insertAfter(1, 30)
    assert ll.head.next.value == 30
コード例 #7
0
class QueueList:
    def __init__(self):
        self._queue = LinkedList()
        self._length = 0

    def is_empty(self):
        return self._length == 0

    @property
    def queue(self):
        if self.is_empty():
            return None
        return self._queue

    @property
    def head(self):
        if self.is_empty():
            return None
        return self._queue[0]

    def put(self, item):
        self._queue.append(item)
        self._length += 1

    def pop_left(self):
        self._length -= 1
        return self._queue.pop(0)

    def display(self):
        if self.is_empty():
            print("Circular queue is empty")
        else:
            print("Circular queue:", self._queue)
コード例 #8
0
def test_greater_than_list_length():
    make_list = LinkedList()
    make_list.append("a")
    make_list.append("b")
    make_list.append("c")
    with pytest.raises(IndexError):
        make_list.k_fromthe_end(4)
コード例 #9
0
def test_multiple_append():
    new_instance = LinkedList()
    new_instance.append('test_one')
    new_instance.append('test_two')
    actual = new_instance.includes('test_one') and new_instance.includes('test_two')
    expected = True
    assert actual == expected
コード例 #10
0
def test_insert_before_begining():
  # insert a node before the first node of a linked list
  lst_four = LinkedList()
  lst_four.insert('apple')
  lst_four.append('banana')
  lst_four.insertBefore('apple', 'watermelon')
  assert lst_four.head.value == 'watermelon'
コード例 #11
0
def test_insert_before_middle():
  # insert a node before a node located i the middle of a linked list
  lst_four = LinkedList()
  lst_four.insert('apple')
  lst_four.append('banana')
  lst_four.insertBefore('banana', 'melon')
  assert lst_four.head.next.value == 'melon'
コード例 #12
0
def test_append_to_multiple_end():
  # add multiple nodes to the end of a linked list
    lst_four = LinkedList()
    lst_four.append('banana')
    lst_four.append('apple')
    assert lst_four.head.value == 'banana'
    assert lst_four.head.next.value == 'apple'
def test_insert_after_node_in_middle():
    object = LinkedList()
    object.append(6)
    object.append(7)
    object.append(9)
    object.insert_after(7, 8)
    assert str(object) == '6=>7=>8=>9=>NULL'
コード例 #14
0
def test_value_is_false():
    ll = LinkedList()
    ll.append(1)
    ll.append(2)
    ll.append(3)
    ll.append(4)
    assert ll.verify_index(7) == False
コード例 #15
0
class TestLinkedList:

    def setup(self):
        e1 = Element(1)
        e2 = Element(2)
        e3 = Element(3)

        self.ll = LinkedList(e1)
        self.ll.append(e2)
        self.ll.append(e3)

    def test_get_position(self):
        assert self.ll.head.next.next.value == 3
        assert self.ll.get_position(3).value == 3
        assert self.ll.get_position(1).value == 1
        assert self.ll.get_position(2).value == 2

    def test_insert_position(self):
        e4 = Element(4)
        e5 = Element(5)
        self.ll.insert(e4, 3)
        assert self.ll.get_position(3).value == 4
        self.ll.insert(e5, 3)
        assert self.ll.get_position(3).value == 5
        assert self.ll.get_position(4).value == 4

    def test_delete_position(self):
        self.ll.delete(1)
        assert self.ll.get_position(1).value == 2
        assert self.ll.get_position(2).value == 3
        assert self.ll.get_position(3).value == 3
コード例 #16
0
def test_append_1():
    ll = LinkedList()
    ll.append(1)
    assert ll.head.value is 1
    ll.append(2)
    assert ll.head.next.value is 2
    assert ll.head.next.next is None
コード例 #17
0
def test_print():
    ll = LinkedList()
    ll.insert(4)
    ll.append(-1)
    ll.append(3)

    assert str(ll) == '{4} ->{-1} ->{3} ->NULL'
コード例 #18
0
def test_linked_list_04():
    ll = LinkedList([1, 2, 3])

    elem = ll.pop()
    assert elem == 3
    assert ll.end.el == 2
    assert ll == LinkedList([1, 2])

    first = ll.pop(0)
    assert first == 1
    assert ll.head.el == 2
    assert len(ll) == 1
    assert ll == LinkedList([2])

    ll.append(3)
    ll.append(4)

    middle = ll.pop(1)
    assert middle == 3
    assert ll.head.next.el == 4
    assert ll == LinkedList([2, 4])

    ll.pop()
    ll.pop()

    assert ll == LinkedList()
    assert ll.head is None
    assert len(ll) == 0

    with pytest.raises(IndexError):
        ll.pop()
コード例 #19
0
def test_value_is_true():
    ll = LinkedList()
    ll.append(1)
    ll.append(2)
    ll.append(3)
    ll.append(4)
    assert ll.verify_index(2) == True
コード例 #20
0
def test3():
    ll = LinkedList()
    ll.append(5)
    ll.append(6)
    ll.append(8)
    ll.insertBefore(8, 10)
    assert ll.head.next.next.value == 10
コード例 #21
0
def test_insert_before():
    lst = LinkedList()
    lst.insert('baby goats')
    lst.insert('more goats')
    lst.append('water')
    lst.insert_before('baby goats', 'before')
    assert str(
        lst) == 'more goats --> before --> baby goats --> water --> None'
コード例 #22
0
def test_linked_list_insert_after():
    ll = LinkedList()
    ll.append(4)
    ll.append(3)
    ll.insertAfter(3, 7)
    actual = str(ll)
    expected = "{4} ->{3} ->{7} ->NULL"
    assert actual == expected
コード例 #23
0
def test_linked_list_insert_before():
    ll = LinkedList()
    ll.append(4)
    ll.append(3)
    ll.insertBefore(4, 2)
    actual = str(ll)
    expected = "{2} ->{4} ->{3} ->NULL"
    assert actual == expected
コード例 #24
0
def test_append_1_s():
    ll = LinkedList()
    ll.insert(4)
    ll.append(-1)
    ll.append('s')
    assert ll.head.value == 4
    assert ll.head.next.value == -1
    assert ll.head.next.next.value == 's'
コード例 #25
0
def test_multi_append():
    link = LinkedList()
    link.insert_node(4)
    link.append(1)
    link.append(2)
    link.append(3)
    actual = str(link)
    expected = f'{{ 4 }} -> {{ 1 }} -> {{ 2 }} -> {{ 3 }} -> NULL'
コード例 #26
0
def test_insert_after_end():
  # insert a node after the last node of the linked list
  lst_four = LinkedList()
  lst_four.insert('apple')
  lst_four.append('banana')
  lst_four.insertAfter('banana', 'strobary')
  assert lst_four.head.next.next.value == 'strobary'
  assert '{apple}-> {banana}-> {strobary}-> ' == lst_four.__str__()
コード例 #27
0
def test_append():
    node = Node(0)
    link = LinkedList(node)
    link.insert(4)
    link.append(10)
    actual = str(link)
    expected = f'{{ 4 }} -> {{ 0 }} -> {{ 10 }} -> NONE'
    assert actual == expected
コード例 #28
0
def test_same_length_k():
    make_list = LinkedList()
    make_list.append("a")
    make_list.append("b")
    make_list.append("c")
    actual = make_list.k_fromthe_end(2)
    expected = "a"
    assert actual == expected
コード例 #29
0
def test_append_for_three_values():
    new_list = LinkedList()
    new_list.append(Node(3))
    new_list.append(Node(1))
    new_list.append(Node(5))
    assert new_list.first_node.value == 3
    assert new_list.first_node.next_node.value == 1
    assert new_list.first_node.next_node.next_node.value == 5
コード例 #30
0
def test2():
    ll = LinkedList()
    ll.append(1)
    ll.append(2)
    ll.append(3)
    ll.insertBefore(2, 20)
    ll.insertBefore(20, 44)
    assert ll.head.next.value == 44
コード例 #31
0
class Stack(object):
	def __init__(self):
		self.stack = LinkedList()

	def push(self, element):
		self.stack.append(element)

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

	def peek(self):
		if self.stack.length():
			return self.stack.retrieve(self.stack.length() - 1)
		else:
			raise Exception("List is Empty")

	def isEmpty(self):
		return not self.stack.length()

	def size(self):
		return self.stack.length()