السلام عليكم ورحمة الله وبركاته
كود single linked list
لكن ارجوا ممن يمر على الموضوع ان يفيدني ويساعدني في تحويل
نفس الكود الى Double linked list
وايظا تحويله الى Circut linked list
لانه صعب علي فهمها الله يعطيكم العافية
# include <iostream>
# include <cstdlib>
#include <time.h>
using namespace std;
int seed;
int node_No;
class node
{
public:
int No;
int info;
node *next;
node();
void setinfo(int y);
node *insert(node *h, node *c);
node *search(node *h, int x);
node *delete_node(node *head, int x);
void print(node *h);
void print_delete_node(node *c, int x);
};
node::node()
{
info=-1;
No=-1;
next=NULL;
}
void node::setinfo(int y)
{
this->info=y;
}
void node::print_delete_node(node *c1, int x)
{
cout<<"Deleted node is"<<endl;
cout<<" "<<c1->info<<" "<<"This No in node #"<<x<<endl;
cout<<"----------------------------------------"<<endl;
}
node *node::insert(node *h, node *c1)
{
node *temp=NULL;
temp=h;
if (temp==NULL)
{
node_No=1;
c1->No=node_No;
temp=h=c1;
return h;
}
if (c1->info<temp->info)
{
node_No++;
c1->No=node_No;
c1->next=temp;
h=c1;
return h;
}
while (temp->info<c1->info)
{
if (temp->next==NULL)
{
node_No++;
c1->No=node_No;
temp->next=c1;
return h;
} else if(temp->next->info <= c1->info)
temp=temp->next;
else
break;
}
if(temp->info==c1->info)
{
c1->print_delete_node(c1,temp->No);
delete(c1) ;
c1=NULL;
return h;
}
else
{
node_No++;
c1->No=node_No;
c1->next=temp->next;
temp->next=c1;
return h;
}
}
void node::print(node *h)
{
node *temp=NULL;
temp=h;
cout<<"Node No"<<" "<<"Node Info"<<endl;
cout<<"---------------------------------------"<<endl;
while (temp!=NULL)
{
cout<<" "<<temp->No<<" "<<temp->info<<endl;
temp=temp->next;
}
}
node *node::search(node *h, int x)
{
node *temp=NULL;
temp=h;
while (temp!=NULL)
{
if (temp->info!=x)
temp=temp->next;
else
break;
}
return temp;
}
node *node::delete_node(node *h, int x)
{
node *temp=NULL;
node *temp1=NULL;
temp=h;
if (temp->info==x)
{
h=h->next;
temp->next=NULL;
delete temp;
return h;
}
while(temp!=NULL)
{
if (temp->next->info!=x)
temp=temp->next;
else
break;
}
if(temp==NULL)
return temp;
else
{
temp1=temp->next;
temp->next=temp->next->next;
delete temp1;
}
return h;
}
int read()
{
// srand(seed);
return (rand()%150)+1;
}
int main()
{
node *head=NULL;
node *Tail=NULL;
node *c=NULL;
int x;
int val;
x=read();
seed=x;
c=new node();
c->setinfo(x);
head=head->insert(head,c);
while(node_No<10)
{
x=-1;
x=read();
c=new node();
c->setinfo(x);
head=head->insert(head,c);
}
c->print(head);
cin>>val;
c=c->search(head,val);
if (c==NULL)
cout<<" The Value is not found in the list"<<endl;
else
cout<<"the value is found in node NO."<<c->No<<endl;
cin>>val;
c=c->delete_node(head, val);
if (c==NULL)
cout<<" The Value is not found in the list"<<endl;
else
{
cout<<"the node has that info is deleted."<<endl;
head=c;
}
c->print(head);
system ("PAUSE");
return 0;
}