The document summarizes chapter 4 on linked lists from a textbook. It covers different types of linked lists including singly linked lists, doubly linked lists, and circular lists. It describes how to implement basic linked list operations like insertion, deletion, and traversal. It also discusses using linked lists to implement stacks, queues, and sparse matrices. Dynamic storage management using linked lists and garbage collection techniques are explained.
1. Chapter4 - Linked List
http://icodeguru.com/vc/10book/books/book1/chap04.htm
Dr. R. Khanchana
Assistant Professor
Department of Computer Science
Sri Ramakrishna College of Arts and Science for
Women
2. 4.1 SINGLY LINKED LISTS
• A linked list is a series of connected nodes
• Each node contains at least
– A piece of data (any type)
– Pointer to the next node in the list
• Head: pointer to the first node
• The last node points to NULL
4. Singly Linked List
• For example
– consider the following list
of all of the three letter
English words ending in AT:
• (BAT, CAT, EAT, FAT, HAT,
JAT, LAT, MAT, OAT, PAT,
RAT, SAT, TAT, VAT, WAT)
5. Linked List -Representation
To insert the data item GAT between FAT and HAT the
following steps are adequate:
(i) get a node which is currently unused; let its address be X;
(ii) set the DATA field of this node to GAT;
(iii) set the LINK field of X to point to the node after FAT which
contains HAT;
(iv) set the LINK field of the node containing FAT to X.
8. DATA and LINK
• GETNODE(X) which provides in X a pointer to a
free node but if no node is free, it prints an
error message and stops;
• RET(X) which returns node X to the storage
pool.
9. Linked List –CREATE Procedure
• Example 4.1: Assume that each node has two fields DATA and LINK.
The following algorithm creates a linked list with two nodes whose DATA fields are
set to be the values 'MAT' and 'PAT' respectively. T is a pointer to the first node in
this list.
10. Linked List – INSERT Procedure
Example 4.2 : Let T be a pointer to a linked list as in Example 4.1. T= 0
if the list has no nodes. Let X be a pointer to some arbitrary node in the
list T. The following algorithm inserts a node with DATA field 'OAT' following
the node pointed at by X.
11. Linked List – DELETE Procedure
• Example 4.3: Let X be a pointer to some node in a linked
list T as in example 4.2. Let Y be the node preceding X. Y = 0
if X is the first node in T (i.e., if X = T). The following algorithm
deletes node X from T.
15. Initial & Boundary Conditions in LL
If we wish to represent n stacks and m queues simultaneously, then
the following set of algorithms and initial conditions
33. 4.5 MORE ON LINKED LISTS
1) INIT which originally links together the AV list
2) GETNODE and
3) RET which get and return nodes to AV
Procedures
INVERT
CONCATENATE
INSERT FRONT
LENGTH
50. DYNAMIC STORAGE MANAGEMENT
Assume we start off with
100,000 words of memory and
five programs P1, P2, P3, P4
and P5 make requests of size
10,000, 15,000, 6,000, 8,000
and 20,000 respectively.
Figure 4.14 indicates the status
of memory after storage for P5
has been allocated.
Figure 4.15 show the status of
memory after the blocks for P2
and P4 are freed.
52. First Fit Strategy
• If we now receive a request for a block of
memory of size N, then it is necessary to search
down the list of free blocks finding the first block
of size N and allocating N words out of this block.
Such an allocation strategy is called first fit.
• The algorithm below makes storage allocations
using the first fit strategy.
– An alternate strategy, best fit, calls for finding a free
block whose size is as close to N as possible, but not
less than N.
54. Determination of Free Nodes
It is not easy to determine whether blocks
adjacent to the block (n, p) (n = size of
block and p = starting location) being
returned are free. The only way to do this,
at present, is to examine all the nodes
in AV to determine whether:
(i) the left adjacent block is free, i.e., the
block ending at p - 1;
(ii) the right adjacent block is free, i.e., the
block beginning at p + n.
In order to determine (i) and (ii) above
without searching the available space list,
we adopt the node structure of figure
4.19 for allocated and free nodes:
59. Garbage Collection and Compaction
• Garbage collection is the process of collecting all unused
nodes and returning them to available space.
– This process is carried out in essentially two phases.
• First phase, known as the marking phase, all nodes in use are
marked.
• Second phase all unmarked nodes are returned to the
available space list.
• When variable size nodes are in use, it is desirable to compact
memory so that all free nodes form a contiguous block of
memory. In this case the second phase is referred to as
memory compaction. Compaction of disk space to reduce
average retrieval time is desirable even for fixed size nodes.
•
60. Marking
• Marking means need to a mark bit in each node.
It will be assumed that this mark bit can be
changed at any time by the marking algorithm.
• Marking algorithms mark
– all directly accessible nodes (i.e., nodes accessible
through program variables referred to as pointer
variables)
– all indirectly accessible nodes (i.e., nodes accessible
through link fields of nodes in accessible lists).
61. Marking
• Each node will have a one bit mark field, MARK, as well as a
one bit tag field, TAG.
• The tag bit of a node will be zero if it contains atomic
information. The tag bit is one otherwise.
• A node with a tag of one has two link fields DLINK and
RLINK.
• Atomic information can be stored only in a node with tag 0.
Such nodes are called atomic nodes.
• All other nodes are list nodes. This node structure is slightly
different from the one used in the previous section where a
node with tag 0 contained atomic information as well as a
RLINK. It is usually the case that the DLINK field is too small
for the atomic information and an entire node is required.