コード例 #1
0
class LinkedListQueue:
    def __init__(self, capacity=float("inf")):
        self._queue = SinglyLinkedList()
        self._capacity = capacity

    def enqueue(self, data):
        if self.get_size() < self._capacity:
            self._queue.add_item_end(data)
        else:
            raise OverflowError()

    def dequeue(self):
        if self.is_empty():
            return None
        else:
            return self._queue.get_first_item()

    def peek(self):
        return self._queue.peek_first_item()

    def get_size(self):
        return self._queue.get_size()

    def get_capacity(self):
        return self._capacity

    def is_empty(self):
        return self._queue.is_empty()
コード例 #2
0
class Queue:
    """
        Queue will be implemented with a linked list as the start
        of the queue as the tail, and the end of the queue as the head.
        This way, enqueue's and dequeue's are O(1).

        Enqueue is appending from the tail, dequeueing is reassigning
        the pointer of the head to None.
    """
    def __init__(self, iterable=None):
        self.queue = SinglyLinkedList()

        if iterable:
            for item in iterable:
                self.queue.append(item)

    def is_empty(self):
        return self.queue.is_empty()

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

    def front(self):
        if self.queue.is_empty():
            return None
        return self.queue.head.data

    def enqueue(self, data):
        self.queue.append(data)

    def dequeue(self):
        if self.queue.size < 1:
            raise ValueError("queue is empty")

        output = self.front()

        self.queue.delete(output)

        return output
コード例 #3
0
class Stack(object):

    def __init__(self):
        self.S = SinglyLinkedList()

    def is_empty(self):
        return self.S.is_empty()

    def push(self, element):
        self.S.insert(SinglyLinkedListNode(element)) # Linked list implementation of stack overflow do not exist

    def pop(self):
        head_node = self.S.delete_head()
        if head_node:
            return head_node.key
        else:
            raise IndexError('stack underflow')
コード例 #4
0
class Stack:
    def __init__(self, desc=None):
        self.__linked_list = SinglyLinkedList()
        self.__desc = desc
        # self = SinglyLinkedList() does not work. Why???

    def push(self, key):
        self.__linked_list.push_front(key)

    def pop(self):
        return self.__linked_list.pop_front()

    def top(self):
        return self.__linked_list.top_front()

    def is_empty(self):
        return self.__linked_list.is_empty()

    def get_desc(self):
        return self.__desc

    # cannot use __str__ for the linked_list, b/c it's hidden
    def __str__(self):
        return str(self.__linked_list) # str(o) calls __str__ method of object o
コード例 #5
0
 def test_is_empty(self):
     llist = SinglyLinkedList()
     self.assertTrue(llist.is_empty())
     llist.insert_first(0)
     self.assertFalse(llist.is_empty())
コード例 #6
0
 def test_empty(self):
     linkedlist = SinglyLinkedList()
     assert linkedlist.num_nodes == 0
     assert list(linkedlist) == []
     assert str(linkedlist) == "SinglyLinkedList([])"
     assert linkedlist.is_empty()