Unit 3 of 4 · B.Sc IT Sem 3

Unit 3: Linked lists

Data Structures notes · PTU syllabus (BSIT302/BSBC302)

3 min read6 topics10 exam questions
On this page
  1. Unit summary
  2. Linked lists and comparison with arrays
  3. Singly linked list operations
  4. Circular and doubly linked lists
  5. Dynamic storage management
  6. Generalised lists
  7. Garbage collection
  8. Key terms
  9. Quick revision
  10. Important questions

Unit summary

Linked lists store data in nodes connected by pointers, growing and shrinking as needed. This unit covers singly, circular and doubly linked lists, dynamic storage management, generalised lists and garbage collection.

After this unit you can

  • Implement singly linked list operations
  • Implement circular and doubly linked lists
  • Explain dynamic storage management and generalised lists
  • Explain garbage collection

PTU syllabus topics

  • Single linked list
  • circular linked list
  • doubly linked list
  • dynamic storage management
  • generalized list
  • garbage collection
ComparisonTypes of linked list
Links
Advantage

Singly linked

Next pointer only

Simple, less memory

Doubly linked

Next and previous pointers

Move both ways, easy deletion

Circular

Last node points to the first

Round-robin traversal

1

Topic 1

Linked lists and comparison with arrays

A linked list is a linear collection of nodes, where each node contains data and a pointer (link) to the next node. The first node is reached through a pointer called START (or HEAD); the last node's link is NULL.

ComparisonArray vs linked list
Array
Linked list

Memory

Consecutive locations

Scattered; nodes linked by pointers

Size

Fixed at declaration

Dynamic: grows and shrinks

Access

Direct by index: O(1)

Sequential from START: O(n)

Insertion/deletion

Slow: elements must be shifted

Fast: only pointers change

Extra memory

None

One pointer per node

2

Topic 2

Singly linked list operations

  • Traversal: start with ptr = start; while ptr != NULL, process ptr->data and move ptr = ptr->next.
  • Search: traverse and compare each node's data with the item: O(n).
ProcessInserting a node at the beginning
  1. 1Create a new node

    newnode = malloc(...)

  2. 2Store data

    newnode->data = item

  3. 3Link to old first node

    newnode->next = start

  4. 4Move START

    start = newnode

  • Insert at end: traverse to the last node and set last->next = newnode, with newnode->next = NULL.
  • Insert after a given node: newnode->next = loc->next; loc->next = newnode;
  • Delete: make the previous node skip the deleted one: prev->next = loc->next; then free(loc). Deleting the first node changes START.

Exam tip

Always handle the special cases: empty list (underflow), inserting into an empty list and deleting the first node.

3

Topic 3

Circular and doubly linked lists

  • Doubly linked list: each node has two pointers, prev and next, so it can be traversed in both directions and a node can be deleted without searching for its predecessor. It uses more memory.
  • Circular linked list: the last node points back to the first instead of NULL, so the list can be traversed from any node. It is useful for round-robin scheduling.
ComparisonTypes of linked list
Pointers per node
Main advantage

Singly

One (next)

Simple, least memory

Doubly

Two (prev, next)

Two-way traversal, easy deletion

Circular

One, last points to first

Continuous looping from any node

4

Topic 4

Dynamic storage management

  • Dynamic allocation: memory requested at run time — malloc, calloc, realloc and free in C; new and delete in C++.
  • Free list (available space list): a linked list of free memory blocks maintained by the system.
  • Allocation strategies: first fit, best fit, worst fit; fragmentation — external (scattered free blocks) and internal (unused space inside blocks); compaction merges free blocks.
cstruct node *p = (struct node *) malloc(sizeof(struct node));
if (p == NULL) { printf("Overflow\n"); }
free(p);   /* return memory to the free pool */
5

Topic 5

Generalised lists

  • Generalised list: a list whose elements may be atoms or lists themselves — e.g., (a, (b, c), d).
  • Node structure: a tag field (atom or list), a data or down-pointer field and a next pointer.
  • Uses: representing polynomials in several variables, LISP data, nested structures.
6

Topic 6

Garbage collection

  • Garbage: memory blocks no longer reachable from any pointer.
  • Garbage collection: automatically finding and reclaiming such memory.
ProcessMark-and-sweep garbage collection
  1. 1Mark

    Traverse from root pointers and mark every reachable node

  2. 2Sweep

    Scan memory; add unmarked nodes to the free list

  3. 3Unmark

    Clear marks for the next cycle

  • Other methods: reference counting (fails with cycles), copying collectors; languages such as Java and Python collect garbage automatically, while C requires manual free.
  • Memory leak: allocated memory never freed and no longer reachable.

Key terms

Node
Element of a linked list with data and pointer fields
Doubly linked list
List with forward and backward pointers
Free list
List of available memory blocks
Generalised list
List whose elements may themselves be lists
Garbage collection
Automatic reclamation of unreachable memory

Quick revision

  • Linked list vs array.
  • SLL traversal, insertion, deletion, search.
  • Circular and doubly linked lists.
  • malloc and free; free list; first, best, worst fit; fragmentation; compaction.
  • Generalised lists; mark-and-sweep, reference counting; memory leaks.

Important exam questions

Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).

Short-answer questions

  1. Q1.State two advantages of linked lists over arrays.
  2. Q2.Write the node structure of a doubly linked list.
  3. Q3.What is a circular linked list?
  4. Q4.Distinguish first fit and best fit.
  5. Q5.What is a generalised list?
  6. Q6.What is garbage collection?

Long-answer questions

  1. Q1.Explain singly linked list operations with algorithms.
  2. Q2.Explain circular and doubly linked lists.
  3. Q3.Explain dynamic storage management and fragmentation.
  4. Q4.Explain generalised lists and garbage collection.

Stuck on this unit?

Message SBS on WhatsApp for help with Data Structures, or to ask about studying B.Sc IT at Synetic.

WhatsApp us