Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a node with given data.
DSA Python
class Node: def __init__(self, data): self.data = [1] self.next = None
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'next' instead of 'data' for the node's data.
Assigning 'self' or 'Node' instead of the parameter.
✗ Incorrect
The node stores the given data in self.data. So, we assign the parameter data to self.data.
2fill in blank
mediumComplete the enqueue method to add a new node at the end of the queue.
DSA Python
def enqueue(self, data): new_node = Node(data) if self.rear is None: self.front = self.rear = new_node else: self.rear.next = [1] self.rear = new_node
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning rear.next to self.front or self.rear instead of new_node.
Assigning rear.next to data instead of a node.
✗ Incorrect
To add a new node at the end, set the current rear's next pointer to the new node.
3fill in blank
hardFix the error in the dequeue method to correctly remove the front node.
DSA Python
def dequeue(self): if self.front is None: return None temp = self.front self.front = self.front.[1] if self.front is None: self.rear = None return temp.data
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' which does not exist in singly linked list nodes.
Using 'data' or 'front' which are not pointers to the next node.
✗ Incorrect
To remove the front node, update self.front to point to the next node after the current front.
4fill in blank
hardFill both blanks to check if the queue is empty.
DSA Python
def is_empty(self): return self.[1] is [2]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking rear instead of front.
Comparing to True instead of None.
✗ Incorrect
The queue is empty if the front pointer is None.
5fill in blank
hardFill all three blanks to implement a method that prints all elements in the queue.
DSA Python
def display(self): current = self.[1] while current is not [2]: print(current.[3], end=' -> ') current = current.next print('None')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Starting from rear instead of front.
Checking for True instead of None in the loop condition.
Printing 'next' instead of 'data'.
✗ Incorrect
Start from the front node, loop until current is None, and print each node's data.