0
0
DSA Pythonprogramming~10 mins

Stack Implementation Using Linked List in DSA Python - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize the top of the stack as empty.

DSA Python
class Stack:
    def __init__(self):
        self.top = [1]
Drag options to blanks, or click blank then click option'
A[]
BNode()
C0
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or an empty list instead of None for top.
2fill in blank
medium

Complete the code to create a new node with the given data.

DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = [1]
Drag options to blanks, or click blank then click option'
Aself
Bdata
CNone
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Setting next to data or 0 instead of None.
3fill in blank
hard

Fix the error in the push method to correctly add a new node on top of the stack.

DSA Python
def push(self, data):
    new_node = Node(data)
    new_node.next = [1]
    self.top = new_node
Drag options to blanks, or click blank then click option'
Aself.top.next
Bself.top
CNone
Dnew_node
Attempts:
3 left
💡 Hint
Common Mistakes
Linking new_node.next to None or new_node itself.
4fill in blank
hard

Fill both blanks to correctly remove the top node and update the stack.

DSA Python
def pop(self):
    if self.top is None:
        return None
    popped_node = self.top
    self.top = [1]
    popped_node.next = [2]
    return popped_node.data
Drag options to blanks, or click blank then click option'
Aself.top.next
BNone
Cself.top
Dpopped_node
Attempts:
3 left
💡 Hint
Common Mistakes
Setting top to popped_node or None incorrectly.
5fill in blank
hard

Fill all three blanks to check if the stack is empty and return the top element's data.

DSA Python
def peek(self):
    if self.top == [1]:
        return [2]
    return self.top.[3]
Drag options to blanks, or click blank then click option'
ANone
Cdata
Dnext
Attempts:
3 left
💡 Hint
Common Mistakes
Returning top or next instead of data, or wrong empty check.