I need help on how to integrate elements linked to the rank list.i already have a list of seven figures to be that the elements that must be included in the right of such a position that the salary arrangement upward
And the work of the Union and the intersection between 2 nodes

Recommended Answers

All 18 Replies

Please help as soon as possible because with me just one day to hand over the post

It might help to explain your problem is you show something that illustrates what you want to do, because I have no clue what you want.

I want the code process
Union
And the intersection between the two linked list

So what you really want to start with is something simple like "isMemberOf".

A union is merely a membership test to see if the item is in either list.
An intersection is a membership test to see if the item is in both lists.

How much of the list functionality do you have already, say
- create a list
- insert into a list
- remove from a list.
These are all basic functions which you'll need. If you can't do that much, then we need to help you with those rather than going straight for the complete solution.

I wrote this code to add and print linked list
And I have stayed code gymnasts European currencies and the intersection only House 2 linked list
List1, list2
For example:
List 1 = (1,3,5,7,8)
List2 = (1,9,1,3,5,2)
List1 union list2 = (1,3,5,7,8,9,2)
List1 intersection list2 =) 1,3,5)

#include <iostream>
#include <string>

using namespace std;

struct node
{
int data;
struct node *next;
};

class linkedList
{
node *head;
public:
void create();
void display();
void selectionSort();
void add();
void unita();
void intre();
};

void linkedList :: create()
{
node *newl=NULL,*end=newl;
int data;
head=NULL;
cout<<"\nCreate the List. Press <ENTER> after each entry\n";
for(int i=0;i<=5;i++)
{
cout<<"Enter a number: ";
cin>>data;
newl = new node;
newl->data=data;
if(head==NULL)
head=newl;
else
end->next=newl;
end=newl;
end->next=NULL;

}
cout<<"\nList is Created\n";
}

void linkedList :: display()
{
node *start;
start=head;
while(start!=NULL){
cout<<start->data; 
if(start->next!=NULL)
cout<<", ";
start=start->next;
}
}
void linkedList :: selectionSort()
{
node *i, *j;
int temp;
for(i=head;i!=NULL;i=i->next){
for(j=i->next;j!=NULL;j=j->next){
if(i->data > j->data){
temp = i->data;
i->data = j->data;
j->data = temp;
}
}
}
cout<<"\nThe numbers are Sorted\n";
}
void linkedList::add()
{
node *i, *j;
int data;
cout<<"Enter a number: ";
cin>>data;
j = new node;
j->data=data;
int temp;
for(i=head;i!=NULL;i=i->next){
if(i->data > j->data){
temp = i->data;
i->data = j->data;
j->data = temp;
}
}
}

void main()
{
linkedList list1;
list1.create();
cout<<"List1 before sorting "<<endl;
list1.display();
list1.selectionSort();
cout<<"List1 after sorting "<<endl;
list1.display(); 
linkedList list2;
list2.create();
cout<<"\n\n\n List2 before sorting "<<endl;
list2.display();
list2.selectionSort();
cout<<"List2 after sorting "<<endl;
list2.display();
cout<<"\n\n\nadd\n";
list1.add();
list1.display();

}

Your formatting leaves a lot to be desired.

void linkedList :: create()
{ 
   node *newl = NULL;
   node *end = newl;
   int data;
  
   head = NULL;
   cout << "\nCreate the List. Press <ENTER> after each entry\n";
   for(int i = 0; i <= 5; i++)
   {
      cout << "Enter a number: ";
      cin >> data;
      newl = new node;
      newl->data = data;
      if(head == NULL)
        head = newl;
      else
        end->next = newl;
      end = newl;
      end->next = NULL;
   }
   cout << "\nList is Created\n";
}

Note consistent indenting, spaces before and after operators, etc. It's not only easier to read code that's consistently and appropriately formatted, it's easier to debug too.

Assuming the posted code works as desired then the next step I would do is to decide exactly what constitutes a union. By that I mean do you have only one copy of a given value of data in the union or can you have multiple copies of data in the union? If you can have multiple copies, then just copy one list into the other to get the union. If you want unique copies of data in the union but there are multiple copies of data in either list then you need to get a little more creative.

Likewise, to me intersection means a list that has unique pieces of data that are in both lists from which the intersection is derived. Therefore pick one list as the target and one list as the source. Then you could use a nested loop something like for each node in the source check each node in the target. If a match is found look at each node the intersection. If matched node of the two lists isn't in the intersection then add it to the intersection.

I did not understand what it mean MY English is not good
I want the code works the intersection of Union and two list type integer

Like I said, a function called "isMemberOf" would be a really useful primitive for working out intersection and union

// return true if val is in the list
bool linkedList::isMemberOf( int val );

Void uinio(linkedlist l1,linkedlist l2)
{
??
??
??
??
??}

In pseudo-code

Intersection
for e = each element in l1
if isElementOf(l2,e) then add e to the result

Union
copy l1 to the result
for e = each element in l2
if NOT isElementOf(result,e) then add e to the result

I think the job of that function is to create a third linked list that is the union of l1 and l2

Void uinio(linkedlist l1,linkedlist l2)
{
    struct node* l3 = 0;
    struct node* node = l1;  
    // add the unique node in l1 to the new linked list
    while( node )
    {
        if( !IsMemberOf(l3, node->data) )
        {
              // add node->data to l3 list
        }
        node = node->next;
   }
   // do the same thing with l2
   node = l2;
    while( node )
    {
        if( !IsMemberOf(l3, node->data) )
        {
              // add node->data to l3 list
        }
        node = node->next;
   }

}

I thank you very Abietin but this sentence error
bool linkedList::isMemberOf( int val);

If allowed to be what I Tcherhali work isElementOf

Post your current code so that we can see what you may have done wrong.

Why Ietunai error on this sentence bool linkedList:: isMemberOf (int val);

Yes, we got that much of the problem already.

The question is, did you just put bool isMemberOf (int val); into the class declaration, without bothering to implement it, or have you tried to implement it and gotten some error messages.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.