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
List :
Length List :
Enter the number to be searched:
found in the list
Enter the number to be deleted:
After deleting the node, List :
Length List :
Your program should use the value 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;
Nodeint val : dataval nextnullptr 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 : headnullptr length Constructor for initializing the circular linked list.
Function to insert a number into the circular linked list.
void insertint val
Node newNode new Nodeval; Create a new node with the given value.
if head
head newNode; If the list is empty, set the new node as the head.
newNodenext head; Make the new node point to itself to create a circular structure.
else
Node temp head;
while tempnext head
temp tempnext;
tempnext newNode; Insert the new node at the end of the list.
newNodenext 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 removeint val
if head
cout "The item to be deleted is not in the list." endl;
return;
Node current head;
Node prev nullptr;
do
if currentdata val
if current head
head headnext; If the head is the node to be deleted, update the head.
prevnext currentnext; 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 currentnext;
while current head;
cout val not in the list." endl;
Function to check if a number is contained in the circular linked list.
bool containsint val
Node current head;
if head
return false;
do
if currentdata val
return true;
current currentnext;
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 currentdata ;
current currentnext;
while current head;
cout endl;
Function to get the length of the circular linked list.
int getLength
return length;
;
int main
circularLinkedList list;
cout "Enter numbers ending with endl;
int num;
cin num;
while num
listinsertnum;
cin num;
listdisplay;
cout "Length List : listgetLength endl;
cout "Enter the number to be searched: ;
cin num;
if listcontainsnum
cout num found in the list" endl;
else
cout num not in the list" endl;
cout "Enter the number to be deleted: ;
cin num;
listremovenum;
listdisplay;
cout "Length List : listgetLength endl;
return ;
Status: FAILED!
Check:
Test: Input output test
Reason: Unable to find ssssssnot 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
