Single Linked List
This lab teaches you the following topics:
 Creation of singly linked list
 Insertion in singly linked list
 Deletion from singly linked list
 Traversal of all nodes
Introduction
A list is a finite ordered set of elements of a certain type.
The elements of the list are called cells or nodes.
A list can be represented statically, using arrays or, more often, dynamically, by allocating and releasing memory as needed. In the case of static lists, the ordering is given implicitly by the onedimension array. In the case of dynamic lists, the order of nodes is set by pointers. In this case, the cells are allocated dynamically in the heap of the program. Dynamic lists are typically called linked lists, and they can be singly or doublylinked.
A singlylinked list may be depicted as in Figure 1.1.
Lab Activities:
Activity 1:
Creation of linked list
Solution:
Consider the following steps which apply to dynamic lists:
 Initially, the list is empty. This can be coded by setting the pointers to the first and last cells to the special value
NULL, i.e. first = NULL, last = NULL.
 Reserve space for a new node to be inserted in the list:
/* reserve space */
p = ( NodeT *Â ) malloc( sizeof( NodeT ) ) ;
Then place the data into the node addressed by p. The implementation depends on the type of data the node holds. For primitive types, you can use an assignment such as âˆ—p = data.
 Make the necessary connections:
pâˆ’>next = NULL;Â Â Â Â Â Â Â Â Â Â /* node i s appended to the l i s t */
i f ( last != NULL )Â Â Â Â /* l i s t i s not empty */
Â Â lastâˆ’>next = p;
else
Â Â f i r s t = p;Â Â Â Â Â Â Â Â Â Â /* f i r s t node */
Â Â last = p;
Activity 2:
Accessing nodes of linked list
Solution:
The nodes of a list may be accessed sequentially, gathering useful information as needed. Typically, a part of the information held at a node is used as a key for finding the desired information. The list is scanned linearly, and the node may or may not be present on the list. A function for searching a specific key may contain the following sequence of statements:
NodeTÂ *p;
p = f i r s t ;
while ( p != NULL )
i f ( pâˆ’>key = givenKey )
{
return p;Â Â Â Â Â /* key found at address p */
}
else
Â Â Â Â Â Â Â Â Â Â Â {
p = pâˆ’>next ;
return NULL;Â Â Â Â Â Â Â Â Â /* not found */
Â Â Â Â Â Â Â Â Â Â Â }
Activity 3:
Insertion in single linked list
Solution:
The node to be inserted may be created as shown at fig1.3. We will assume here that the node to insert is pointed to by p.
 If the list was empty then there would be only one node, i.e.
i f ( f i r s t == NULL )
{
f i r s t = p;
last = p;
pâˆ’>next = NULL;
}
 If the list was not empty, then insertion follows different patterns depending on the position where the node is to be Thus, we have the following cases:
 Insertion before the first node of the list:
i f ( f i r s t != NULL )
{
pâˆ’>next = f i r s t ;
f i r s t = p;
}

 Insertion after the last node of the list (this operation is also called append):
i f ( last != NULL )
{
pâˆ’>next= NUL L;
lastâˆ’>next= p;
last= p;
}
 Insertion before a node given by its key.
There are two steps to execute:
a. Search for the node containing the given Key:
NodeT *q, *q1;
q1 = NULL;Â Â Â Â Â Â Â Â /* initialize Â */
q = f i r s t ;
while ( q != NULL )
{
i f ( qâˆ’>key == givenKey )
break;
q1 = q;
q = qâˆ’>next ;
}
b. Insert the node pointed to by p, and adjust links:
i f ( q != NULL )
{
/* node with key given Key has address q */
i f ( q == f i r s t )
{
Â /* insert after first node */
pâˆ’>next = f i r s t ;
f i r s t = p;
}
else
{
q1âˆ’>next = p;
pâˆ’>next = q;
}
}
 Insertion after a node given by its key. Again, there are two steps to execute:
a. SearchÂ for the node containing the givenKey:
NodeT *q, *q1;
q1 = NULL;Â Â Â Â Â Â /* initialize Â */
q = f i r s t ;
while ( q != NULL )
{
i f ( qâˆ’>key == givenKey ) break;
q1 = q;
q = qâˆ’>next ;
}
b. Insert the node pointed to by p, and adjust links:
i f ( q != NULL )
{
pâˆ’>next = qâˆ’>next ;Â Â Â Â Â Â /* node with key givenKey has address q */
qâˆ’>next = p;
i f ( q == last )
Â Â Â last = p;
Â Â Â Â Â Â Â Â Â Â }
Activity 4:
Deleting a node from single linked list
Solution:
When we are to remove a node from a list, there are some aspects to take into account:
 list may be empty; (ii) list may contain a single node; (iii) list has more than one And, also, deletion of the first, the last or a node given by its key may be required. Thus we have the following cases:
 Removing the first node of a list
NodeT *p;
i f ( f i r s t != NULL )
{
/*Â nonâˆ’empty l i s t*/
p = f i r s t ;
f i r s t = f irst âˆ’>next ;
free ( p ) ;Â Â Â Â Â Â /* free up memory */
i f ( f i r s t == NULL )Â Â Â Â Â /*Â list is now empty */
last = NULL;
}
 Removing the last node of a list
NodeT *q, *q1;
q1 = NULL;Â Â Â Â Â Â Â /*Â initialize */
q = f i r s t ;
i f ( q != NULL )
{Â Â Â
/*Â nonâˆ’empty list */
while ( q != last )
{Â Â Â /* advance towards end */
q1 = q;
q = qâˆ’>next ;
}
i f ( q == f i r s t )
{
/* onlyÂ Â one node */
f i r s t = last = NULL;
}
else
{
/* more than one node */
q1âˆ’>next = NULL;
last = q1;
}
free ( q ) ;
}
 Removing a node given by a key
NodeT *q, *q1;
q1 = NULL; /*Â initialize */
q = f i r s t ;
/* search node */
while ( q != NULL )
{
i f ( qâˆ’>key == givenKey ) break;
q1 = q;
q = qâˆ’>next ;
}
i f ( q != NULL )
{Â Â Â Â
/* found a node with supplied key */
i f ( q == f i r s t )
{
/* i s the f i r s t node */
f i r s t = f irst âˆ’>next ;
free ( q ) ;Â Â Â Â Â Â Â /* release memory */
i f ( f i r st == NULL )
last = NULL;
}
else
{
/* other than first node */
q1âˆ’>next = qâˆ’>next ;
i f ( q == last ) last = q1;
free ( q ) ;Â Â Â /* release memory */
}
}
Activity 5:
Complete deletion of single linked list
Solution:
For a complete deletion of a list, we have to remove each node of that list, i.e.
NodeTÂ *p;
while ( first != NULL )
{
p = f i r s t ;
f i r s t = f irst âˆ’>next ;
free ( p ) ;
}
last = NULL;
Home Activities:
 Write a function that prints all nodes of a linked list in the reverse
 Write a function which reverses the order of the linked
 Write a function which rearranges the linked list by group nodes having even numbered and odd numbered value in their data part.
 Write a function which takes two values as input from the user and searches them in the If both the values are found, your task is to swap both the nodes in which these values are found. Note, that you are not supposed to swap values.
Related linksÂ
Single link listÂ Â Â Â Â Â Â Â Â StackÂ Â Â Â Â Â Â Â AVL TreesÂ Â Â Â Â Â Â Binary searchÂ Â Â Â Â Â Counting Sort
Doubly link listÂ Â Â Â Â Â Â Â QueueÂ Â Â Â Â Â Â Â GraphsÂ Â Â Â Â Â Â Â Â Bubble SortÂ Â Â Â Â Â Â Â Radix sort
Circular link listÂ Â Â Â Â Â Â Â Binary search treeÂ Â Â Â HashingÂ Â Â Â Â Insertion SortÂ Â Â Â Â Bucket Sort
Josephus ProblemÂ Â Â Â Â Â Tree TraversalÂ Â Â Â Â Â Â Â HeapsÂ Â Â Â Â Â Â Â Â Quick SortÂ Â Â Â Â Â Â Merge Sort
At Cui tutorial, courses, past papers and final year projects
#tutorial #cui #pastpaper #courses
adx