Answered step by step
Verified Expert Solution
Question
1 Approved Answer
what is wrong ? I want to write a function addbefore #include using namespace std; struct node { int info; node *next; }; class sll
what is wrong ? I want to write a function addbefore #include using namespace std; struct node { int info; node *next; }; class sll { private: node *head; public: sll(){head=NULL;} void AddNode (int); void Traverse(); void AddBefore(int,int); }; void sll::AddNode (int val) { node *newNode= new node; newNode->info=val; newNode->next=head; head=newNode; } void sll::Traverse() { if (head==NULL) cout for (node * curr=head;curr; curr=curr->next) coutinfo } void sll::AddBefore(int k , int x) { node*per=head; node * curr=head->next; while(curr!=head){ if(curr->info==k){ break; per=curr; curr=curr->next;} } if(curr==head) cout else{ node*p=new node; p->info=x; p->next=curr; per->next=p; } } int main() { sll s; int inf, ch ; int k,x; while(1) { cout cin>>ch; switch(ch) { case 1: cout cin>>inf; s.AddNode(inf); break; case 2:cout s.Traverse(); break; case 3: cout cin>>k,x; cout break; default: exit(0); } } return 0; } Create a program to store a list of integer numbers dynamically through a Circular Header linked list, your program should contain the following functions: 1- AddNode (int): Adds a new node at the end of the list. 2- Traverse0: Prints the values stored in the list. 3- void addBefore(int, int): Inserts a new node before a given value
what is wrong ? I want to write a function addbefore
#include
using namespace std;
struct node
{
int info;
node *next;
};
class sll
{
private:
node *head;
public:
sll(){head=NULL;}
void AddNode (int);
void Traverse();
void AddBefore(int,int);
};
void sll::AddNode (int val)
{
node *newNode= new node;
newNode->info=val;
newNode->next=head;
head=newNode;
}
void sll::Traverse()
{
if (head==NULL)
cout
for (node * curr=head;curr; curr=curr->next)
coutinfo
}
void sll::AddBefore(int k , int x)
{
node*per=head;
node * curr=head->next;
while(curr!=head){
if(curr->info==k){
break;
per=curr;
curr=curr->next;}
}
if(curr==head)
cout
else{
node*p=new node;
p->info=x;
p->next=curr;
per->next=p;
}
}
int main()
{
sll s;
int inf, ch ;
int k,x;
while(1)
{
cout
cin>>ch;
switch(ch)
{
case 1:
cout
cin>>inf;
s.AddNode(inf);
break;
case 2:cout
s.Traverse();
break;
case 3:
cout
Create a program to store a list of integer numbers dynamically through a Circular Header linked list, your program should contain the following functions: 1- AddNode (int): Adds a new node at the end of the list. 2- Traverse0: Prints the values stored in the list. 3- void addBefore(int, int): Inserts a new node before a given valuecin>>k,x;
cout
break;
default: exit(0);
}
}
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered 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