📖 Scenario: You are building a simple stack using a linked list in C. A stack is like a pile of plates where you add new plates on top. Here, each plate is a node in the linked list.
🎯 Goal: Build a linked list stack and add a new node to the top using the push operation.
📋 What You'll Learn
Create a struct called Node with an int data and a Node* next pointer.
Create a pointer called top to represent the top of the stack and initialize it to NULL.
Create a new node with data = 10 and push it onto the stack by updating top.
Print the data of the top node after the push operation.
💡 Why This Matters
🌍 Real World
Stacks are used in many programs like undo features, expression evaluation, and backtracking algorithms.
💼 Career
Understanding linked list operations like push is essential for software developers working with low-level data structures and memory management.
Progress0 / 4 steps
1
Create the Node struct and initialize the top pointer
Write the struct definition for Node with an int data and a Node* next. Then create a pointer called top and set it to NULL.
DSA C
Hint
Use struct Node to define the node. Initialize top as a pointer to Node and set it to NULL.
2
Create a new node with data 10
Create a new Node pointer called newNode. Allocate memory for it using malloc. Set its data to 10 and its next to NULL.
DSA C
Hint
Use malloc(sizeof(struct Node)) to allocate memory. Use arrow -> to set data and next.
3
Push the new node onto the stack
Set the next of newNode to the current top. Then update top to point to newNode.
DSA C
Hint
To push, link newNode->next to top and then update top to newNode.
4
Print the data of the top node
Use printf to print the data of the top node in this format: Top data: 10\n.
DSA C
Hint
Use printf("Top data: %d\n", top->data); to print the data of the top node.
Practice
(1/5)
1. What does the push operation do in a linked list when using nodes?
easy
A. Searches for a node with a specific value
B. Adds a new node at the beginning of the list
C. Removes the last node from the list
D. Deletes the entire linked list
Solution
Step 1: Understand the push operation
The push operation adds a new element to the linked list.
Step 2: Identify where the new node is added
In a linked list, push adds the new node at the start (head) of the list.
Final Answer:
Adds a new node at the beginning of the list -> Option B
Quick Check:
Push = Add at head [OK]
Hint: Push always adds at the start of the linked list [OK]
Common Mistakes:
Thinking push adds at the end
Confusing push with pop
Assuming push deletes nodes
2. Which of the following is the correct syntax to create a new node and push it onto the linked list in C?
easy
A. newNode = malloc(sizeof(Node)); newNode->prev = head; head = newNode;
B. newNode = malloc(Node); newNode->next = head; head = newNode;
C. newNode = malloc(sizeof(Node)); newNode->next = head; head = newNode;
D. newNode = malloc(sizeof(Node)); head = newNode->next; newNode = head;
Solution
Step 1: Allocate memory correctly for new node
Use malloc(sizeof(Node)) to allocate memory for the new node.
Step 2: Link new node to current head and update head
Set newNode->next to current head, then update head to newNode.
Final Answer:
newNode = malloc(sizeof(Node)); newNode->next = head; head = newNode; -> Option C
Quick Check:
Correct malloc and linking = newNode = malloc(sizeof(Node)); newNode->next = head; head = newNode; [OK]
Hint: Use sizeof(Node) with malloc and link newNode->next to head [OK]
Common Mistakes:
Forgetting sizeof in malloc
Assigning head before setting newNode->next
Using prev pointer in singly linked list
3. What will be the linked list after executing the following code?
B. newNode->data should be assigned after head update
C. newNode->next should be NULL instead of head
D. newNode is not allocated memory; fix by malloc(sizeof(Node))
Solution
Step 1: Check newNode initialization
newNode is declared but not allocated memory with malloc, so it points to garbage.
Step 2: Fix by allocating memory
Use newNode = malloc(sizeof(Node)) before accessing newNode->data or newNode->next.
Final Answer:
newNode is not allocated memory; fix by malloc(sizeof(Node)) -> Option D
Quick Check:
Always malloc before using newNode [OK]
Hint: Always allocate memory before using new node [OK]
Common Mistakes:
Using uninitialized pointer
Assigning data before allocation
Changing head before linking newNode
5. You want to push multiple values 5, 15, 25 onto an empty linked list using the push function below. After pushing, you want to print the list from head to tail. Which code snippet correctly performs this and prints the list?
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* head = NULL;
void push(int val) {
Node* newNode = malloc(sizeof(Node));
newNode->data = val;
newNode->next = head;
head = newNode;
}
// Your code here to push and print