Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7734481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:15:38+00:00 2026-06-01T07:15:38+00:00

In my void union function I am unsure how to insert the data from

  • 0

In my “void union” function I am unsure how to insert the data from both linked lists “A” and “B” that the user has input respectively since the “AUB” is not within the void function. I would have put:

AUB.insert

but I was unsure. Any suggestions?

#include "stdafx.h"
#include <iostream>

using namespace std;

class Sets
{
private:struct NODE
        {
            int info;
            NODE *next;
        };
        NODE *list;
public:Sets()
       {
           list=NULL;
       }
       void Insert(int x)
       {
           NODE *p=list, *q=list, *r;
           //create a new node
           r = new (NODE);
           r->info = x;
           r->next = NULL;
           //find the insertion place
           while(p != NULL && p->info < x)
           {
               q=p;
               p=p->next;
           }
           if(p==list)//x is the first info
           {
               list=r;
               r->next=p;
           }
           else if(p==NULL)//x is the last info
           {
               q->next=r;
           }
           else //x is neither forst nor last info
           {
               r->next=p;
               q->next=r;
           }
       }
        void display()
        {
            NODE *p=list;
            while(p != NULL)
            {
                cout << p->info << "-->";
                p=p->next;
            }
            cout << "NULL\n";
        }
        void Union(Sets setA,Sets setB)
        {
            NODE *p=setA.list, *q=setB.list;
            while(p != NULL && q != NULL)
            {
                if(p->info > q-> info)
                {
                    (q->info)
                    q=q->next;
                }
                else if(p->info == q->info)
                {
                    insert(p->info)
                    p=p->next;
                    q=q->next;
                }
                else//P->info < q->info
                {
                    insert(p->info);
                    p=p->next;
                }
            }
            while(p !=NULL)
            {
                insert(p->info);
                p=p->next;
            }
            while(q != NULL)
            {
                insert(q->info);
                q=q->next;
            }
        }
};


int main()
{   
    //create a set of integers
    int x;
    Sets A, B, setAUB;
    cout << "Enter data for setA:\n";
    cout << "Enter a group of positive integer numbers with -1 at the end end: ";
    cin >> x;
    while(x != -1)
    {
        A.Insert(x);
        cin >> x;
    };
    //display setA
    cout << endl << "setA=";
    A.display();

    cout << "Enter data for setB:\n";
    cout << "Enter a group of positive integer numbers with -1 at the end end: ";
    cin >> x;
    while(x != -1)
    {
        B.Insert(x);
        cin >> x;
    };
    //display setB
    cout << endl << "setB=";
    B.display();

    setAUB.Union(A, B);
    //display setAUB
    cout << endl << "setAUB=";
    setAUB.display();

    system ("pause");

    //terminate program
    return 0;
}; 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T07:15:40+00:00Added an answer on June 1, 2026 at 7:15 am

    You define it: void Union(Sets setA,Sets setB).

    What are you doing? Both passed by value, and the return value is void – where does the result go?

    Does your current object (this in the Union function) become that union? If so, what happens to the data already in it? You’re not deleting it, so you’re basically merging three sets, not two…

    I would suggest to create a static merge function that would take the two parameters and return a new list which would be the merge of the two.

    Otherwise, create a regular merge function, that would only take 1 parameter, and merge it into the current object.

    By the way – why Sets, when it is obviously a sorted linked list?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple function Bar that uses a set of values from a
If have a union that currently foresees some simple data types: union Value {
i want to build a pointer to a Qt Slot: union { void (*set_slot)(unsigned
I am working on a C program that uses a Union. The union definition
I'm implementing a union-find data structure in C#. The elements must extend the Element
I have a function like this: void something(void *obj) { obj->Set(); } The compiler
So I'm trying to write a program that finds the union, intersection, and determining
Let's say there is a function like void SendToTheWorld(const Foo& f); and I need
I have looked up how to pass an enumeration in a function, but that
I have a callback function called got_packet: void got_packet(u_char *args, const struct pcap_pkthdr *header,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.