0% found this document useful (0 votes)
12 views11 pages

Dsa Lab 5

The document contains a C++ implementation of a linked list with various operations such as traversal, searching, insertion, updating, deletion, and bubble sorting. It defines a Node class for individual elements and a LinkedList class to manage the linked list functionalities. The main function provides a menu-driven interface for users to interact with the linked list operations.

Uploaded by

shehzaibmirza428
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)
12 views11 pages

Dsa Lab 5

The document contains a C++ implementation of a linked list with various operations such as traversal, searching, insertion, updating, deletion, and bubble sorting. It defines a Node class for individual elements and a LinkedList class to manage the linked list functionalities. The main function provides a menu-driven interface for users to interact with the linked list operations.

Uploaded by

shehzaibmirza428
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
You are on page 1/ 11

SHEHZAIB

S2024332016

DSA LAB
TASK CODE:
#include <iostream>
using namespace std;
// Node class for the linked list
class Node
{
public:
int data; // Data of the node
Node* next; // Pointer to the next node
// Constructor to initialize node
Node(int value)
{
data = value;
next = nullptr;
}
};
// LinkedList class to manage linked list operations
class LinkedList
{
private:
Node* head; // Pointer to the head of the linked list

public:
// Constructor to initialize with default values
LinkedList() {

head = nullptr;
// Initialize with default values (1-10)
for (int i = 10; i >= 1; i--) {
Node* newNode = new Node(i);
newNode->next = head;
head = newNode;
}
}
// Function to traverse and display the linked list
void traverse() {
Node* temp = head;
if (temp == nullptr) {
cout << "List is empty" << endl;
return;
}
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
// Function to search for a specific value in the linked list
void search(int value)
{
Node* temp = head;

int position = 1;
bool found = false;
while (temp != nullptr) {
if (temp->data == value) {
cout << "Value found at position " << position << endl;
found = true;
break;
}
temp = temp->next;
position++;
}
if (!found) {
cout << "Value not found in the list" << endl;
}
}
// Function to insert a new value at the end of the linked list
void insert(int value) {
Node* newNode = new Node(value);
if (head == nullptr) {
head = newNode;
}
else
{
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}

temp->next = newNode;
}
cout << "Value inserted successfully" << endl;
}
// Function to update the value of a specific node
void update(int oldValue, int newValue) {
Node* temp = head;
bool updated = false;
while (temp != nullptr) {
if (temp->data == oldValue) {
temp->data = newValue; updated = true;
break;
}
temp = temp->next;
}
if (updated) {
cout << "Value updated successfully" << endl;
} else {
cout << "Value not found in the list" << endl;
}
}
// Function to delete a specific value from the linked list
void remove(int value) {

if (head == nullptr) {
cout << "List is empty" << endl;
return;
}
if (head->data == value) {
Node* temp = head;
head = head->next;
delete temp;
cout << "Value deleted successfully" << endl;
return;
}
Node* current = head;
Node* prev = nullptr;
while (current != nullptr && current->data != value) {
prev = current;
current = current->next;
}
if (current == nullptr) {
cout << "Value not found in the list" << endl;
return;
}
prev->next = current->next;
delete current;
cout << "Value deleted successfully" << endl;
}

// Function to bubble sort the linked list


void bubbleSort() {
if (head == nullptr || head->next == nullptr) {
return;
}
bool swapped;
Node* current;
Node* lastSorted = nullptr;
do {
swapped = false;
current = head;
while (current->next != lastSorted) {
if (current->data > current->next->data) {
int temp = current->data;
current->data = current->next->data;
current->next->data = temp;
swapped = true;
}
current = current->next;
}
lastSorted = current;
} while (swapped);
cout << "List sorted successfully" << endl;
}
};

int main() {
LinkedList list;
int choice, value, oldValue, newValue;

do {
cout << "\nOperations:\n";
cout << "1. Traverse\n";
cout << "2. Search\n";
cout << "3. Insert\n";
cout << "4. Update\n";
cout << "5. Delete\n";
cout << "6. Sort (Bubble Sort)\n";
cout << "7. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
list.traverse();

break;
case 2:
cout << "Enter value to search: ";
cin >> value;
list.search(value);
break;
case 3:

cout << "Enter value to insert: ";


cin >> value;
list.insert(value);
break;
case 4:
cout << "Enter old value to update: ";
cin >> oldValue;
cout << "Enter new value: ";
cin >> newValue;
list.update(oldValue, newValue);
break;
case 5:

cout << "Enter value to delete: ";


cin >> value;
list.remove(value);
break;
case 6:
list.bubbleSort();
break;
case 7:
cout << "Exiting program." << endl;
break;
default:
cout << "Invalid choice! Try again." << endl;
}
} while (choice != 7);

return 0;
}
OUTPUTS:

You might also like