Question: Please write code in C++: Using the code given in class, add the following features: Basic operations on linked lists: Add items to the list

Please write code in C++:

Using the code given in class, add the following features:

  • Basic operations on linked lists:
    • Add items to the list
    • Print the list
    • Determine whether the list is empty
    • Find the length of the list
    • Destroy the list
    • Retrieve info contained in the first or last node
    • Search the list for a given item
    • Insert an item in the list
    • Delete an item from the list
    • Make a copy of the linked list

Code given:

#include

#include

using namespace std;

struct nodeType

{

int info;

nodeType *link;

};

//Write an application that creates a linked list and prints it out

int main()

{

nodeType *head=nullptr;

nodeType *current = nullptr;

nodeType *newNode = nullptr;

int value; //obtain value from user

cout << "Enter an integer (-1 to stop): ";

cin >> value;

while (value != -1)

{

// add data to the list

newNode = new nodeType;

newNode->info = value;

newNode->link = nullptr;

if (head == nullptr)

{

head = newNode;

}

else

{

current = head;

while (current->link !=nullptr)

{

current = current->link;

}

current->link = newNode;

}

cout << "Enter an integer (-1 to stop): ";

cin >> value;

}

//print out the list

cout << "The list is " << endl;

current = head;

while(current != nullptr)

{

cout << current->info << endl;

current= current->link;

}

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!