MCS-208 · Unit 3

Lists

Section 3 of 8

Block 1 · Unit 3Lists11 min · Section 3 of 8

3.3Singly Linked Lists: Nodes, Creation and Traversal

Covers IGNOU MCS-208 §3.4

An array list must shift elements and has a fixed size. A linked list avoids both problems by storing each element in its own block of memory and connecting the blocks with pointers. This page introduces the node, the head pointer and the basic loop that walks along a list.

Node type
typedef struct node {
    int data;              /* the element                               */
    struct node *next;     /* address of the next node, or NULL at the end */
} Node;

Node *head = NULL;         /* an empty list */
A list with three nodes (addresses are examples)
 head            1000                 1050                 1200
+------+      +-------+------+     +-------+------+     +------+------+
| 1000 | ---> | madan | 1050 | --> | mohan | 1200 | --> | babu | NULL |
+------+      +-------+------+     +-------+------+     +------+------+

The nodes can be anywhere in memory; only the links fix their order.

Creating nodes and traversing

A new node is created at run time with malloc, which may return NULL when memory runs out, so the result must be checked. Every node created with malloc must be released with free when it is no longer needed.

To traverse the list (visit every node), start at head and follow next until you reach NULL:

The traversal loop
for (Node *p = head; p != NULL; p = p->next)
    visit(p->data);

The same loop counts the nodes (Check Your Progress, Q4) or prints their addresses (Q1): replace visit by count++ or by printf("%p\n", (void *)p). Each is O(n)O(n).

Key takeaways

  • A node = data + pointer to the next node; the list is reached through head; the last link is NULL.
  • Empty list: head == NULL; nodes are created with malloc (checked) and released with free.
  • Traversal, counting and printing all use the loop p = head; p != NULL; p = p->next: O(n)O(n).
  • A function that may change head must receive &head or return the new head.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

How is an empty singly linked list represented?

Show answer

Answer: C. A head pointer equal to NULL

With no nodes there is nothing for head to point to. A node whose next is NULL is a list with one element, not an empty list.

Question 2 · multiple choice

What does this program print?

C · q3-traverse.c
#include <stdio.h>

typedef struct node {
    int data;
    struct node *next;
} Node;

int main(void)
{
    Node c = {30, NULL}, b = {20, &c}, a = {10, &b};
    int sum = 0, n = 0;
    for (Node *p = &a; p->next != NULL; p = p->next) {
        sum += p->data;
        n++;
    }
    printf("%d %d\n", n, sum);
    return 0;
}
Show answer

Answer: B. 2 30

The condition p->next != NULL stops at the last node without visiting it, so only 10 and 20 are added. With p != NULL the result would be 3 60.

Question 3 · multiple choice

Why does append in the example take a parameter of type Node **?

Show answer

Answer: B. Because appending to an empty list must change the caller’s head variable

C passes arguments by value. To change the caller’s head, the function needs its address. The alternative is to return the new head.

Question 4 · multiple choice

What is the time complexity of counting the nodes of a singly linked list with nn nodes?

Show answer

Answer: D. O(n)O(n)

Each node is visited once. Counting is O(1)O(1) only if the list keeps a separate size field that is updated on every insertion and deletion.

Question 5 · multiple choice

Which line correctly frees every node of a list?

Show answer

Answer: C. while (head) { Node *n = head->next; free(head); head = n; }

The next pointer must be saved before the node is freed. The first and last options read next from memory that has already been freed; free(head) alone frees only the first node.

Question 6 · short answer

Write a C function that prints the memory address and data of every node of a singly linked list (Check Your Progress, Q1).

Show worked answer

void print_locations(const Node *head) { for (const Node *p = head; p != NULL; p = p->next) printf("%p: %d\n", (void *)p, p->data); } The loop works for an empty list (prints nothing), and %p with a (void *) cast is the correct way to print a pointer. The addresses differ from run to run.

↑ ↓ move↵ openTab switch type