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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started