0% found this document useful (0 votes)
31 views10 pages

C++ Linked List Operations Lab Report

This document outlines a lab report for an experiment on singly and doubly linked lists in C++. It includes objectives, methods for performing operations on linked lists, and sample C++ code for implementing these operations. The report also contains assessment criteria for lab and report performance.

Uploaded by

shanzay.jamil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views10 pages

C++ Linked List Operations Lab Report

This document outlines a lab report for an experiment on singly and doubly linked lists in C++. It includes objectives, methods for performing operations on linked lists, and sample C++ code for implementing these operations. The report also contains assessment criteria for lab and report performance.

Uploaded by

shanzay.jamil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

AIR UNIVERSITY

Department Of Computer Engineering


Experiment No : 5

Lab Tittle : XXXXXX

Name: XXXXXXXXX Reg No:

Objectives: XXXXXXXX

LAB ASSESSMENT:

Excelle Goo Averag Satisfacto Unsatisfacto


Attributes
nt d e (3) ry (2) ry (1)
(5) (4)
Ability to Conduct
Experiment
Ability to assimilate
the results
Effective use of lab
equipment and
follows the lab safety
rules

Total Marks : Obtained Marks :

LAB REPORT ASSESSMENT:


Excellent Goo Averag Satisfacto Unsatisfacto
Attributes
(5) d e (3) ry (2) ry (1)
(4)

Data presentation

Experimental results

Conclusion

Total Marks: Obtained Marks:

Date: DD MM YY Signature:
Department of Computer Engineering

Data Structures and Algorithms

LAB REPORT: 4

Tittle: xxxxxx

Name: Write Your Own Name

Roll No: Write Your Own Roll. No

Submitted To: Engr. Shanzay Jamil

Date: DD MM YY
Experiment No: 5
Doubly Linked List

Introduction:
In this lab, you will learn about singly and doubly linked lists and how
they work in C++. A singly linked list is a dynamic data structure made up of
nodes, where each node stores data and a pointer to the next node. A doubly
linked list extends this idea by adding another pointer to the previous node,
allowing traversal in both directions. These lists are useful because they can
grow or shrink easily during program execution, unlike arrays that have a
fixed size.

Objectives:
• To understand how to create and manage singly and doubly linked lists
in C++.
• To perform basic operations on a singly linked list such as insertion,
deletion, and traversal.
• To learn the concept and structure of a doubly linked list and how it
allows two-way traversal
• To compare singly and doubly linked lists in terms of structure,
flexibility, and ease of manipulation.

1. Singly Linked List


Add Node at End
 Create a new node and set its next to null because it will be the last node.
 If the list is empty, make the new node the head.
 Otherwise, move through the list until you reach the last node.
 Link the last node’s next to the new node.

Add Node in Middle (After a Given Value)


 Start from the head and move through the list to find the given value.
 If the value is not found, show a message.
 If found, create a new node.
 Set the new node’s next to the next node of the found value.
 Link the found node’s next to the new node.

Delete Node at End


 If the list is empty, show a message.
 If there is only one node, delete it and make head = null.
 Otherwise, move through the list to reach the second last node.
 Delete the last node and set the second last node’s next to null.

Delete Node from Middle


 If the list is empty or has only one node, show a message.
 Use two pointers: move one (fast) two steps at a time and the other (slow) one
step at a time.
 Keep track of the node before slow using another pointer (prev).
 When fast reaches the end, slow will be at the middle node.
 Change prev->next to skip the middle node.
 Delete the middle node.

#include <iostream>
using namespace std;

class Node {
public:
int data;
Node* next;
Node(int val) {
data = val;
next = NULL;
}
};

class LinkedList {
private:
Node* head;
public:
LinkedList() {
head = NULL;
}

// Add node at end


void addAtEnd(int val) {
Node* newNode = new Node(val);
if (head == NULL) {
head = newNode;
return;
}
Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}

// Insert node in middle


void insertAtMiddle(int val) {
Node* newNode = new Node(val);
if (head == NULL) {
head = newNode;
return;
}
Node* slow = head;
Node* fast = head->next;
while (fast != NULL && fast->next != NULL) {
slow = slow->next;
fast = fast->next->next;
}
newNode->next = slow->next;
slow->next = newNode;
}

// Delete node from middle


void deleteFromMiddle() {
if (head == NULL || head->next == NULL) {
cout << "List too small to delete middle node." << endl;
return;
}
Node* slow = head;
Node* fast = head;
Node* prev = NULL;
while (fast != NULL && fast->next != NULL) {
fast = fast->next->next;
prev = slow;
slow = slow->next;
}
cout << "Deleted from middle: " << slow->data << endl;
prev->next = slow->next;
delete slow;
}

// Delete node from end


void deleteFromEnd() {
if (head == NULL) {
cout << "List is empty!" << endl;
return;
}
if (head->next == NULL) {
cout << "Deleted from end: " << head->data << endl;
delete head;
head = NULL;
return;
}
Node* temp = head;
while (temp->next->next != NULL) {
temp = temp->next;
}
cout << "Deleted from end: " << temp->next->data << endl;
delete temp->next;
temp->next = NULL;
}

// Display list
void traverse() {
Node* temp = head;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
};

// Main Function
int main() {
LinkedList list;
[Link](1);
[Link](2);
[Link](3);
[Link](4);
[Link](5);

cout << "Original List: ";


[Link]();

[Link](99);
cout << "After inserting 99 in middle: ";
[Link]();

[Link]();
cout << "After deleting from end: ";
[Link]();

[Link]();
cout << "After deleting from middle: ";
[Link]();

return 0;
}
Doubly Linked List

A doubly linked list allows efficient traversal in both directions since each
node contains pointers to both the previous and next nodes. This enables
quick insertion, deletion, and navigation through the list. It is more flexible
than a singly linked list, making data manipulation faster and easier in
complex operations.

Representation of Doubly Linked List in Data Structure


In a data structure, a doubly linked list is represented using nodes that have
three fields:
1. Data
2. A pointer to the next node (next)
3. A pointer to the previous node (prev)

1. Node Definition
#include <iostream>
using namespace std;

class Node {
public:
// To store the Value or data
int data;

// Pointer to point the Previous Element


Node* prev;

// Pointer to point the Next Element


Node* next;

// Constructor
Node(int d) {
data = d;
prev = nullptr;
next = nullptr;
}
};

Each node in a doubly linked list has some data, a pointer to the next node,
and a pointer to the previous node. These links let us move through the list
both forward and backward, which is the main feature of a doubly linked list.

Creating a Doubly Linked List with 4 Nodes

 Create the head node by making the first node and setting its prev and
next to null.
 Add the second node and link it to the head:
[Link] = new Node(value2)
[Link] = head
 Add more nodes the same way, for example:
[Link] = new Node(value3)
[Link] = [Link]
 Continue until all 4 nodes are created.
 Set the last node’s next to null to mark the end of the list.
 Keep track of the head (and optionally tail) to make traversal and adding
new nodes easier.

#include <iostream>
using namespace std;

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

Node(int value) {
data = value;
prev = nullptr;
next = nullptr;
}
};

int main() {
// Create the first node (head of the list)
Node* head = new Node(10);

// Create and link the second node


head->next = new Node(20);
head->next->prev = head;

// Create and link the third node


head->next->next = new Node(30);
head->next->next->prev = head->next;

// Create and link the fourth node


head->next->next->next = new Node(40);
head->next->next->next->prev = head->next->next;

// Traverse the list forward and print elements


Node* temp = head;
while (temp != nullptr) {
cout << temp->data;
if (temp->next != nullptr) {
cout << " <-> ";
}
temp = temp->next;
}

return 0;
}
Output:

Task 1: Insertion and Deletion in Singly Linked List


Write a C++ program to perform both insertion and deletion operations in a
singly linked list.
 Start with an existing list (for example: 1 → 2 → 3 → 4 → 5).
 Insert a new node with value 6 in the middle of the list.
 Then, delete the middle node (any automatically found middle node, not by
value).
 Display the list before insertion, after insertion, and after deletion.

Task 2: Create a Doubly Linked List with 3 Nodes


Write a C++ program to create a doubly linked list with three nodes.
 The nodes should contain the values 10, 20, and 30.
 Properly connect all nodes using next and prev pointers.
 Display the list in both forward and backward directions.

Task 3: Insert and Delete Node at End in Doubly Linked List


Write a C++ program to insert and delete a node at the end of a doubly linked

 Start with a doubly linked list having nodes (10 ⇄ 20 ⇄ 30).


list.

 Insert a new node with value 40 at the end.


 Display the list after insertion.

You might also like