Unit – III
Linked Lists
19/01/2025 FOP-DSA-Unit-III 2
What to study– SPPU Recommendations
 Unit III Linked Lists 08 Hours
 Concept, Comparison of sequential and linked organizations,
Primitive operations, Realization of Linked Lists, Realization of
linked list using arrays, Dynamic Memory Management, Linked list
using dynamic memory management, Linked List Abstract Data
Type, Linked list operations, Head pointer and header node,
 Types of linked list- Linear and circular linked lists, Doubly
Linked List and operations, Circular Linked List, Singly circular
linked list, Doubly circular linked list, Polynomial
Manipulations - Polynomial addition, Multiplication of two
polynomials using linked list.
 Generalized Linked List (GLL) concept, representation of
polynomial and sets using GLL.
 Case Study- Garbage Collection
19/01/2025 FOP-DSA-Unit-III 3
Objectives and Outcome
 Objectives:
 To understand Dynamic Memory Management
 To be able to create information records in free memory
 To be able to access the records– read, search, add, delete,
modify, sort
 Outcome:
 Efficient use of available free memory
 Use of records which are spread over the memory space
 Develop good code for searching, sorting, indexed storage
19/01/2025 FOP-DSA-Unit-III 4
Simple Lists
 What is Linked List
 C++ Program to Implement Singly Linked List
 Class Declaration
 Creating Node
 Examples of Linked Lists
19/01/2025 FOP-DSA-Unit-III 5
What is a Linked List
 A linked list is a sequence of items (objects)
where every item is linked to the next.
 Graphically:
data data data data
head_ptr tail_ptr
19/01/2025 FOP-DSA-Unit-III 6
List ADT
 Mathematical description: a sequence of items
 Ai precedes Ai+1 for 1  i < n
 Operations
 First() = position
 Value(position) = item
 Next(position) = position
 Length() = integer
 Insert(item,position)
 Delete(position)
( A1 A2 … An-1 An )
length = n
19/01/2025 FOP-DSA-Unit-III 7
C++ Program to Implement Singly Linked List
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
/* Node Declaration */
struct node
{
int info;
struct node *next;
}*start
19/01/2025 FOP-DSA-Unit-III 8
Class Declaration
class node
{
public:
int data;
node *next
};
class single_llist
{
node * head;
public:
node* createnode(int);
void insertbegin();
.
.
.
single_llist()
{
head= NULL;
}
};
19/01/2025 FOP-DSA-Unit-III 9
Operations on Linked Lists
 Insert a new item
 At the head of the list, or
 At the tail of the list, or
 Inside the list, in some designated position
 Search for an item in the list
 The item can be specified by position, or by some value
 Delete an item from the list
 Search for and locate the item, then remove the item,
and finally adjust the surrounding pointers
 size( );
 isEmpty( )
19/01/2025 FOP-DSA-Unit-III 10
Insert a node
 Insert– At the Head
• The link value in the new item = old head_ptr
• The new value of head_ptr = newPtr
 Insert – At the Tail
• The link value in the new item = NULL
• The link value of the old last item = newPtr
 Insert – inside the List
• The link-value in the new item = link-value of prev
item
• The new link-value of prev item = newPtr
19/01/2025 FOP-DSA-Unit-III 11
Delete a node
 Delete – the Head Item
• The new value of head_ptr = link-value of the old
head item
• The old head item is deleted and its memory returned
 Delete – the Tail Item
• New value of tail_ptr = link-value of the 3rd
last item
• New link-value of new last item = NULL.
 Delete – an inside Item
• New link-value of the item located before the deleted
one =
the link-value of the deleted item
19/01/2025 FOP-DSA-Unit-III 12
19/01/2025 FOP-DSA-Unit-III 13
size() and isEmpty()
 We need to scan the items in the list from the
head_ptr to the last item marked by its link-value
being NULL
 Count the number of items in the scan, and return
the count. This is the size().
 Alternatively, keep a counter of the number of item,
which gets updated after each insert/delete. The
function size( ) returns that counter
 If head_ptr is NULL, isEmpty() returns true; else, it
returns false.
19/01/2025 FOP-DSA-Unit-III 14
Time of the Operations
 Time to search() is O(L) where L is the relative
location of the desired item in the List. In the worst
case. The time is O(n). In the average case it is
O(N/2)=O(n).
 Time for remove() is dominated by the time for
search and is thus O(n).
 Time for insert at head or at tail is O(1)(if tail pointer
is maintained).
 Time for insert at other positions is dominated by
search time and thus O(n).
 Time for size() is O(n), and time for isEmpty() is O(1)
19/01/2025 FOP-DSA-Unit-III 15
Implementations
Lis
t
Stac
k
Queu
e
Vector
Linked
List
Sorted
Vector
19/01/2025 FOP-DSA-Unit-III 16
Lis
t
Linked
List
Linked List
using
References
nodeB.value = “b”;
nodeC.value = “c”;
list = nodeB;
nodeB.next = nodeC
b c 
Linked List
using Arrays
“c” “b”
0 2
list = 4;
19/01/2025 FOP-DSA-Unit-III 17
Circular List
first
last
first
BAT CAT EAT FAT GAT HAT
last
19/01/2025 FOP-DSA-Unit-III 18
More Operations
 Update a given Node
 Reverse Linked List
 Display Elements of a linked list
19/01/2025 FOP-DSA-Unit-III 19
Reversing singly linked list
 Initialize three pointers prev as NULL, curr as head and
next as NULL.
 Iterate trough the linked list. In loop, do following.
// Before changing next of current,
// store next node
next = curr->next
 // Now change next of current
// This is where actual reversing happens
curr->next = prev
 // Move prev and curr one step forward
prev = curr
curr = next
19/01/2025 FOP-DSA-Unit-III 20
Node *current = head;
Node *prev = NULL, *next = NULL;
while (current != NULL)
{
// Store next
next = current->next;
// Reverse current node's pointer
current->next = prev;
// Move pointers one position ahead.
prev = current;
current = next;
}
head = prev;
19/01/2025 FOP-DSA-Unit-III 21
19/01/2025 FOP-DSA-Unit-III 22
Examples
 Heap Memory
 Hash Tables
 Stack and Queues
 File Allocation Tables
Advanced Linked Lists
19/01/2025 FOP-DSA-Unit-III 24
Doubly Linked List
 Drawbacks of Singly Linked Lists
 Solution to overcome them
 C++ program to create a doubly linked list
 Examples of DLL
19/01/2025 FOP-DSA-Unit-III 25
Operations on Doubly Linked Lists
 Insert a new item
 At the head of the list, or
 At the tail of the list, or
 Inside the list, in some designated position
 Search for an item in the list
 The item can be specified by position, or by some value
 Delete an item from the list
 Search for and locate the item, then remove the item,
and finally adjust the surrounding pointers
 size( );
 isEmpty( )
19/01/2025 FOP-DSA-Unit-III 26
Inserting Node p to the Right of Node x
in a Doubly Linked List
left data right
Header Node
x
p
q
q = x -> right;
p -> left = x;
p -> right = q;
q -> left = p;
x -> right = p;
19/01/2025 FOP-DSA-Unit-III 27
Deleting Node x in a Doubly Linked List
x
left data right
Header Node
x -> left -> right = x -> right;
x -> right -> left = x -> left;
19/01/2025 FOP-DSA-Unit-III 28
19/01/2025 FOP-DSA-Unit-III 29
Doubly Linked Circular List
19/01/2025 FOP-DSA-Unit-III 30
Examples
 Lift
 Thread Scheduler
19/01/2025 FOP-DSA-Unit-III 31
Polynomials
1
2
3 8
14


 x
x
a
14
3 2 8 1 0 0
a.first
6
10
14
10
3
8 x
x
x
b 


8 14 -3 10 10 6
b.first
19/01/2025 FOP-DSA-Unit-III 32
Addition of Two Polynomials (1)
 It is an easy way to represent a polynomial by
a linked list.
 Example of adding two polynomials a and b
a.first 14
3 2 8 1 0 0
p
b.first 14
8 -3 10 10 6 0
q
(i) p->exp == q->exp
c.first 11 14 0
19/01/2025 FOP-DSA-Unit-III 33
Addition of Two Polynomials (2)
a.first 14
3 2 8 1 0 0
p
b.first 14
8 -3 10 10 6 0
q
(ii) p->exp < q->exp
c.first 11 14 -3 10 0
19/01/2025 FOP-DSA-Unit-III 34
Addition of Two Polynomials (3)
a.first 14
3 2 8 1 0 0
p
b.first 14
8 -3 10 10 6 0
q
(iii) p->exp > q->exp
c.first 11 14 -3 10 2 8 0
19/01/2025 FOP-DSA-Unit-III 35
19/01/2025 FOP-DSA-Unit-III 36
Generalized Lists
 A generalized list, A, is a finite sequence of n 0
≥
elements, a0, a1, a2, …, an-1, where i, is either an atom
or a list. The elements i,0 i n –
≤ ≤ 1, that are not
atoms are said to be the sub lists of A.
 A list A is written as A = (a0, …, an-1 ), and the length of
the list is n.
 A list name is represented by a capital letter and an
atom is represented by a lowercase letter.
 a0 is the head of list A and the rest (a1, …,
an-1) is the tail of list A.
19/01/2025 FOP-DSA-Unit-III 37
Examples of Generalized Lists
 A = ( ): the null, or empty, list; its length is zero.
 B = (a, (b, c)): a list of length two; its first element is the atom a, and
its second element is the linear list (b, c).
 C = (B, B, ( )): A list of length three whose first two elements are the
list B, and the third element is the null list.
 D = (a, D): is a recursive list of length two; D corresponds to the
infinite list D = (a, (a, (a, …))).
 head(B) = ‘a’ and tail(B) = ((b, c)), head(tail(C))=B and tail(tail(C)) = ( ).
 Lists may be shared by other lists.
 Lists may be recursive.
19/01/2025 FOP-DSA-Unit-III 38
Representations of Generalized Lists
f a t 0
f b f c 0
B
t t t 0 0
C
f a t 0
D
A = 0 Empty list
B=(a, (b,
c))
C=(B, B,
( ))
D=(a, D)
19/01/2025 FOP-DSA-Unit-III 39
19/01/2025 FOP-DSA-Unit-III 40
19/01/2025 FOP-DSA-Unit-III 41
Reference Counts in Shared Lists
 Lists may be shared by other lists for storage
saving.
 Add a header node to store the reference count,
which is the number of pointers to it.
 The reference count can be dynamically
updated. The list can be deleted only when the
reference count is 0.
19/01/2025 FOP-DSA-Unit-III 42
Reference Counts in Shared Lists
D f 2 f a t 0
f 1 0
C f 1 t t t 0
B f 3 f a t 0
f 1 f b f c 0
B=(a, (b, c))
C=(B, B,
( ))
D=(a, D)
A f 1 0
header
node
reference count in header node: # of pointers to
the list
Free Memory
Management
19/01/2025 FOP-DSA-Unit-III 44
Free Memory Management
 Virtual Memory
 Dynamic Allocation of Pages
 What About malloc() and free()?
 The OS only allocates and frees memory in units of 4KB
page
 Each process manages its own heap memory
 There are many different strategies for managing free
memory
 Free Space Management
19/01/2025 FOP-DSA-Unit-III 45
Why Should You Care?
 Regardless of language, all of our code uses
dynamic memory
 However, there is a performance cost associated
with using dynamic memory
 Understanding how the heap is managed leads
to:
 More performant applications
 The ability to diagnose difficult memory related
errors and performance bottlenecks
19/01/2025 FOP-DSA-Unit-III 46
Key Challenges
 Maximizing CPU performance
 Keeping track of memory usage requires effort
 Maximize parallelism
 Heap memory is shared across threads
 Thus, synchronization may be necessary
 Minimizing memory overhead
 Metadata is needed to track memory usage
 This metadata adds to the size of each object
 Minimize fragmentation
 Over time, de-allocations create useless gaps in
memory
19/01/2025 FOP-DSA-Unit-III 47
How to Manage Free Memory?
Free Lists
 Basics
 Speeding Up malloc() and free()
 Slab Allocation
 Common Bugs
Garbage Collectors
 Reference Counting
 Mark and Sweep
 Generational/Ephemeral GC
 Parallel Garbage Collection
19/01/2025 FOP-DSA-Unit-III 48
The Free List
 A free list is a simple data structure for
managing heap memory
 Three key components
1. A linked-list that records free regions of memory
 Free regions get split when memory is allocated
 Free list is kept in sorted order by memory address
2. Each allocated block of memory has a header
that records the size of the block
3. An algorithm that selects which free region of
memory to use for each allocation request
• Design challenge: linked lists are dynamic
data structures
• Dynamic data structures go on the heap
• But in this case, we are implementing the
heap?!
19/01/2025 FOP-DSA-Unit-III 49
The Free List
 Allocating Memory (Splitting)
 Freeing Memory
 Choosing Free Regions
19/01/2025 FOP-DSA-Unit-III 50
Basic Free List Review
 Singly-linked free list
 List is kept in sorted order
 free() is an O(n) operation
 Adjacent free regions are coalesced
 Various strategies for selecting which free region to
use for a given malloc(n)
 First-fit: use the first free region with >=n bytes available
 Worst-case is O(n), but typically much faster
 Tends to lead to external fragmentation at the head of the list
 Best-fit: use the region with size closest (and >=) to n
 Less external fragments than first-fit, but O(n) time
19/01/2025 FOP-DSA-Unit-III 51
Improving Performance
1. Use a circular linked list and Next-Fit
 Faster than Best-Fit, less fragmentation than First-fit
2. Use a doubly-linked free list with footers
 Good: makes free() and coalesce O(1) time
 Bad: small amount of memory wasted due to headers and
footers
3. Use bins to quickly locate appropriately sized free
regions
 Good: much less external fragmentation, O(1) time
 Bad: much more complicated implementation
 Bad: some memory wasted due to internal fragmentation
19/01/2025 FOP-DSA-Unit-III 52
Two More Things
 How can you make your code manage memory
more quickly?
 Slab allocation
 Common memory bugs
 Memory leaks
 Dangling pointers
 Double free
19/01/2025 FOP-DSA-Unit-III 53
Speeding Up Your Code
 Typically, the memory allocation algorithm is not
under your control
 You don’t choose what library to use (e.g. glibc)
 You don’t know the internal implementation
 How can your make your code faster?
 Avoid the memory allocator altogether!
 Use an object cache plus slab allocation
19/01/2025 FOP-DSA-Unit-III 54
Garbage Collection
 Invented in 1959
 Automatic memory management
 The GC reclaims memory occupied by objects that
are no longer in use
 Such objects are called garbage
 Conceptually simple
1. Scan objects in memory, identify objects that
cannot be accessed (now, or in the future)
2. Reclaim these garbage objects
 In practice, very tricky to implement
19/01/2025 FOP-DSA-Unit-III 55
Approaches to GC
 Reference Counting
 Each object keeps a count of references
 If an objects count == 0, it is garbage
 Mark and Sweep
 Starting at the roots, traverse objects and “mark” them
 Free all unmarked objects on the heap
 Copy Collection
 Extends mark & sweep with compaction
 Addresses CPU and external fragmentation issues
 Generational Collection
 Uses heuristics to improve the runtime of mark & sweep
19/01/2025 FOP-DSA-Unit-III 56
Generational Collection
 Problem: mark and sweep is slow
 Expensive full traversals of live objects
 Expensive scan of heap memory
 Problem: copy collection is also slow
 Expensive full traversals of live objects
 Periodically, all live objects get copied
 Solution: leverage knowledge about object creation
patterns
 Object lifetime tends to be inversely correlated with
likelihood of becoming garbage (generational hypothesis)
 Young objects die quickly – old objects continue to live

Unit – III.pptx Data Structures and Algorithms

  • 1.
  • 2.
    19/01/2025 FOP-DSA-Unit-III 2 Whatto study– SPPU Recommendations  Unit III Linked Lists 08 Hours  Concept, Comparison of sequential and linked organizations, Primitive operations, Realization of Linked Lists, Realization of linked list using arrays, Dynamic Memory Management, Linked list using dynamic memory management, Linked List Abstract Data Type, Linked list operations, Head pointer and header node,  Types of linked list- Linear and circular linked lists, Doubly Linked List and operations, Circular Linked List, Singly circular linked list, Doubly circular linked list, Polynomial Manipulations - Polynomial addition, Multiplication of two polynomials using linked list.  Generalized Linked List (GLL) concept, representation of polynomial and sets using GLL.  Case Study- Garbage Collection
  • 3.
    19/01/2025 FOP-DSA-Unit-III 3 Objectivesand Outcome  Objectives:  To understand Dynamic Memory Management  To be able to create information records in free memory  To be able to access the records– read, search, add, delete, modify, sort  Outcome:  Efficient use of available free memory  Use of records which are spread over the memory space  Develop good code for searching, sorting, indexed storage
  • 4.
    19/01/2025 FOP-DSA-Unit-III 4 SimpleLists  What is Linked List  C++ Program to Implement Singly Linked List  Class Declaration  Creating Node  Examples of Linked Lists
  • 5.
    19/01/2025 FOP-DSA-Unit-III 5 Whatis a Linked List  A linked list is a sequence of items (objects) where every item is linked to the next.  Graphically: data data data data head_ptr tail_ptr
  • 6.
    19/01/2025 FOP-DSA-Unit-III 6 ListADT  Mathematical description: a sequence of items  Ai precedes Ai+1 for 1  i < n  Operations  First() = position  Value(position) = item  Next(position) = position  Length() = integer  Insert(item,position)  Delete(position) ( A1 A2 … An-1 An ) length = n
  • 7.
    19/01/2025 FOP-DSA-Unit-III 7 C++Program to Implement Singly Linked List #include<iostream> #include<cstdio> #include<cstdlib> using namespace std; /* Node Declaration */ struct node { int info; struct node *next; }*start
  • 8.
    19/01/2025 FOP-DSA-Unit-III 8 ClassDeclaration class node { public: int data; node *next }; class single_llist { node * head; public: node* createnode(int); void insertbegin(); . . . single_llist() { head= NULL; } };
  • 9.
    19/01/2025 FOP-DSA-Unit-III 9 Operationson Linked Lists  Insert a new item  At the head of the list, or  At the tail of the list, or  Inside the list, in some designated position  Search for an item in the list  The item can be specified by position, or by some value  Delete an item from the list  Search for and locate the item, then remove the item, and finally adjust the surrounding pointers  size( );  isEmpty( )
  • 10.
    19/01/2025 FOP-DSA-Unit-III 10 Inserta node  Insert– At the Head • The link value in the new item = old head_ptr • The new value of head_ptr = newPtr  Insert – At the Tail • The link value in the new item = NULL • The link value of the old last item = newPtr  Insert – inside the List • The link-value in the new item = link-value of prev item • The new link-value of prev item = newPtr
  • 11.
    19/01/2025 FOP-DSA-Unit-III 11 Deletea node  Delete – the Head Item • The new value of head_ptr = link-value of the old head item • The old head item is deleted and its memory returned  Delete – the Tail Item • New value of tail_ptr = link-value of the 3rd last item • New link-value of new last item = NULL.  Delete – an inside Item • New link-value of the item located before the deleted one = the link-value of the deleted item
  • 12.
  • 13.
    19/01/2025 FOP-DSA-Unit-III 13 size()and isEmpty()  We need to scan the items in the list from the head_ptr to the last item marked by its link-value being NULL  Count the number of items in the scan, and return the count. This is the size().  Alternatively, keep a counter of the number of item, which gets updated after each insert/delete. The function size( ) returns that counter  If head_ptr is NULL, isEmpty() returns true; else, it returns false.
  • 14.
    19/01/2025 FOP-DSA-Unit-III 14 Timeof the Operations  Time to search() is O(L) where L is the relative location of the desired item in the List. In the worst case. The time is O(n). In the average case it is O(N/2)=O(n).  Time for remove() is dominated by the time for search and is thus O(n).  Time for insert at head or at tail is O(1)(if tail pointer is maintained).  Time for insert at other positions is dominated by search time and thus O(n).  Time for size() is O(n), and time for isEmpty() is O(1)
  • 15.
  • 16.
    19/01/2025 FOP-DSA-Unit-III 16 Lis t Linked List LinkedList using References nodeB.value = “b”; nodeC.value = “c”; list = nodeB; nodeB.next = nodeC b c  Linked List using Arrays “c” “b” 0 2 list = 4;
  • 17.
    19/01/2025 FOP-DSA-Unit-III 17 CircularList first last first BAT CAT EAT FAT GAT HAT last
  • 18.
    19/01/2025 FOP-DSA-Unit-III 18 MoreOperations  Update a given Node  Reverse Linked List  Display Elements of a linked list
  • 19.
    19/01/2025 FOP-DSA-Unit-III 19 Reversingsingly linked list  Initialize three pointers prev as NULL, curr as head and next as NULL.  Iterate trough the linked list. In loop, do following. // Before changing next of current, // store next node next = curr->next  // Now change next of current // This is where actual reversing happens curr->next = prev  // Move prev and curr one step forward prev = curr curr = next
  • 20.
    19/01/2025 FOP-DSA-Unit-III 20 Node*current = head; Node *prev = NULL, *next = NULL; while (current != NULL) { // Store next next = current->next; // Reverse current node's pointer current->next = prev; // Move pointers one position ahead. prev = current; current = next; } head = prev;
  • 21.
  • 22.
    19/01/2025 FOP-DSA-Unit-III 22 Examples Heap Memory  Hash Tables  Stack and Queues  File Allocation Tables
  • 23.
  • 24.
    19/01/2025 FOP-DSA-Unit-III 24 DoublyLinked List  Drawbacks of Singly Linked Lists  Solution to overcome them  C++ program to create a doubly linked list  Examples of DLL
  • 25.
    19/01/2025 FOP-DSA-Unit-III 25 Operationson Doubly Linked Lists  Insert a new item  At the head of the list, or  At the tail of the list, or  Inside the list, in some designated position  Search for an item in the list  The item can be specified by position, or by some value  Delete an item from the list  Search for and locate the item, then remove the item, and finally adjust the surrounding pointers  size( );  isEmpty( )
  • 26.
    19/01/2025 FOP-DSA-Unit-III 26 InsertingNode p to the Right of Node x in a Doubly Linked List left data right Header Node x p q q = x -> right; p -> left = x; p -> right = q; q -> left = p; x -> right = p;
  • 27.
    19/01/2025 FOP-DSA-Unit-III 27 DeletingNode x in a Doubly Linked List x left data right Header Node x -> left -> right = x -> right; x -> right -> left = x -> left;
  • 28.
  • 29.
  • 30.
  • 31.
    19/01/2025 FOP-DSA-Unit-III 31 Polynomials 1 2 38 14    x x a 14 3 2 8 1 0 0 a.first 6 10 14 10 3 8 x x x b    8 14 -3 10 10 6 b.first
  • 32.
    19/01/2025 FOP-DSA-Unit-III 32 Additionof Two Polynomials (1)  It is an easy way to represent a polynomial by a linked list.  Example of adding two polynomials a and b a.first 14 3 2 8 1 0 0 p b.first 14 8 -3 10 10 6 0 q (i) p->exp == q->exp c.first 11 14 0
  • 33.
    19/01/2025 FOP-DSA-Unit-III 33 Additionof Two Polynomials (2) a.first 14 3 2 8 1 0 0 p b.first 14 8 -3 10 10 6 0 q (ii) p->exp < q->exp c.first 11 14 -3 10 0
  • 34.
    19/01/2025 FOP-DSA-Unit-III 34 Additionof Two Polynomials (3) a.first 14 3 2 8 1 0 0 p b.first 14 8 -3 10 10 6 0 q (iii) p->exp > q->exp c.first 11 14 -3 10 2 8 0
  • 35.
  • 36.
    19/01/2025 FOP-DSA-Unit-III 36 GeneralizedLists  A generalized list, A, is a finite sequence of n 0 ≥ elements, a0, a1, a2, …, an-1, where i, is either an atom or a list. The elements i,0 i n – ≤ ≤ 1, that are not atoms are said to be the sub lists of A.  A list A is written as A = (a0, …, an-1 ), and the length of the list is n.  A list name is represented by a capital letter and an atom is represented by a lowercase letter.  a0 is the head of list A and the rest (a1, …, an-1) is the tail of list A.
  • 37.
    19/01/2025 FOP-DSA-Unit-III 37 Examplesof Generalized Lists  A = ( ): the null, or empty, list; its length is zero.  B = (a, (b, c)): a list of length two; its first element is the atom a, and its second element is the linear list (b, c).  C = (B, B, ( )): A list of length three whose first two elements are the list B, and the third element is the null list.  D = (a, D): is a recursive list of length two; D corresponds to the infinite list D = (a, (a, (a, …))).  head(B) = ‘a’ and tail(B) = ((b, c)), head(tail(C))=B and tail(tail(C)) = ( ).  Lists may be shared by other lists.  Lists may be recursive.
  • 38.
    19/01/2025 FOP-DSA-Unit-III 38 Representationsof Generalized Lists f a t 0 f b f c 0 B t t t 0 0 C f a t 0 D A = 0 Empty list B=(a, (b, c)) C=(B, B, ( )) D=(a, D)
  • 39.
  • 40.
  • 41.
    19/01/2025 FOP-DSA-Unit-III 41 ReferenceCounts in Shared Lists  Lists may be shared by other lists for storage saving.  Add a header node to store the reference count, which is the number of pointers to it.  The reference count can be dynamically updated. The list can be deleted only when the reference count is 0.
  • 42.
    19/01/2025 FOP-DSA-Unit-III 42 ReferenceCounts in Shared Lists D f 2 f a t 0 f 1 0 C f 1 t t t 0 B f 3 f a t 0 f 1 f b f c 0 B=(a, (b, c)) C=(B, B, ( )) D=(a, D) A f 1 0 header node reference count in header node: # of pointers to the list
  • 43.
  • 44.
    19/01/2025 FOP-DSA-Unit-III 44 FreeMemory Management  Virtual Memory  Dynamic Allocation of Pages  What About malloc() and free()?  The OS only allocates and frees memory in units of 4KB page  Each process manages its own heap memory  There are many different strategies for managing free memory  Free Space Management
  • 45.
    19/01/2025 FOP-DSA-Unit-III 45 WhyShould You Care?  Regardless of language, all of our code uses dynamic memory  However, there is a performance cost associated with using dynamic memory  Understanding how the heap is managed leads to:  More performant applications  The ability to diagnose difficult memory related errors and performance bottlenecks
  • 46.
    19/01/2025 FOP-DSA-Unit-III 46 KeyChallenges  Maximizing CPU performance  Keeping track of memory usage requires effort  Maximize parallelism  Heap memory is shared across threads  Thus, synchronization may be necessary  Minimizing memory overhead  Metadata is needed to track memory usage  This metadata adds to the size of each object  Minimize fragmentation  Over time, de-allocations create useless gaps in memory
  • 47.
    19/01/2025 FOP-DSA-Unit-III 47 Howto Manage Free Memory? Free Lists  Basics  Speeding Up malloc() and free()  Slab Allocation  Common Bugs Garbage Collectors  Reference Counting  Mark and Sweep  Generational/Ephemeral GC  Parallel Garbage Collection
  • 48.
    19/01/2025 FOP-DSA-Unit-III 48 TheFree List  A free list is a simple data structure for managing heap memory  Three key components 1. A linked-list that records free regions of memory  Free regions get split when memory is allocated  Free list is kept in sorted order by memory address 2. Each allocated block of memory has a header that records the size of the block 3. An algorithm that selects which free region of memory to use for each allocation request • Design challenge: linked lists are dynamic data structures • Dynamic data structures go on the heap • But in this case, we are implementing the heap?!
  • 49.
    19/01/2025 FOP-DSA-Unit-III 49 TheFree List  Allocating Memory (Splitting)  Freeing Memory  Choosing Free Regions
  • 50.
    19/01/2025 FOP-DSA-Unit-III 50 BasicFree List Review  Singly-linked free list  List is kept in sorted order  free() is an O(n) operation  Adjacent free regions are coalesced  Various strategies for selecting which free region to use for a given malloc(n)  First-fit: use the first free region with >=n bytes available  Worst-case is O(n), but typically much faster  Tends to lead to external fragmentation at the head of the list  Best-fit: use the region with size closest (and >=) to n  Less external fragments than first-fit, but O(n) time
  • 51.
    19/01/2025 FOP-DSA-Unit-III 51 ImprovingPerformance 1. Use a circular linked list and Next-Fit  Faster than Best-Fit, less fragmentation than First-fit 2. Use a doubly-linked free list with footers  Good: makes free() and coalesce O(1) time  Bad: small amount of memory wasted due to headers and footers 3. Use bins to quickly locate appropriately sized free regions  Good: much less external fragmentation, O(1) time  Bad: much more complicated implementation  Bad: some memory wasted due to internal fragmentation
  • 52.
    19/01/2025 FOP-DSA-Unit-III 52 TwoMore Things  How can you make your code manage memory more quickly?  Slab allocation  Common memory bugs  Memory leaks  Dangling pointers  Double free
  • 53.
    19/01/2025 FOP-DSA-Unit-III 53 SpeedingUp Your Code  Typically, the memory allocation algorithm is not under your control  You don’t choose what library to use (e.g. glibc)  You don’t know the internal implementation  How can your make your code faster?  Avoid the memory allocator altogether!  Use an object cache plus slab allocation
  • 54.
    19/01/2025 FOP-DSA-Unit-III 54 GarbageCollection  Invented in 1959  Automatic memory management  The GC reclaims memory occupied by objects that are no longer in use  Such objects are called garbage  Conceptually simple 1. Scan objects in memory, identify objects that cannot be accessed (now, or in the future) 2. Reclaim these garbage objects  In practice, very tricky to implement
  • 55.
    19/01/2025 FOP-DSA-Unit-III 55 Approachesto GC  Reference Counting  Each object keeps a count of references  If an objects count == 0, it is garbage  Mark and Sweep  Starting at the roots, traverse objects and “mark” them  Free all unmarked objects on the heap  Copy Collection  Extends mark & sweep with compaction  Addresses CPU and external fragmentation issues  Generational Collection  Uses heuristics to improve the runtime of mark & sweep
  • 56.
    19/01/2025 FOP-DSA-Unit-III 56 GenerationalCollection  Problem: mark and sweep is slow  Expensive full traversals of live objects  Expensive scan of heap memory  Problem: copy collection is also slow  Expensive full traversals of live objects  Periodically, all live objects get copied  Solution: leverage knowledge about object creation patterns  Object lifetime tends to be inversely correlated with likelihood of becoming garbage (generational hypothesis)  Young objects die quickly – old objects continue to live