Login
Order Now
Support
theprogrammingassignmenthelp

Implement Queue & Stack ADT using DLinkedListADT

  • 5th Sep, 2019
  • 21:48 PM

Implement Queue ADT using DLinkedListADT


Method list:
- QueueADT: constructor
- isEmpty: check empty queue
- isFull: check full queue
- enQueue: enqueue
- deQueue: dequeue
- size: get size of queue
"""

from DLinkedListADT import DLinkedListADT


class QueueADT():
    def __init__(self, min_):
        self.items = DLinkedListADT()
        if min_ <= 0:
            raise Exception('Min value should be greater than 0')
        self.capacity = min_ * 2

    def isEmpty(self):
        if self.items.size() == 0:
            return True
        return False

    def isFull(self):
        if self.items.size() == self.capacity:
            return True
        return False

    def enQueue(self, item):
        if self.isFull():
            raise Exception('Queue is full. Can not enqueue more')
        self.items.addLast(item)

    def deQueue(self):
        if self.isEmpty():
            raise Exception('Queue is empty. Can not dequeue')
        return self.items.deleteFirst()

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

 

Implement Stack ADT using DLinkedListADT

Method list:
- StackADT: constructor
- isEmpty: check empty stack
- isFull: check full stack
- push: add new item to stack
- pop: remove item from stack
- size: get size of stack
"""

from DLinkedListADT import DLinkedListADT


class StackADT():
    def __init__(self, min_):
        self.items = DLinkedListADT()
        if min_ <= 0:
            raise Exception('Min value should be greater than 0')
        self.capacity = min_ * 2

    def isEmpty(self):
        if self.items.size() == 0:
            return True
        return False

    def isFull(self):
        if self.items.size() == self.capacity:
            return True
        return False

    def push(self, item):
        if self.isFull():
            raise Exception('Stack is full. Can not add more')
        self.items.addLast(item)

    def pop(self):
        if self.isEmpty():
            raise Exception('Stack is empty. Can not get POP')
        return self.items.deleteLast()

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

 

Share this post