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

How to Print elements in a queue(Python)

user9042207
1#
user9042207 Published in 2018-02-14 03:58:35Z

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)     
JDSchenck
2#
JDSchenck Reply to 2018-02-14 04:10:37Z

Try this:

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
        counter = 1
        while tmp != None :
            #print(tmp.get_elements())
            print('Entry # ', counter, '=', tmp.get_elements()) 
            tmp = tmp._next
            counter+=1

    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    



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':
            queue.display()
            break
    '''
        for item in queue.display():
        n = item

    counter = 0
    for i in range(len(q)):
        counter += 1
        print('Entry # ', counter, '=', n) 

    ''' 
Sruthi V
3#
Sruthi V Reply to 2018-02-14 04:12:40Z

You can make the below changes to display :

def display(self):
    my_q=[]
    tmp = self._head
    print("In display")
    while tmp != None :
        print(tmp.get_elements())
        my_q.append(tmp.get_elements())
        tmp = tmp._next    
    return my_q 

This way display will actually return something you can iterate over

This to display with a counter :

    counter =1
    for item in queue.display():
        n = item
        print('Entry # ', counter, '=', n) 
        counter+=1

OUTPUT

n - to add a number
q - to quit
Enter your selection: n
Enter a number: 234
n - to add a number
q - to quit
Enter your selection: n
Enter a number: 123
n - to add a number
q - to quit
Enter your selection: n
Enter a number: 789
n - to add a number
q - to quit
Enter your selection: q
In display
234
123
789
Entry #  1 = 234
Entry #  2 = 123
Entry #  3 = 789
You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO