Answered step by step
Verified Expert Solution
Link Copied!

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advanced Database Systems For Integration Of Media And User Environments 98

Authors: Yahiko Kambayashi, Akifumi Makinouchi, Shunsuke Uemura, Katsumi Tanaka, Yoshifumi Masunaga

1st Edition

9810234368, 978-9810234362

More Books

Students also viewed these Databases questions

Question

=+How might you approach the topic differently?

Answered: 1 week ago