FAQ: Queues: Python - Queues Python Enqueue

This community-built FAQ covers the “Queues Python Enqueue” exercise from the lesson “Queues: Python”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

Linear Data Structures

FAQs on the exercise Queues Python Enqueue

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hello people!

Can someone help me understand the Enqueue method? I think i understand well until de Else statement under the first If. Here:

  else:
    self.tail.set_next_node(item_to_add)
    self.tail = item_to_add
  self.size += 1

I just can’t understand why set the next node of the current tail to the item_to_add. When i think of a queue i think in setting the next node of item_to_add to the current tail, not the otherwise.

Thanks!

You’re basically saying. If the Queue is NOT empty. Add John(item_to_add) to the back of the queue and tell Mary(tail), that was previously at the back, that John is now behind her. After that you tell your program, that Adam is now the “tail” of the queue.

Remember that “self.tail.set_next_node(item_to_add)” is telling the current tail, that item_to_add is coming after it.

6 Likes

Hello,

as far as I can see set_next_node() is a method of Node class. In the exercise we use this method on a Queue class object q. To put it exactly: We do self.tail.set_next_node(item_to_add) where self is a Queue object.
isinstance(self, Node) yields False, i.e self is not an instance of Node. So my question is: Why can we use a method of a different class (Node) on a class (Queue) which is not inherited from the first class ? Why doesn’t self.tail.set_next_node(item_to_add) throw an error ?

Any help is highly appreciated.
Thank you.

Why do I get an AttributeError at line? 43?
from node import Node

class Queue:
def init(self, max_size=None):
self.head = None
self.tail = None
self.max_size = max_size
self.size = 0

Add your enqueue method below:

def enqueue(self, value):
if self.has_space():
item_to_add = Node(value)
print(“Adding " + str(item_to_add.get_value()) + " to the queue!”)
if self.is_empty():
self.head = item_to_add
self.tail = item_to_add
else:
self.tail.set_next_node(item_to_add)
self.tail = item_to_add
self.size += 1
else:
print(“Sorry, no more room!”)

def peek(self):
if self.is_empty():
print(“Nothing to see here!”)
else:
return self.head.get_value()

def get_size(self):
return self.size

def has_space(self):
if self.max_size == None:
return True
else:
return self.max_size > self.get_size()

def is_empty(self):
return self.size == 0
q = Queue(“all the fluffy kitties”)
q.enque(“all the fluffy kitties”)

During step 2 I am getting “Did you add self and value as paramenters for enqueque()?” (the misspelling is there). The weird thing is that I defined enqueque() with parameters self and value in step 1 and I passed that step.

When I replace my code (below) with the solution code, it lets me pass.

from node import Node

class Queue:
def init(self, max_size=None):
self.head = None
self.tail = None
self.max_size = max_size
self.size = 0

Add your enqueue method below:

def enqueue(self, value):
if self.has_space():
item_to_add = Node(value)
print("Adding "+ str(item_to_add.get_value() + ’ to the queue!'))
if self.is_empty():
self.tail = item_to_add
self.head = item_to_add
else:
self.tail.set_next_node(item_to_add)
self.tail = item_to_add
self.size +=1
else:
print(“Sorry, no more room!”)

def peek(self):
if self.is_empty():
print(“Nothing to see here!”)
else:
return self.head.get_value()

def get_size(self):
return self.size

def has_space(self):
if self.max_size == None:
return True
else:
return self.max_size > self.get_size()

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

q = Queue()
q.enqueue(“all the fluffy kitties”)
print(q.peek())

Ah, I see what I did. I’m missing a parenthesis 3 lines down.