Lab 03 Linked Lists
Lab 03 Linked Lists
Session 3
Course: Data Structures (CL2001) Semester: Fall 2024
Instructor: Zainab Asif Jawed T.A:
Note:
● Maintain discipline during the lab.
● Listen and follow the instructions as they are given.
● Just raise hand if you have any problem.
● Completing all tasks of each lab is compulsory.
● Get your lab checked at the end of the session.
Linked List:
Like arrays, Linked List is a linear data structure. Unlike arrays, linked list elements are not stored at a
contiguous location; the elements are linked using pointers.
A singly linked list is indeed a type of linked list where each element (node) consists of two parts:
● Data: This part of the node holds the actual value or data that you want to store in the list. It can be any
data type, such as integers, characters, or custom data structures.
● Pointer to the Next Node: This part of the node contains a reference or pointer to the next node in the
list. This pointer helps maintain the structure of the linked list and allows you to traverse the list in a
unidirectional manner, usually from the head (the first node) to the last node. The last node typically
points to NULL to indicate the end of the list.
Creating a Linked List: Node and Singly Class
Adding a node to a linked list involves inserting an element, which consists of data and a reference to the next
node, either at the front (beginning), the end (tail), or at any specified position within the list.
InsertionAtStart: Append
InsertionAtTail: Append
1. Create a new node with some value
2. Check if there is no node (head is null), then make this new node head node.
3. Else
a. assign head node to a pointer (Node* current = head)
b. loop until current->next != NULL
c. at the end of the loop (current points to last Node) assign current->next = new node
d. point new node’s next to NULL since this is the last node (inserted at tail).
Performing a deletion operation on a linked list involves removing an element, either from the front
(beginning), the end (tail), or from any specified position within the list, effectively altering the list's structure
DeletionFromEnd
DeletionFromStart
Append: Add a new node to the end of the circular linked list.
Insert: Add a new node at a specific position in the circular linked list.
Delete: Remove a node with a given value from the circular linked list.
Search: Find a node with a specific value in the circular linked list.
Display: Print the elements of the circular linked list.
Reverse: Reverse the order of nodes in the circular linked list.
Creation of Circular Linked List
class Node {
public:
int data;
Node* next;
Node* prev;
if (tail == nullptr) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
}
}
};
Lab Exercises:
3. Insert some elements in Singly Linked List and write a function to segregate even and
odd numbers such that all even numbers precede odd numbers.
5. Write a program to sort a doubly linked list and remove duplicates from the sorted list.