class Stack(object):
    def __init__(self):
        self.items = LinkedList()

    def __str__(self):
        s = ', '.join(map(str, self.items))
        return 'Stack(>[' + s + '])'

    def __len__(self):
        return len(self.items)

    def isEmpty(self):
        return len(self) == 0

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

    def pop(self):
        try:
            return self.items.popleft()
        except KeyError:
            return None

    def peek(self):
        return self.items[0]

    def clear(self):
        while not self.isEmpty():
            self.pop()
Esempio n. 2
0
class Queue(object):
    def __init__(self):
        self.items = LinkedList()

    def __str__(self):
        s = ', '.join(map(str, self.items))
        return f'Queue([{s}]<)'

    def __len__(self):
        return len(self.items)

    def enqueue(self, item):
        """Items enqueue in the rear(tail) of the list"""
        self.items.append(item)

    def dequeue(self):
        """Items dequeue from the front(head) of the list"""
        return self.items.popleft()

    def isEmpty(self):
        return len(self) == 0