Home How to Print elements in a queue(Python)
Reply: 0

How to Print elements in a queue(Python)

user9761
1#
user9761 Published in September 21, 2018, 8:24 am

I have the following code in python. Inside the LinkedQueue class I have created a display method which prints all the elements in the queue.

class LinkedQueue :
    class _Node :

        def __init__(self, element, next):
            self._element = element
            self._next = next

        def get_elements(self):
            return self._element

        def set_elements(self, num):
            self._element = num        

    def __init__(self) :
        self._head = None
        self._tail = None
        self._size = 0

    def display(self):
        tmp = self._head
        while tmp != None :
            print(tmp.get_elements())
            tmp = tmp._next        

    def __len__(self) :
        return self._size

    def is_empty(self) :
        return self._size == 0

    def first(self) :
        if self.is_empty() :
            raise Empty('Queue is empty')
        return self._head._element

    def dequeue(self) :
        if self.is_empty():
            raise Empty('Queue is empty')
        answer = self._head._element
        self._head = self._head._next
        self._size -= 1
        if self.is_empty() :
            self._tail = None
        return answer

    def enqueue(self, e) :
        newest = self._Node(e,None)
        if self.is_empty() :
            self._head = newest
        else :
            self._tail._next = newest
        self._tail = newest
        self._size += 1

class Empty(Exception) :
    pass    

I have also created a menu function outside the class which allows the user to choose from two options: add numbers to the queue or to quit the menu. Pressing 'n' allows the user to add numbers to the queue. Pressing 'q' allows the user to quit the menu. After the user has pressed quit, the numbers that they have entered will be printed in the order that they were entered. I am having trouble with printing the numbers and am unsure of how to fix it.

def menu():
    queue = LinkedQueue()
    while True:
        print('n - to add a number')
        print('q - to quit')
        selection = input('Enter your selection: ')
        if selection == 'n':
            number = int(input("Enter a number: "))
            queue.enqueue(number)
        elif selection == 'q':
            break
    for item in queue.display():
        n = item

    counter = 0
    for i in range(len(queue)):
        counter += 1
        print('Entry # ', counter, '=', n)     
share|improve this question
  • You should give a more detailed description of your 'trouble with printing the numbers'. What is your expected result? What did you see instead? – Anthony Kong Feb 14 at 4:08
  • Since display does not return anything, how can you iterate through it? Wouldn't calling queue.display() suffice ? – Sruthi V Feb 14 at 4:09
  • 1
    Your for i in range(len(queue)): doesn't update n so you'll only ever see the same value for each element. – Ken Y-N Feb 14 at 4:10
  • One remark, you could consider making is_empty a property rather than a callable method. – Brad Solomon Feb 14 at 4:19

1 Answer 1

active oldest votes
up vote 0 down vote
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.420931 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO