Untitled

Доработал класс стека так, чтобы он работал как нужно. Теперь он корректно проходит проверку вашим циклом.
mail@pastecode.io avatar
unknown
python
a year ago
1.9 kB
3
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")
        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")
        x = self.queue[self.head]
        for _ in range(self.head+1,self.size):
            self.queue[_-1]=self.queue[_]
        self.tail-=1
        self.queue[self.tail]= None
        self.size -= 1
        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):
        print(queue.pop())
    print('Размер очереди:', queue.Qsize())