Technical column: github.com/yongxinz/te…

Meanwhile, welcome to follow my wechat official account AlwaysBeta, more exciting content waiting for you.

The stack

A stack, also known as a stack, is a linear table with limited operations. The limitation is that only one end of the table is allowed for insert and delete operations. This end is called the top of the stack, and the other end is called the bottom of the stack. Adding a new element to a stack is also called pushing, pushing, or pushing. It is done by placing the new element on top of the top element to make it the new top element. To remove an element from a stack is also called to stack or unstack. It is to remove the top element from the stack so that the adjacent element becomes the new top element.

Stacks can be implemented in either a sequential list or a linked list, but the sequential list is used here for convenience.

# -*- coding: utf-8 -*-


class Stack(object):
    """ Stack implementation class """

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

    # push(item) Adds a new element item to the top of the stack
    def push(self, item):
        self.__items.append(item)

    # pop() pops the top element of the stack
    def pop(self):
        return self.__items.pop()

    # peek() returns the top element of the stack
    def peek(self):
        return self.__items[self.size() - 1]

    # is_empty() checks if the stack is empty
    def is_empty(self):
        return self.__items == []

    # size() returns the number of elements on the stack
    def size(self):
        return len(self.__items)


if __name__ == '__main__':
    stack = Stack()
    stack.push(2)
    stack.push(3)
    stack.push(4)
    stack.push(5)
    tmp = stack.pop()
    print(tmp)
    print(stack.peek())
    print(stack.size())
    print(stack.is_empty())
Copy the code

The queue

A queue is a special kind of linear table, special in that it only allows deletion at the front of the table and insertion at the rear. Like a stack, a queue is a linear table with limited operation. The end that inserts is called the end of the queue, and the end that deletes is called the head of the queue. A queue with no elements is called an empty queue.

The data elements of a queue are also called queue elements. Inserting a queue element into a queue is called enqueueing, and removing a queue element from a queue is called enqueueing. Because the queue can only be inserted at one end and deleted at the other end, only the earliest element can be deleted from the queue first, so the queue is also called FIFO – first in first out (FIFO – first in first out) linear table

# -*- coding: utf-8 -*-


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

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

    # push(item) Adds an item element to the queue
    def push(self, item):
        self.__items.insert(0, item)

    # pop() removes an element from the queue head
    def pop(self):
        return self.__items.pop()

    # is_empty() determines if a queue is empty
    def is_empty(self):
        return self.__items == []

    # size() returns the queue size
    def size(self):
        return len(self.__items)


if __name__ == '__main__':
    queue = Queue()
    queue.push(1)
    queue.push(2)
    queue.push(3)
    queue.push(4)
    print(queue.pop())
    print(queue.pop())
    print(queue.pop())
    print(queue.size())
    print(queue.is_empty())
Copy the code

deque

A deque (full name double-ended queue) is a data structure that has the properties of queue and stack.

Elements in a two-ended queue can pop up from both ends, with restricted inserts and deletions occurring at both ends of the table. A double-enqueued queue can enter or leave a queue at either end.

# -*- coding: utf-8 -*-


class Deque(object):
    """ "Double-endian queue ""

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

    # add_front(item) Adds an item element from the header of the queue
    def add_front(self, item):
        self.__items.insert(0, item)

    # add_rear(item) Adds an item element from the end of the queue
    def add_rear(self, item):
        self.__items.append(item)

    # remove_front() removes an item element from the header
    def remove_front(self):
        return self.__items.pop(0)

    # remove_rear() removes an item element from the end of the queue
    def remove_rear(self):
        return self.__items.pop()

    # is_empty() checks whether the double-ended queue is empty
    def is_empty(self):
        return self.__items == []

    # size() returns the queue size
    def size(self):
        return len(self.__items)

    def print_items(self):
        print(self.__items)


if __name__ == '__main__':
    deque = Deque()
    deque.add_front(1)
    deque.add_front(3)
    deque.add_front(5)
    deque.print_items()
    deque.add_rear(9)
    deque.add_rear(8)
    deque.add_rear(7)
    deque.print_items()
    print(deque.is_empty())
    print(deque.remove_front())
    print(deque.remove_rear())
    deque.print_items()
Copy the code

Those who need the source code can download it: Code portal

Baagee. VIP /index/artic…