0% found this document useful (0 votes)
2 views

Lab 03 Linked Lists

The document outlines the structure and operations of various types of linked lists, including singly linked lists, circular linked lists, and doubly linked lists. It provides instructions for lab exercises related to creating and manipulating these data structures, such as inserting and deleting nodes, traversing the list, and sorting. Additionally, it emphasizes the importance of maintaining discipline and completing all tasks during the lab session.

Uploaded by

alishba.subhani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab 03 Linked Lists

The document outlines the structure and operations of various types of linked lists, including singly linked lists, circular linked lists, and doubly linked lists. It provides instructions for lab exercises related to creating and manipulating these data structures, such as inserting and deleting nodes, traversing the list, and sorting. Additionally, it emphasizes the importance of maintaining discipline and completing all tasks during the lab session.

Uploaded by

alishba.subhani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Data Structures Lab

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 Node to Linked List(Front,End and anywhere)

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).

InsertAfter/Insert at any Position


Displaying the Linked List/Traversal

Removal of Nodes: Deletion

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

Circular Linked List and Helper Functions :


A Circular Linked List is a data structure in which elements, known as nodes, are connected in a circular
fashion. Unlike a regular singly linked list, where the last node points to null, in a circular linked list, the last
node points back to the first node, creating a loop.
Here's a list of common helper functions that are often implemented in a Circular Linked List:

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

Doubly Linked List:


A Doubly Linked List is a linear data structure similar to a singly linked list, but with an important difference:
each node in a doubly linked list contains two pointers instead of one. It has a pointer to the next node as well as
a pointer to the previous node. This bidirectional connectivity allows traversal in both forward and backward
directions.
Creation of Doubly Linked List

class Node {
public:
int data;
Node* next;
Node* prev;

Node() : data(0), next(NULL), prev(NULL) {}

Node(int val) : data(val), next(NULL), prev(NULL) {}


};
class DoublyLinkedList {
public:
Node* head;
Node* tail;

DoublyLinkedList() : head(NULL), tail(NULL) {}

void insertAtEnd(int val) {


Node* newNode = new Node(val);

if (tail == nullptr) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
}
}
};
Lab Exercises:

1. Create a singly linked list and write functions to


 Insert node at front
 Insert node at end
 Insert node before and after a given value
 Delete node from head
 Delete last node
 Delete node of a particular value
 Count number of nodes

2. Write a function to reverse the singly linked list.

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.

4. Create a doubly linked list and write functions to


 Insert node at front
 Insert node at end
 Insert node before and after a given value
 Delete node from head
 Delete last node
 Delete node of a particular value

5. Write a program to sort a doubly linked list and remove duplicates from the sorted list.

You might also like