DSA Tutorial3 Linked Lists
DSA Tutorial3 Linked Lists
DSA Tutorial3 Linked Lists
By Omieno K.Kelvin
Department of Computer Science-MMUST (www.mmust.ac.ke)
Linked List
Sequentially related information can be stored in non-sequential storage locations
List Nodes are the containers that hold the information; Links are stored with the information to
determine the location of the next item; Links are usually array subscripts or pointers; The position
of the first item is stored separately
Stored in an Array
The linked list is a very flexible dynamic data structure: items may be added to it or deleted from it at will.
A programmer need not worry about how many items a program will have to accommodate: this allows us to
write robust programs which require much less maintenance. A very common source of problems in program
maintenance is the need to increase the capacity of a program to handle larger collections: even the most
generous allowance for growth tends to prove inadequate over time!
In a linked list, each item is allocated space as it is added to the list. A link is kept with each item to
the next item in the list.
Each node of the list has two elements
As items are added to a list, memory for a node is dynamically allocated. Thus the number of items
that may be added to a list is limited only by the amount of memory available.
Adding to a list
The simplest strategy for adding an item to a list is to:
This strategy is fast and efficient, but each item is added to the head of the list.
An alternative is to create a structure for the list which contains both head and tail pointers:
struct fifo_list {
struct node *head;
struct node *tail;
};
List variants
They permit scanning or searching of the list in both directions. (To go backwards in a simple list, it is
necessary to go back to the start and scan forwards.) Many applications require searching backwards and
forwards through sections of a list: for example, searching for a common name like "Kim" in a Korean
telephone directory would probably need much scanning backwards and forwards through a small region of
the whole list, so the backward links become very useful. In this case, the node structure is altered to have
two links:
struct t_node {
void *item;
struct t_node *previous;
struct t_node *next;
} node;
Lists in arrays
Although this might seem pointless (Why impose a structure which has the overhead of the "next" pointers
on an array?), this is just what memory allocators do to manage available space.
Memory is just an array of words. After a series of memory allocations and de-allocations, there are
blocks of free memory scattered throughout the available heap space. In order to be able to re-use
this memory, memory allocators will usually link freed blocks together in a free list by writing pointers
to the next free block in the block itself. An external free list pointer points to the first block in the
free list. When a new block of memory is requested, the allocator will generally scan the free list
looking for a freed block of suitable size and delete it from the free list (re-linking the free list
around the deleted block). Many variations of memory allocators have been proposed: refer to a text
on operating systems or implementation of functional languages for more details. The entry in the
index under garbage collection will probably lead to a discussion of this topic.
Key terms
Dynamic data structures
Structures which grow or shrink as the data they hold changes. Lists, stacks and trees are all
dynamic structures.
More intrusive notes