Question
Circular linked list in C++ The following program stores a list of integer numbers dynamically through a circular header linked list, complete it by defining
Circular linked list in C++
The following program stores a list of integer numbers dynamically through a circular header linked list, complete it by defining the following functions:
1- void add (int item);
This function inserts a new node after the first node (when the list is empty, the inserted node will be the first node).
2- void change (int value);
This function used to change the value of the last node to value.
#include
using namespace std;
struct node
{ int info;
node *next;
};
class clist
{
private:
node *head;
public:
clist(){head=new node; head->next=head;}
void traverse()
{
if(head->next==head)
cout
else
{
node*curr=head->next;
while(curr!=head)
{
coutinfo
curr=curr->next;
}
cout
}
}
void add(int item)
{
}
void change (int value)
{
}
};
(add function):creat node and put the item inside it ,then ask if the list empyt but the node in the first if it is not empty but it aftear the first node ?
(change function): we will use pointer until we came to the last node then we will said this node the info for it number the user will enter and if thefunction empty print massage that is empty.
-----------------------------------------------------------------------------------------------------------
int main()
{
clist s;
s.add(4);
cout
s.traverse();
s.add(3);
cout
s.traverse();
s.add(6);
cout
s.traverse();
s.change(8);
cout
s.traverse();
return 0;
}
The Output will be as the following:
The list after adding the first value ' 4 ' : 4 The list after adding the second value ' 3 ': 43 The list after adding the third value ' 6 ' : 463 The list after calling 'change' function with parameter ' 8 : 468 Program ended with exit code
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