0% found this document useful (0 votes)
14 views14 pages

LINK LIST ALL OPERATIONS LAB TASK 6

The document contains code for a LinkedList implementation in C++, featuring methods for inserting, deleting, and displaying nodes, as well as sorting the list in descending order. It includes error handling for operations when the list is full or empty. The main function demonstrates the usage of these methods through various operations on the LinkedList.

Uploaded by

ranahyderhyder
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)
14 views14 pages

LINK LIST ALL OPERATIONS LAB TASK 6

The document contains code for a LinkedList implementation in C++, featuring methods for inserting, deleting, and displaying nodes, as well as sorting the list in descending order. It includes error handling for operations when the list is full or empty. The main function demonstrates the usage of these methods through various operations on the LinkedList.

Uploaded by

ranahyderhyder
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/ 14

Name: SHAIR ZAMAN

Section: 2312134
Course: DSA LAB
LAB TASK-3
(Q1)
Code:
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
};
class LinkedList {
private:
Node* head;
int count;
public:
LinkedList() : head(nullptr), count(0) {}
void insertFront(int val) {
if (count >= 10) {
cout << "Error: List is full.\n";
return;
}
Node* newNode = new Node();
newNode->data = val;
newNode->next = head;
head = newNode;
count++;
}
void insertEnd(int val) {
if (count >= 10) {
cout << "Error: List is full.\n";
return;
}
Node* newNode = new Node();
newNode->data = val;
newNode->next = nullptr;
if (head == nullptr) {
head = newNode;
} else {
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
}
count++;
}
void insertAfter(int after, int val) {
if (count >= 10) {
cout << "Error: List is full.\n";
return;
}
Node* temp = head;
while (temp != nullptr && temp->data != after) {
temp = temp->next;
}
if (temp == nullptr) {
cout << "Number " << after << " not found.\n";
} else {
Node* newNode = new Node();
newNode->data = val;
newNode->next = temp->next;
temp->next = newNode;
count++;
}
}
void deleteFront() {
if (head == nullptr) {
cout << "List is empty.\n";
return;
}
Node* temp = head;
head = head->next;
delete temp;
count--;
}
void deleteEnd() {
if (head == nullptr) {
cout << "List is empty.\n";
return;
}
if (head->next == nullptr) {
delete head;
head = nullptr;
} else {
Node* temp = head;
while (temp->next->next != nullptr) {
temp = temp->next;
}
delete temp->next;
temp->next = nullptr;
}
count--;
}
void deleteValue(int val) {
if (head == nullptr) {
cout << "List is empty.\n";
return;
}
if (head->data == val) {
Node* temp = head;
head = head->next;
delete temp;
count--;
return;
}
Node* temp = head;
while (temp->next != nullptr && temp->next->data != val) {
temp = temp->next;
}
if (temp->next == nullptr) {
cout << "Number " << val << " not found.\n";
} else {
Node* nodeToDelete = temp->next;
temp->next = nodeToDelete->next;
delete nodeToDelete;
count--;
}
}

void display() {
if (head == nullptr) {
cout << "List is empty.\n";
return;
}
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
};
int main() {
LinkedList list;

list.insertFront(10);
list.insertEnd(20);
list.insertEnd(30);
list.insertAfter(20, 25);
list.display();
list.deleteFront();
list.display();

list.deleteEnd();
list.display();

list.deleteValue(25);
list.display();

return 0;
}
Output:

(Q2)
Code:
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
};
class LinkedList {
private:
Node* head;
int count;
public:
LinkedList() : head(nullptr), count(0) {}

void insertFront(int val) {


if (count >= 10) {
cout << "Error: List is full.\n";
return;
}
Node* newNode = new Node();
newNode->data = val;
newNode->next = head;
head = newNode;
count++;
}
void insertEnd(int val) {
if (count >= 10) {
cout << "Error: List is full.\n";
return;
}
Node* newNode = new Node();
newNode->data = val;
newNode->next = nullptr;
if (head == nullptr) {
head = newNode;
} else {
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
}
count++;
}
void insertAfter(int after, int val) {
if (count >= 10) {
cout << "Error: List is full.\n";
return;
}
Node* temp = head;
while (temp != nullptr && temp->data != after) {
temp = temp->next;
}
if (temp == nullptr) {
cout << "Number " << after << " not found.\n";
} else {
Node* newNode = new Node();
newNode->data = val;
newNode->next = temp->next;
temp->next = newNode;
count++;
}
}
void deleteFront() {
if (head == nullptr) {
cout << "List is empty.\n";
return;
}
Node* temp = head;
head = head->next;
delete temp;
count--;
}
void deleteEnd() {
if (head == nullptr) {
cout << "List is empty.\n";
return;
}
if (head->next == nullptr) {
delete head;
head = nullptr;
} else {
Node* temp = head;
while (temp->next->next != nullptr) {
temp = temp->next;
}
delete temp->next;
temp->next = nullptr;
}
count--;
}
void deleteValue(int val) {
if (head == nullptr) {
cout << "List is empty.\n";
return;
}
if (head->data == val) {
Node* temp = head;
head = head->next;
delete temp;
count--;
return;
}
Node* temp = head;
while (temp->next != nullptr && temp->next->data != val) {
temp = temp->next;
}
if (temp->next == nullptr) {
cout << "Number " << val << " not found.\n";
} else {
Node* nodeToDelete = temp->next;
temp->next = nodeToDelete->next;
delete nodeToDelete;
count--;
}
}
void display() {
if (head == nullptr) {
cout << "List is empty.\n";
return;
}
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
int countNodes() {
return count;
}

void sortDescending() {
if (head == nullptr) return;

Node* temp1 = head;


while (temp1 != nullptr) {
Node* temp2 = temp1->next;
while (temp2 != nullptr) {
if (temp1->data < temp2->data) {
int tempData = temp1->data;
temp1->data = temp2->data;
temp2->data = tempData;
}
temp2 = temp2->next;
}
temp1 = temp1->next;
}
}
};
int main() {
LinkedList list;

list.insertFront(50);
list.insertEnd(40);
list.insertEnd(60);
list.insertEnd(20);
list.display();
cout << "Node count: " << list.countNodes() << endl;
list.sortDescending();
list.display();
return 0;
}
Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy