Question: C + + help: I am having a problem with the output. Instructions This chapter defined and identified various operations on a circular linked list.

C++ help: I am having a problem with the output.
Instructions
This chapter defined and identified various operations on a circular linked list.
Write the definitions of the class circularLinkedList and its member functions. (You may assume that the elements of the circular linked list are in ascending order.)
Write a program to test various operations of the class defined in the step above. Your program should accept a sorted list as input and output the following:
The length of the list.
The list after deleting a number.
A message indicating if a number is contained in the list.
An example of the program is shown below:
Enter number ending with -999
1
2
3
4
5
6
7
8
9
10
-999
List 1: 12345678910
Length List 1: 10
Enter the number to be searched: 4
4 found in the list
Enter the number to be deleted: 4
After deleting the node, List 1: 1235678910
Length List 1: 9
Your program should use the value -999 to denote the end of the input list.
main.cpp
#include
using namespace std;
// Define a node structure for the circular linked list.
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr){}// Constructor for the Node structure.
};
class circularLinkedList {
private:
Node* head; // Pointer to the head of the circular linked list.
int length; // Variable to store the length of the list.
public:
circularLinkedList() : head(nullptr), length(0){}// Constructor for initializing the circular linked list.
// Function to insert a number into the circular linked list.
void insert(int val){
Node* newNode = new Node(val); // Create a new node with the given value.
if (!head){
head = newNode; // If the list is empty, set the new node as the head.
newNode->next = head; // Make the new node point to itself to create a circular structure.
} else {
Node* temp = head;
while (temp->next != head){
temp = temp->next;
}
temp->next = newNode; // Insert the new node at the end of the list.
newNode->next = head; // Make the new node circular by pointing to the head.
}
length++; // Increment the length of the list.
}
// Function to delete a number from the circular linked list.
void remove(int val){
if (!head){
cout << "The item to be deleted is not in the list." << endl;
return;
}
Node* current = head;
Node* prev = nullptr;
do {
if (current->data == val){
if (current == head){
head = head->next; // If the head is the node to be deleted, update the head.
}
prev->next = current->next; // Remove the current node from the list.
delete current; // Deallocate memory for the deleted node.
length--; // Decrement the length of the list.
return;
}
prev = current;
current = current->next;
} while (current != head);
cout << val <<" not in the list." << endl;
}
// Function to check if a number is contained in the circular linked list.
bool contains(int val){
Node* current = head;
if (!head){
return false;
}
do {
if (current->data == val){
return true;
}
current = current->next;
} while (current != head);
return false;
}
// Function to display the circular linked list.
void display(){
if (!head){
cout << "List is empty." << endl;
return;
}
Node* current = head;
cout << "List: ";
do {
cout << current->data <<"";
current = current->next;
} while (current != head);
cout << endl;
}
// Function to get the length of the circular linked list.
int getLength(){
return length;
}
};
int main(){
circularLinkedList list1;
cout << "Enter numbers ending with -999"<< endl;
int num;
cin >> num;
while (num !=-999){
list1.insert(num);
cin >> num;
}
list1.display();
cout << "Length List 1: "<< list1.getLength()<< endl;
cout << "Enter the number to be searched: ";
cin >> num;
if (list1.contains(num)){
cout << num <<" found in the list" << endl;
} else {
cout << num <<" not in the list" << endl;
}
cout << "Enter the number to be deleted: ";
cin >> num;
list1.remove(num);
list1.display();
cout << "Length List 1: "<< list1.getLength()<< endl;
return 0;
}
Status: FAILED!
Check: 2
Test: Input / output test 2
Reason: Unable to find '['123\\s+321\\s+456\\s+654\\s+789\\s+987','6','(1234)+\\s+(not in the list)', 'The item to be deleted is not in the list']' in the program's output.

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!