Data structure - linear structure: Queue ["Queue" corresponds to a subset of array operations;]

Posted by djrichwz on Fri, 18 Feb 2022 10:36:31 +0100

2, Queue

The operation can only be performed at one end of the list (the operation can be performed at the other end of the list) through the specified order of the list (the operation can be performed at the other end of the list).

Queue: a First In First Out linear table, called FIFO for short. The end allowed to be inserted is the end of the queue, and the end allowed to be deleted is the head of the queue. Operation in the middle of the queue is not allowed! Suppose the queue is q = (a1, a2,..., an), then a1 is the head element and an is the tail element. In this way, we can always start from a1 when deleting, and always end in the queue when inserting. This is also more in line with our usual habits in life. Those who come first are given priority, and those who come last are of course at the end of the line.

1. Operation of "queue"

methodeffect
Queue()Create an empty queue
enqueue(item)Add an item element to the queue
dequeue()Delete an element from the queue header
is_empty()Judge whether a queue is empty
size()Returns the size of the queue

2. Code implementation of "queue" structure

The queue can be realized by sequential table (dynamic array) (the list data structure in python is a kind of sequential table (dynamic array)) or linked list.

class Queue(object):
    """queue"""

    def __init__(self):
        self.__items = [] # Set as private property

    def is_empty(self):
        return self.__items == []
    # The time complexity of the tail operation of the list is O(1), and the time complexity of the head operation is O(n)
	# In the actual business, if the operation of saving data is more than that of fetching data, a new element is added at the end of the list and the element is fetched from the head of the list
	# In the actual business, if the number of operations of saving data is less than that of fetching data, a new element is added to the head of the list and the element is fetched from the tail of the list
    def enqueue(self, item):
        """Add a to the queue item element"""
        self.__items.append(item) # Add a new element to the tail of the items list (the time complexity is O(1))
        # self.__items .insert(0, item)  # Add a new element to the tail of the items list (the time complexity is O(n))

    def dequeue(self):
        """Deletes an element from the head of the queue"""
        return self.__items .pop(0)	# Take out an element from the header of the items list (the time complexity is O(n))

    def size(self):
        """Return size"""
        return len(self.__items )


if __name__ == "__main__":
    q = Queue()
    q.enqueue("1")
    q.enqueue("2")
    q.enqueue("3")
    print('Queue size=', q.size())
    print('Pop an element from the queue=', q.dequeue())
    print('Pop an element from the queue=', q.dequeue())
    print('Pop an element from the queue=', q.dequeue())

3. Double ended queue

deque (full name: double ended queue) is a data structure with the properties of queue and stack.

Elements in the double ended queue can be popped from both ends, and their restricted insertion and deletion operations are carried out at both ends of the table.

Dual ended queues can be queued in and out at either end of the queue.

4. Operation of "queue"

methodeffect
Deque()Create an empty double ended queue
add_front(item)Add an item element from the head of the team
add_rear(item)Add an item element from the end of the team
remove_front()Delete an item element from the queue header
remove_rear()Delete an item element from the end of the queue
is_empty()Judge whether the double ended queue is empty
size()Returns the size of the queue

5. Code implementation of "double ended queue" structure

class Deque(object):
    """Double ended queue"""

    def __init__(self):
        self.__items = []

    def is_empty(self):
        """Judge whether the queue is empty"""
        return self.__items == []

    def add_front(self, item):
        """Add an element at the head of the queue"""
        self.__items.insert(0, item)

    def add_rear(self, item):
        """Add elements at the end of the queue"""
        self.__items.append(item)

    def remove_front(self):
        """Delete element from queue head"""
        return self.__items.pop(0)

    def remove_rear(self):
        """Delete element from end of queue"""
        return self.__items.pop()

    def size(self):
        """Return queue size"""
        return len(self.__items)

if __name__ == "__main__":
    deque = Deque()
    deque.add_front(1)
    deque.add_front(2)
    deque.add_rear(3)
    deque.add_rear(4)
    print(deque.size())
    print(deque.remove_front())
    print(deque.remove_front())
    print(deque.remove_rear())
    print(deque.remove_rear())

Topics: data structure queue