Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Where is the mistake!!! #include using namespace std; struct node { int info; node *forw; node*back; }; class sll { private: node *head; public: sll(){head=new
Where is the mistake!!!
#include
using namespace std;
struct node
{
int info;
node *forw;
node*back;
};
class sll
{
private:
node *head;
public:
sll(){head=new node;
head->forw=head;
head->back=head;
}
void AddNode (int);
void Traverse();
void AddBefore(int,int);
};
void sll::AddNode (int val)
{
node *newNode= new node;
newNode->info=val;
newNode->forw=head;
newNode->back=head->back;
head->back->forw=newNode;
head->back=newNode;
}
void sll::Traverse()
{
if (head->forw==head)
cout<<" The list is empty";
for (node * curr=head->forw ;curr!=head; curr=curr->forw){
cout<info<<" , ";}
}
void sll::AddBefore(int k , int item)
{
node * curr=head->forw;
while(curr!=head){
if(curr->info==k){
break;
curr=curr->forw;}
}
if(curr==head)
cout<<"is empty";
else{
node*p=new node;
p->info=item;
p->back=curr->forw;
p->forw=curr;
curr->forw->back=p;
curr->back=p;
}
}
int main()
{
sll s;
int inf, ch ;
int k,item;
while(1)
{
cout<<" choice case";
cin>>ch;
switch(ch)
{
case 1:
cout<<" Put info/value to Add: ";
cin>>inf;
s.AddNode(inf);
break;
case 2:cout<<" Linked List Values: ";
s.Traverse();
break;
case 3:
cout<<"AddBefore:";
cin>>k>>item;
s.AddBefore( k ,item );
break;
default: exit(0);
}
}
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