Доработал

mail@pastecode.io avatar
unknown
python
a year ago
1.9 kB
4
Indexable
Never
class Queue:
    def __init__(self, capacity):
        self.capacity = capacity
        self.queue = [None] * capacity
        self.head = 0
        self.tail = 0
        self.size = 0

    def push(self, x):
        if self.size >= self.capacity:
            raise ValueError("Queue is full")
        if self.tail == self.capacity:
            self.tail = 0
        self.queue[self.tail] = x
        self.tail = self.tail + 1
        self.size += 1

    def pop(self):
        if self.size == 0:
            raise ValueError("Queue is empty")
        if self.head == self.capacity:
            self.head = 0
        self.head += 1
        self.size -= 1
        x = self.queue[self.head - 1]
        self.queue[self.head - 1] = None
        return x

    def clear(self):
        self.queue = [None] * self.capacity
        self.head = 0
        self.tail = 0
        self.size = 0

    def empty(self):
        return self.size == 0

    def Qsize(self):
        return self.size

    def front(self):
        if self.size == 0:
            raise ValueError("Queue is empty")
        return self.queue[self.head]

    def back(self):
        if self.size == 0:
            raise ValueError("Queue is empty")
        return self.queue[self.tail - 1]


# all_years = {}
# for i in range(1,7):
#    all_years[i] = Queue(100)
# with open('input.txt', 'r', encoding='utf-8') as inp:
#    for line in inp:
#        name, year = line.split()
#        all_years[int(year)].push(name)
#
# with open('output.txt', 'w', encoding='utf-8') as out:
#    for i in range(1, 7):
#        while not all_years[i].empty():
#            student = all_years[i].pop()
#            out.write(f'{student} {i}\n')
queue = Queue(100)

for j in range(15):
    for i in range(10):
        queue.push(i)
    for _ in range(5):
        queue.pop()
    print('Размер очереди:', queue.Qsize())