Datawhale 9th group learning -- data structure and algorithm Task04: queue (2 days)

Theoretical part
  • Implement a sequence queue with an array.
  • Implement a circular queue with an array.
  • A chained queue is implemented with a chained list.

Queue: is a first in, first out (FIFO) linear table, which only allows the insertion of elements at one end of the table, and the deletion of elements at the other end of the table. In a queue, one end of an element that is allowed to be inserted is called a Rear, and the other end of an element that is allowed to be deleted is called a Front.
Sequential queue: the sequential storage structure of a queue refers to the elements of a queue stored in a group of storage units with continuous addresses.
Circular queue: the sequence queue is assumed to be a circular structure, which is called circular queue.
Chain queue: the queue represented by chain list is called chain queue for short.

Exercise part
  1. Simulate banking service completion program code.

At present, the queuing system is widely used in the window industry represented by the bank business hall. The system completely simulates the whole process of the crowd queuing, and replaces the hard work of people standing in the queue through the functions of getting tickets into the queue, queuing and waiting, and calling service.

The specific operation process of queuing software is as follows:

Customer service No
When the customer arrives at the service hall, go to the number taking machine next to the entrance, and press the corresponding service button on it, the number taking machine will automatically print out a service sheet. The service number and the number of people waiting for service in front of the service number are shown on the sheet.

Service staff calls customers
The service staff only need to press the corresponding button of the pager on the counter, then the customer's service number will be displayed on the display screen in order, and send out "Ding Dong" and relevant voice information to prompt the customer to go to the window for work. After a customer completes the work, the counter service staff only need to press the corresponding key of the pager to automatically call the next customer.

Write a program to simulate the above work process, the main requirements are as follows:

After the program is running, when you see the prompt of "please click the touch screen to get the number", just press enter to display the prompt of "your number is XXX, you have YYY digit in front of you", where XXX is the service number obtained and YYY is the number of people who are waiting for service before XXX.
Multi thread technology is used to simulate service windows (multiple windows can be simulated), which has the behavior of server calling customers. Assuming that the service time of each customer is 10000ms, when the time is up, it will display "please XXX to ZZZ window!" Hint. Where ZZZ is the window number to be served for customers.

import time
import threading


# Implementation stack
class Queue:
    def __init__(self):
        self.items = []

    def isEmpty(self):
        return self.items == []

    def enqueue(self, item):
        self.items.insert(0,item)

    def dequeue(self):
        if self.items != []:
            return self.items.pop()
        else:
            return False

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

    def top(self):
        if self.items != []:
            return self.items[len(self.items)-1]
        else:
            return False


# Realize the function of calling at the bank counter
class Counter(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.waitQueue = Queue()  # Initialize waiting team
        self.lock = threading.Lock()

    def callIng(self):
        while True:
            # Keep calling at the counter, keep cycling
            time.sleep(10)
            if not self.waitQueue.isEmpty():
                self.lock.acquire()
                print("please{}Number customer,reach{}No. window for business".format(self.waitQueue.top(), threading.current_thread().name))
                self.waitQueue.dequeue()
                self.lock.release()


class bankSystem:
    def __init__(self):
        self.serviceQueue = Queue()
        self.nowNum = 0
        # self.windows = k  # Number of bank counters
        self.maxSize = 100

    def getNumber(self):
        if self.nowNum < self.maxSize:
            self.nowNum += 1
            return self.nowNum
        else:
            print("Business is busy now, please come back later")


if __name__ == "__main__":
    bank_sys = bankSystem()
    windowcount = 3
    serviceWindow = [None] * windowcount
    threadList = [None] * windowcount

    for i in range(windowcount):
        serviceWindow[i] = Counter()
        serviceWindow[i].waitQueue = bank_sys.serviceQueue
        threadList[i] = threading.Thread(name=(i + 1), target=serviceWindow[i].callIng, args=())
        threadList[i].start()
        # threadList[i].join()

    while True:
        input("Please click the touch screen to get the number:")
        callNumber = bank_sys.getNumber()
        if bank_sys.serviceQueue != None:
            print("Your current number is{},Before you{}personal".format(str(callNumber), str(bank_sys.serviceQueue.size())))
            bank_sys.serviceQueue.enqueue(bank_sys.nowNum)
        else:
            print('Your number is:{},0 in front of you'.format(callNumber))

Published 6 original articles, won praise 0, visited 1297
Private letter follow

Tags: Windows

Posted on Mon, 13 Jan 2020 08:04:46 -0500 by nrsh_ram