Data Structures and Algorithms
Lab 5
Lab Instructor: Haris Baig
Course Code: CC2042L
Session: Fall 2023
School of Systems and Technology
UMT Lahore Pakistan
Link List
What is Linked List:
A linked list is a linear data structure, in which the elements are not stored at con-
tiguous memory locations. The elements in a linked list are linked using pointers as
shown in the below image:
Linked-List-Data-Structure
In simple words, a linked list consists of nodes where each node contains a data
field and a reference(link) to the next node in the list.
Singly Link List
The linked list stores data in sequential storage, like arrays. Though the data are stored
sequentially, the memory locations are not contiguous.
Unlike an array, the linked list can store data of different data types.
The below diagram represents the linked-list structure.
In C++ the linked list can be represented with a class and a Node class separately,
which has two members, namely data and a next pointer which points to the next
node.
InsertNode: In this article, insertion is done at the end of the list . Follow the steps to
insert a node in the linked list.
Let’s say, 4 is to be inserted on the existing linked list, i.e., 1 -> 2 -> 3.The re-
sultant linked list will be 1 -> 2 -> 3 -> 4.
To insert a new node traverse till the end of the list until NULL node is found.
Create a new Node, and link the new node to the last node of the linked list.
DeleteNode: In this article, deletion is done using the index of the node. Follow the
steps to delete a node:
If the node to be deleted is the head node, store the head in temp variable.
Then update head as head->next. Delete temp.
If the index of the node to be deleted is greater than the length of the list
then return from the function.
Traverse till the node to be deleted. Delete the node, and link the previous
node to the next node of the deleted node.
Tasks:
You need to implement the concept of Singly Link List:
[1] Insertion Functions Includes:
Add At Head
Add At Tail
Add At Specific Position
[2] Deletion Function Includes:
Delete from Head
Delete from Tail
[3] Display the Singly Link List