In: Computer Science
C++ question:
Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member functions for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found.
In addition, it should have member functions to display the list, check if the list is empty, and return the length of the list. Be sure to have a class constructor, a class destructor, and a copy constructor for deep copy. Demonstrate your class with a driver program (be sure to include the following cases: insertion at the beginning, end, and inside the list, deletion of first item, last item, and an item inside, searching for an existing/non-existing item, and modifying a list that was initialized to an existing list).
Screenshot
------------------------------------------------------------------------------------------
Program
LinkedList.h
#include<iostream>
#include<string>
#include<cstdlib>
// Create a class linked list
class LinkedList {
//Instance variable part
private:
typedef struct node {
int data;
node* next;
}*nodePtr;
nodePtr head;
nodePtr curr;
nodePtr temp;
//Member functions
public:
//Constructor
LinkedList();
//Destructor
~LinkedList()=default;
//Copy constructor
LinkedList(const LinkedList& copy);
//Add a value
void addNode(int addData);
//Print list
void printList();
//Delete a value using position
void deleteNode(int position);
//Search a value
int searchNode(int data);
};
LinkedList.cpp
#include "LinkedList.h"
using namespace std;
//Constructor
LinkedList::LinkedList() {
head = NULL;
temp = NULL;
curr = NULL;
}
//Copy constructor
LinkedList::LinkedList(const LinkedList& copy) {
curr = copy.head;
while (curr != NULL)
{
addNode(curr->data);
curr = curr->next;
}
}
//Insert an item in linked list
void LinkedList::addNode(int addData) {
//Create a node
nodePtr n = new node;
n->data = addData;
n->next = NULL;
//Check any element present or not
if (head != NULL) {
//head value check
if (head->data > n->data)
{
n->next =
head;
head = n;
return;
}
curr = head;
temp = head;
while (curr->next!= NULL
&& curr->data<n->data) {
temp =
curr;
curr =
curr->next;
}
if (curr->next!= NULL) {
temp->next =
n;
n->next =
curr;
}
else if (curr->data >
n->data) {
temp->next =
n;
n->next =
curr;
}
else {
curr->next =
n;
}
}
//First data
else {
head = n;
}
}
//Print the linked list
void LinkedList::printList() {
curr = head;
while (curr != NULL) {
cout << curr->data
<< " ";
curr = curr->next;
}
cout << endl;
}
//Delete value at the specified index
void LinkedList::deleteNode(int position) {
int counter = 0;
nodePtr delPtr = NULL;
temp = head;
curr = head;
while (curr != NULL && counter != position)
{
temp = curr;
curr = curr->next;
counter++;
}
if (curr == NULL) {
cout << "Data not found!!!"
<< endl;
delete delPtr;
}
else {
delPtr = curr;
curr = curr->next;
temp->next = curr;
if (delPtr == head) {
head =
head->next;
temp =
NULL;
}
delete delPtr;
}
}
//Search an item
int LinkedList::searchNode(int data) {
int i = 0;
curr = head;
while (curr != NULL) {
if (curr->data == data) {
return i;
}
curr = curr->next;
i++;
}
return -1;
}
Driver.cpp
#include "LinkedList.h"
using namespace std;
int main()
{
//Create object of linke list class
LinkedList ll;
//Add values
//Check first last and middle
ll.addNode(3);
ll.addNode(5);
ll.addNode(4);
ll.addNode(7);
ll.addNode(2);
//Print list
ll.printList();
//Delete first
ll.deleteNode(0);
ll.printList();
//Delete last
ll.deleteNode(3);
ll.printList();
//Delete middle
ll.deleteNode(1);
ll.printList();
//Search
cout << "Search 3 in list and found in index of
"<<ll.searchNode(3) << endl;
cout << "Search 7 in list and found in index of
" << ll.searchNode(7) << endl;
//Copy constructor check
LinkedList copy;
copy.addNode(3);
copy.addNode(5);
copy.addNode(4);
copy.addNode(7);
copy.addNode(2);
ll = copy;
ll.printList();
}
----------------------------------------------------------------------
Output
2 3 4 5 7
3 4 5 7
3 4 5
3 5
Search 3 in list and found in index of 0
Search 7 in list and found in index of -1
2 3 4 5 7