Answer to Question #13025 in C++ for Bassam

Question #13025
Write a program to make two dynamic single linked list and make them as one??

sorry for my bad english
1
Expert's answer
2012-08-14T09:23:21-0400
#include <iostream>

using namespace std;
struct NODE
{
int value;
NODE *link;
};

NODE *add(NODE *beg, int value)
{
NODE *tmp = beg;
if (beg != NULL)
{
& while (tmp->link!=NULL)
& {
tmp = tmp->link;
& }
& tmp->link = new NODE;
& tmp->link->value = value;
& tmp->link->link = NULL;

}
else
{
& beg = new NODE;
& beg->value = value;
& beg->link = NULL;
}
return beg;
}

void output(NODE *beg)
{
if (beg == NULL)
{
& cout<<"Empty"<<endl;
& return;

}
while(beg != NULL)
{
& cout<<beg->value<<"& ";
& beg = beg->link;
}
cout<<endl;
}

int main()
{
NODE *list1 = NULL, *list2 = NULL;
int n1, n2;
cout<<"Enter list1 length: ";
cin>>n1;
cout<<"Enter list2 length: ";
cin>>n2;
for (int i=0;i<n1;i++)
{
& list1 = add(list1,100+i+1);
}
for (int i=0;i<n2;i++)
{
& list2 = add(list2,200+i+1);
}
cout<<"List 1: ";
output(list1);
cout<<"List 2: ";
output(list2);
NODE *tmp = list1;
if (tmp != NULL)
{
& while (tmp->link!=NULL)
& {
tmp = tmp->link;
& }
& tmp->link = list2;
}
else
{
& list1 = list2;
}
cout<<"Total list: ";
output(list1);
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS