السلام عليكم ورحمة الله وبركاته
اذا احد يقدر يساعدني في هدا السؤال اني بحط محاولتي واتمنى المساعدة
المطلوب حل السؤال با Recursion
اذا احد يقدر يساعدني في هدا السؤال اني بحط محاولتي واتمنى المساعدة
المطلوب حل السؤال با Recursion
Write a function 'CheckSmaller' that takes two linked list as input arguments. These linked list contain numbers like this:
num1->3->5->2->NULL (assuming that num1 is pointing to number 352)
num2->4->3->9->1->NULL (assuming that num2 is pointing to number 4391)
The function CheckSmaller should return 1 if num1 points to a linked list which represents a smaller number than the number pointed to by num2 linked list. Otherwise, it returns -1. If both linked list point to exactly the same number, CheckSmaller returns a 0.
int CheckSmaller(Node* num1, Node* num2);
Notice that if two linked lists are:
num1->8->4->2->NULL (assuming that number 1 is 842) and
num2->8->4->3->NULL (assuming that number 2 is 843)
then your function should return 1 since 842 is smaller than 843.
You must use Recursion for this function
هدي محاولتي اني بالصراحة ما عرفت بالRecurstion
الا يعرف يساعدني
#include <iostream>
using namespace std;
class node
{
public:
int num1;
int num2;
int value;
node*next;
node();
node(int n1, int n2 , int v);
};
node::node()
{
num1=0;
num2=0;
value=0;
next=NULL;
}
node::node(int n1,int n2 , int v)
{
num1=n1;
num2=n2;
value=v;
next=NULL;
}
void Print(node *s)
{
if (!s)
cout<<"Empty list!!"<<endl;
while (s)
{
cout<<"Frist number:"<<s->num1<<endl;
s=s->next;
}
}
void Print2(node *h)
{
if (!h)
cout<<"Empty list!!"<<endl;
while (h)
{
cout<<"second number:"<<h->num2<<endl;
h=h->next;
}
}
int Length(node* head)
{
int i = 0;
while (head)
{
i++;
head = head->next;
}
return i;
}
int CheckSmaller(node* h1, node* h2)
{
if (Length(h1)<Length(h2))
{ return -1;}
if (Length(h1)==Length(h2))
{
while(h1&&h2)
{
h1=h1->next;
h2=h2->next;
if (h1)
{ return 1;}
else
{return -1;}
}
}
}
int main()
{
node * tmp = new node;
tmp->value=tmp->num1;
tmp->next=NULL;
node*h=new node;
h->value=h->num2;
h->next=NULL;
cout<<"Enter first number\n";
cin>>tmp->num1;
cout<<endl;
Print(tmp);
cout<<"Enter second number\n";
cin>>h->num2;
cout<<endl;
Print2(h);
cout<<endl;
if (CheckSmaller(tmp,h))
{cout<<"1";}
else {cout<<"-1";}
cout<<endl;
return 0;
}
شــــكرا مقدما
num1->3->5->2->NULL (assuming that num1 is pointing to number 352)
num2->4->3->9->1->NULL (assuming that num2 is pointing to number 4391)
The function CheckSmaller should return 1 if num1 points to a linked list which represents a smaller number than the number pointed to by num2 linked list. Otherwise, it returns -1. If both linked list point to exactly the same number, CheckSmaller returns a 0.
int CheckSmaller(Node* num1, Node* num2);
Notice that if two linked lists are:
num1->8->4->2->NULL (assuming that number 1 is 842) and
num2->8->4->3->NULL (assuming that number 2 is 843)
then your function should return 1 since 842 is smaller than 843.
You must use Recursion for this function
هدي محاولتي اني بالصراحة ما عرفت بالRecurstion
الا يعرف يساعدني
#include <iostream>
using namespace std;
class node
{
public:
int num1;
int num2;
int value;
node*next;
node();
node(int n1, int n2 , int v);
};
node::node()
{
num1=0;
num2=0;
value=0;
next=NULL;
}
node::node(int n1,int n2 , int v)
{
num1=n1;
num2=n2;
value=v;
next=NULL;
}
void Print(node *s)
{
if (!s)
cout<<"Empty list!!"<<endl;
while (s)
{
cout<<"Frist number:"<<s->num1<<endl;
s=s->next;
}
}
void Print2(node *h)
{
if (!h)
cout<<"Empty list!!"<<endl;
while (h)
{
cout<<"second number:"<<h->num2<<endl;
h=h->next;
}
}
int Length(node* head)
{
int i = 0;
while (head)
{
i++;
head = head->next;
}
return i;
}
int CheckSmaller(node* h1, node* h2)
{
if (Length(h1)<Length(h2))
{ return -1;}
if (Length(h1)==Length(h2))
{
while(h1&&h2)
{
h1=h1->next;
h2=h2->next;
if (h1)
{ return 1;}
else
{return -1;}
}
}
}
int main()
{
node * tmp = new node;
tmp->value=tmp->num1;
tmp->next=NULL;
node*h=new node;
h->value=h->num2;
h->next=NULL;
cout<<"Enter first number\n";
cin>>tmp->num1;
cout<<endl;
Print(tmp);
cout<<"Enter second number\n";
cin>>h->num2;
cout<<endl;
Print2(h);
cout<<endl;
if (CheckSmaller(tmp,h))
{cout<<"1";}
else {cout<<"-1";}
cout<<endl;
return 0;
}
شــــكرا مقدما
تعليق