السلآم عليكم ورحمه الله و بركآته
اولاً .. اشكر كل القآئمين على هذآ المنتدى الرائع ..
يعطيكم الف عآفيه و الله يجعله في ميزآن حسنآتكم ان شاء الله ..
ندخل في موضوعي :ah16:
الدكتور طلب مني اسوي فنكشن اسمهآ DeleteBefore
وظيفه هآذي الفنكشن انها تمسح النود اللي قبل النود اللي يحمل القيمه المدخله ..
يعني اذا كان عندي
8<-6<-4<-2<-
و انا دخلت 4 الرقم اللي بينمسح هو 2 يعني بيصير
8<-6<-4<-
واذا دخلت 2 يقولي انه مافي نود قبل عشان يمسحه !!
و إذا دخلت رقم مو موجود يقولي انه مو موجود ..
المهم هآذي محآولتي بس في شي غلط و عجزت اعرفه
مو طآلع عندي اخطآء بس الفنكشن ما تسوي وظيفتها صح !!!
كود:
#include <iostream>
using namespace std;
class Node
{
public:
int value;
Node * next;
Node();
Node(int);
};
Node::Node():value(0),next(NULL){}
Node::Node(int v):value(v),next(NULL){}
void Print(Node* h)
{
if(!h) cout<<"List is empty\n";
while(h)
{
cout<<" -> "<<h->value;
h=h->next;
}
cout<<endl;
}
void DeleteBefore (Node* h, int v)
{
if (h)
{
if (h->value !=v)
{
if (h->next)
{
Node *n=h;
Node *tmp=h->next;
if (tmp->value !=v)
{
while (tmp->next)
{
if (tmp->next->value==v)
{
n=tmp->next;
delete tmp;
}
else
{
tmp=tmp->next;
n->next=tmp;
}
}
}
else
{
h=h->next;
delete n;
}
}
else
{cout<<"There is no more nodes"<<endl;}
}else
{cout<<"There is no Node befor to delete !!"<<endl;}
}
else
{cout<<"List is empty !!"<<endl;}
}
int main()
{
Node * h = NULL;
cout<<"first manually create 3 nodes and link them together\n";
Node* one = new Node(1);
Node* two = new Node(2);
Node* three = new Node(3);
h=one;
one->next=two;
two->next=three;
Print(h);
cout<<"Delete the value before 2"<<endl;
DeleteBefore (h,2);
Print(h);
return 0;
}
*احتآجه ضروري ..
وشكراً مقدماً
المفضلات