Linked lists
Linked lists
2
Implementation of a List
3
Implementation of Contiguous List
To implement a contiguous list, we need to define
a large enough array to hold all the items of the
list.
The first item of the list is
Placed at 0th position of array
and successive items in successive
positions of the array.
The last item of the list
is indicated by “last index”.
In the figure, Last index=7.
4
Implementation of Contiguous List…
Assumptions
MAXLIST is defined.
CList[MAXLIST] is defined as an array to hold the items of the list.
last_index is defined to indicate index the last element.
Initially, last_index=-1.
Insertion:
1.Read item x to insert.
2.If last_index==MAXLIST-1, declare list full and return.
3.If last_index==-1, increment last_index and perform Clist[0]=x and return.
4.Read position pos to insert (pos=0 means CList[0]).
5.If pos>last_index+1, declare invalid and return.
i=last_index;
while(i>=pos)
CList[i+1]=CList[i];
i=i-1;
Clist[pos]=x;
Increment last_index
Return 5
Implementation of Contiguous List…
Assumptions
Deletion:
1. If last_index==-1, declare list empty and return.
2. Read position pos to delete.
3. If pos>last_index, declare invalid and return.
4. x=CList[pos];
5.i=pos;
6. while(i<last_index)
CList[i]=CList[i+1];
i=i+1;
7. Decrement last_index
8. Return x as deleted item
6
Linked List (Linear/Singly Linked List)
A linked list is defined as a collection of nodes in
which each node has two parts:
(i) information part and
(ii) link part
To store a set of items in linked list representation, the
information part of a node contains the actual item of
the list while the link part of the node contains the
address of the next node in the list.
The address of the last node of a linked list is NULL.
7
C Implementation of Linked List
In C, a linked list template is created using a self-referential
structure and nodes of the linked list are created and removed
using dynamic memory allocation functions like malloc() and
free(). We consider startnode as an external pointer. This
pointer helps in creating and accessing other nodes in the linked
list.
The following code defines the structure for linked list and
creates a startnode.
struct linkedlist
{
int info;
struct linkedlist *link;
};
struct linkedlist *startnode;
startnode=NULL;
Note: The pointer member variable link points to (i.e. contains
address of ) a structure variable of the structure type linkedlist.
8
9
Creating the empty list:
void createEmptyList(NodeType *head)
{
head=NULL;
}
10
11
12
13
14
15
16
17
18
19
20
21
22
Prepared by : Narayan Dhamala 23
24
25
26
27
28
29
30
31
32
33
34
35
Assignment #2
Write the advantages and disadvantage of different
types of linked list.
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
Advantages and disadvantages of singly linked list
Advantages of Singly linked list
Singly linked list is probably the most easiest data structure to implement.
Insertion and deletion of element can be done easily.
Insertion and deletion of elements doesn't requires movement of all elements
when compared to an array.
Requires less memory when compared to doubly, circular or doubly circular
linked list.
Can allocate or deallocate memory easily when required during its execution.
It is one of most efficient data structure to implement when traversing in one
direction is required.
Disadvantages of Singly linked list
It uses more memory when compared to an array.
Since elements are not stored sequentially hence requires more time to access
each elements of list.
Traversing in reverse is not possible in case of Singly linked list when compared
to Doubly linked list.
Requires O(n) time on appending a new node to end. Which is relatively very
high when compared to array or other linked list.
51
Advantage and disadvantage of doubly linked list
Advantages of doubly linked list
Bidirectional traversing is possible.
Easy to insert and delete a node to/from a linked list
(as there is no need to keep track of the previous node
during traversal or no need to traverse to find the
previous node).
Disadvantages of doubly linked list
Complex to handle double pointer.
More memory space is needed.
52
53