Algorithm that creates a Python queue

Asked

Viewed 260 times

-1

Hello, I would like to create an algorithm but I don’t know much how to start it...

It would be to write a program to create a queue, represented by a list, and present to the user the options to insert, remove, get the front of the queue and view all the contents of the queue, as well as an option to terminate the program.

And it would also just be integer numbers as elements to be stored in the queue.

Someone could save me?

2 answers

1

The common way to create data structures in Python and other more modern languages is by using classes, you can create one class for the queue and another for the nos and within them implement methods to manage the queue. Take a look at this article: https://algoritmosempython.com.br/cursos/algoritmos-python/estruturas-dados/filas/

But you can also use the common lists in python and with it implement a queue managing it with the methods of the list class (pop, appende, Insert, etc).

But if you just want to create a program that uses the queue data structure, without you wanting to implement it, you can import from Collections, deck, that already implements a ED queue more effectively. See more about deck here: https://docs.python.org/pt-br/3/library/collections.html#Collections.deque

from collections import deque

1


This question has become a little difficult to understand, but I’ll try to put it in context. I hope it helps. Regarding the queue, I will use a literal, with people. I will take as something basic for being a question also of algorithm. More information here on devmedia.


Let’s assume there are already two people

pessoas = ['Keanan', 'Eduard']


Then one more has just arrived. It has come last, so go to the end

pessoas.append('Ayla')

[Keanan, 'Eduard', 'Ayla'']



Continuing... the queue finally walked, then the first one was answered. Soon he leaves the queue

pessoas.pop(0)

Staying: ['Eduard', 'Ayla']
Here things get really interesting. We have several ways to get people out of line. We can call her by name, as in people.remove('Ayla') or by number, as in people.pop(1). It is also possible to use del people1, no parentheses in that case.



At the end of everything, the most important thing is to remember that the counting of the lists starts at zero. Soon at ['Maisie', 'Smith', 'Hannah'] or I am the 1.
For the first element we use [0], and the last [-1].



Colinha:

idades = [10, 33, 5]
idades.append(3) # adiciona ao final
idades.pop(2) # remove o 5... o número dois é o indice
idades.remove(10) # remove o 10... o número dez é o valor
idades[0] # mostra o primeiro
idades[-1] # mostra o último

print(idades) # mostra todos

Here’s an example of code that can solve the problem, I didn’t put everything to let you think a little. On the Insert, it puts the person in a certain position in the queue, with the 0 being the beginning.

sair = False
fila = []

while not sair:
    entrada = int(input('''
Selecione uma opção:
  - [1] Adicionar
  - [2] Adicionar no Inicio
  - [3] Remover
  - [4] Ver Fila
  - [5] Sair \n 
'''))
    
    if entrada == 1:
    valor = input('O que deseja adicionar? ')
    fila.append(valor)
        
    if entrada == 2:
    valor = input('O que deseja adicionar ao inicio? ')
    fila.insert(0, valor)
        
    if entrada == 5:
    sair = True
  • Hi, then, the idea would be to create a menu, and in it would have the options to insert, remove, add in front of the queue, view all the content of the queue and repeat this "menu" until selecting the option to finish the program. Could you please help me how to do it this way?

  • I’ll edit the post

Browser other questions tagged

You are not signed in. Login or sign up in order to post.