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

FirstAssignment

The document contains a C++ program that implements a sorted linked list with functionalities to insert, search, remove, and display values. It defines a Node structure and a SortedList class, which manages the linked list operations. The main function allows user interaction to input values, search for a value, and delete a value from the list.

Uploaded by

mejbah.uddin.232
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

FirstAssignment

The document contains a C++ program that implements a sorted linked list with functionalities to insert, search, remove, and display values. It defines a Node structure and a SortedList class, which manages the linked list operations. The main function allows user interaction to input values, search for a value, and delete a value from the list.

Uploaded by

mejbah.uddin.232
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

First Assignment - C++ Source Code

#include <iostream>

using namespace std;

// Structure defining a Node in the linked list


struct Node {
double data; // Directly store the value instead of using a pointer
Node *nextNode; // Pointer to the next node in the list

// Constructor to initialize a new node with a given value


Node(double value) {
data = value; // Store the value directly
nextNode = nullptr; // Initialize next pointer to NULL
}
};

class SortedList {
private:
Node* head; // Pointer to the first node in the list

public:
// Constructor initializes the list as empty
SortedList() {
head = nullptr;
}

// Destructor to free all allocated memory when the list is destroyed


~SortedList() {
while (head) {
Node* temp = head;
head = head->nextNode;
delete temp;
}
}

// Function to insert a value into the sorted list


void insert(double value) {
Node* newNode = new Node(value);

// If the list is empty or new value should be placed at the beginning


if (!head || head->data >= value) {
newNode->nextNode = head;
head = newNode;
return;
}

// Traverse the list to find the correct position to insert


Node* current = head;
while (current->nextNode && current->nextNode->data < value) {
current = current->nextNode;
}

// Insert the new node in the correct position


newNode->nextNode = current->nextNode;
current->nextNode = newNode;
}

// Function to search for a value in the list


bool search(double value) {
Node* current = head;
while (current) {
if (current->data == value) {
return true;
}
current = current->nextNode;
}
return false;
}

// Function to remove a value from the list


void remove(double value) {
if (!head) return;

// If the value is at the head, remove the head node


if (head->data == value) {
Node* temp = head;
head = head->nextNode;
delete temp;
return;
}

// Traverse the list to find the node before the one to delete
Node* current = head;
while (current->nextNode && current->nextNode->data != value) {
current = current->nextNode;
}

// If the value is found, remove the node


if (current->nextNode) {
Node* temp = current->nextNode;
current->nextNode = current->nextNode->nextNode;
delete temp;
}
}

// Function to display all values in the list


void display() {
Node* current = head;
while (current) {
cout << current->data << " -> ";
current = current->nextNode;
}
cout << "NULL" << endl;
}
};

// Main function
int main() {
SortedList list;
int n;
double value;

cout << "Enter the number of elements: ";


cin >> n;
for (int i = 0; i < n; i++) {
cout << "Enter value: ";
cin >> value;
list.insert(value);
}

// Display sorted list


cout << "The sorted list is: ";
list.display();

// Search for a value


cout << "Enter value to search: ";
cin >> value;
cout << (list.search(value) ? "Found" : "Not Found") << endl;

// Delete a value from the list


cout << "Enter the value you want to delete: ";
cin >> value;
list.remove(value);

// Display list after deleting a value


cout << "Sorted list after deleting value: ";
list.display();

return 0;
}
Program Execution Output

You might also like