Applications of Linked List-2
Applications of Linked List-2
Outline
• Linked Stack
– Operations and Algorithm
• Linked Queue
– Operations and Algorithm
• Polynomial Manipulation
– Operations and Algorithm
• Garbage Collection and Compaction
Implementation of stacks and queues
using linked lists.
• Stacks and Queues are easier to implement
using linked lists.
• There is no need to set a limit on the size of
the stack.
• Easy to develop Push, pop and Enqueue and
dequeue functions for nodes containing
integer data.
• For Queue operations, we can maintain two
pointers – qfront and qback as we had done for
the case of array implementation of queues
• Multiplying polynomials:
(2x – 3)(2x2 + 3x – 2)
= 2x(2x2 + 3x – 2) – 3(2x2 + 3x – 2)
= 4x3 + 6x2 – 4x – 6x2 – 9x+ 6
= 4x3 – 13x+ 6
5 x12 + 2 x9 + 4x7 + 6x5 + x2 + 12x
• Now this 12th order polynomial does not have all the 13
terms (including the constant term).
Let us call malloc to create a new node p3 to build the third list
p3 = phead3
if ( p1 != NULL)
append (p1, phead3) ;
else
append (p2, phead3);
http://www.manojagarwal.co.in/polynomial-
representation-addition-multiplication/
Memory Management
Allocation
CODE 1. First Fit
2. Best Fit
3. Worst Fit
DATA
HEAP
STACK
Garbage collection
• Garbage collection(GC) is a dynamic approach
to automatic memory management and heap
allocation that processes and identifies dead
memory blocks and reallocates storage for
reuse.
• The primary purpose of garbage collection is
to reduce memory leaks.
GC implementation - Approaches
• Mark-and-sweep –
– In process when memory runs out, the GC locates all accessible memory
and then reclaims available memory.
• Reference counting –
– Allocated objects contain a reference count of the referencing number.
– When the memory count is zero, the object is garbage and is then
destroyed.
– The freed memory returns to the memory heap.
• Copy collection –
– There are two memory partitions.
– If the first partition is full, the GC locates all accessible data structures and
copies them to the second partition, compacting memory after GC process
and allowing continuous free memory.
Garbage Collection: General
Fragmentation causes:
Slow allocation
Premature GC when allocating large objects
Bad chances for allocating huge objects