Unit 3: Linked lists
Data Structures notes · PTU syllabus (BSIT302/BSBC302)
On this page
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
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
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.
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
Topic 2
Singly linked list operations
- Traversal: start with
ptr = start; whileptr != NULL, processptr->dataand moveptr = ptr->next. - Search: traverse and compare each node's data with the item: O(n).
- 1Create a new node
newnode = malloc(...)
- 2Store data
newnode->data = item
- 3Link to old first node
newnode->next = start
- 4Move START
start = newnode
- Insert at end: traverse to the last node and set
last->next = newnode, withnewnode->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;thenfree(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.
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.
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
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 */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.
Topic 6
Garbage collection
- Garbage: memory blocks no longer reachable from any pointer.
- Garbage collection: automatically finding and reclaiming such memory.
- 1Mark
Traverse from root pointers and mark every reachable node
- 2Sweep
Scan memory; add unmarked nodes to the free list
- 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
- Q1.State two advantages of linked lists over arrays.
- Q2.Write the node structure of a doubly linked list.
- Q3.What is a circular linked list?
- Q4.Distinguish first fit and best fit.
- Q5.What is a generalised list?
- Q6.What is garbage collection?
Long-answer questions
- Q1.Explain singly linked list operations with algorithms.
- Q2.Explain circular and doubly linked lists.
- Q3.Explain dynamic storage management and fragmentation.
- 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.
