Question

In: Computer Science

With C++, 1. Assume we use two linked lists that represent Set A and Set B...

With C++,

1. Assume we use two linked lists that represent Set A and Set B respectively. Implement the
following function to calculate A = A U B. Note that a SET should not contain duplicated
elements (e.g., integers).


void unionLL (Node * LA, Node * LB);


2. There are two linked lists, LA and LB. Their elements are both in the non-descending order.
Implement the following function to merge LA and LB into a new linked list, LC. Make sure that
the elements in LC are still in the non-descending order. (50 Points)


void mergeLL (Node * LA, Node * LB, Node * LC);


Example:
LA = (3, 5, 8, 11)
LB = (2, 6, 8, 9, 22, 24)
Then:
LC = (2, 3, 5, 6, 8, 8, 9, 11, 22, 24)


Additional requirements:
1. Create a main function and print out the numbers in the linked lists before and after
executing each method above.

2. Assume Node is defined as follows:
struct Node{
int num;
Node * next;
};

Any help appreciated.

Solutions

Expert Solution

1. code:

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

struct Node
{
int num;
struct Node *next;
};
void append (struct Node **L_head, int x);

bool isPresent (struct Node *head, int num);

void printList (struct Node *node);

void unionLL (Node * LA, Node * LB)
{
struct Node *t1 = LA;
struct Node *t2 = LB;
struct Node *t3 = NULL;
while (t1 != NULL)
{
append (&t3, t1->num);
t1 = t1->next;
}
while (t2 != NULL)
{
if (!isPresent (t3, t2->num))
   append (&t3, t2->num);

t2 = t2->next;
}
printf ("\nUnion of A and B is:\n");
printList (t3);
}

void append (struct Node **L_head, int x)
{
struct Node *new_node = (struct Node *) malloc (sizeof (struct Node));
struct Node *last = (*L_head);

new_node->num = x;
new_node->next = NULL;
if (*L_head == NULL)
{
*L_head = new_node;
return;
}
while (last->next != NULL)
last = last->next;
last->next = new_node;
}

void printList (struct Node *node)
{
while (node != NULL)
{
printf ("%d ", node->num);
node = node->next;
}
}

bool isPresent (struct Node *head, int num)
{
struct Node *t = head;
while (t != NULL)
{
if (t->num == num)
   return 1;
t = t->next;
}
return 0;
}

int main ()
{
struct Node *LA = NULL;
struct Node *LB = NULL;
struct Node *mergeL = NULL;
append (&LA, 3);
append (&LA, 5);
append (&LA, 8);
append (&LA, 11);
append (&LB, 2);
append (&LB, 6);
append (&LB, 8);
append (&LB, 9);
append (&LB, 22);
append (&LB, 24);
printf ("\nA is:\n");
printList (LA);
printf ("\nB is:\n");
printList (LB);

unionLL (LA, LB);
return 0;
}
output:


Related Solutions

C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of...
C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of songs/artist pairs. Allow your user to add song / artist pairs to the list, remove songs (and associated artist) from the list and be sure to also write a function to print the list! Have fun! Make sure you show your implementation of the use of vectors in this lab (You can use them too ) You MUST modularize your code ( meaning, there...
The goal of this assignment is to implement a set container using linked lists. Use the...
The goal of this assignment is to implement a set container using linked lists. Use the authors bag3.h and bag3.cpp as a basis for implementing your set container using linked lists. The authors bag3.h and bag3.cpp can be found here https://www.cs.colorado.edu/~main/chapter5/ Since you are using the authors bag3.h and bag3.cpp for your Set container implementation, make sure that you change the name of the class and constructors to reflect the set class. Additionally you will need to implement the follow...
Working with Linked Lists in C++ Tasks As stated in the objectives, you have two methods...
Working with Linked Lists in C++ Tasks As stated in the objectives, you have two methods to implement. These methods (which contain TODO comments) are found in linked_list.cpp. Part 1 Implement the copy constructor; use the LinkedBag as inspiration for your solution as the technique of creating a deep copy is essentially the same. Part 2 Implement the replace method. //linked_list.cpp #include "linked_list.h" // Header file #include <cassert> template<class Object> LinkedList<Object>::LinkedList() : headPtr( nullptr ), itemCount( 0 ) { }...
Using C++, you will create a program, where you will create two doubly linked lists. These...
Using C++, you will create a program, where you will create two doubly linked lists. These doubly linked lists will contain integers within them. Using the numbers in both of these linked lists, you add the numbers together, and insert the addition of the two numbers into a singly linked list. the input can be from the user or you just write the input. for example, if one number in the doubly linked list is 817 and in the other...
Write a pseudocode function that interchanges two adjacent items of: (a) singly linked lists (b) doubly...
Write a pseudocode function that interchanges two adjacent items of: (a) singly linked lists (b) doubly linked lists if you can make it clean and short that be nice
In C++ Use vectors instead of linked lists Create a Hash table program using H(key) =...
In C++ Use vectors instead of linked lists Create a Hash table program using H(key) = key%tablesize with Chaining and Linear probing for a text file that has a list of 50 numbers Ask the user to enter the file name, what the table size is, and which of the two options they want to use between chaining and linear probing
C++ language or Python. Linked Lists You are given a linked list that contains N integers....
C++ language or Python. Linked Lists You are given a linked list that contains N integers. You are to perform the following reverse operation on the list: Select all the subparts of the list that contain only even integers. For example, if the list is {1,2,8,9,12,16}, then the selected subparts will be {2,8}, {12,16}. Reverse the selected subpart such as {8,2} and {16,12}. The list should now be {1,8,2,9,16,12}. Your node definition should consist of 2 elements: the integer value...
1- Use FirstLastList: Write method public void join(FirstLastList SecondList) such that given two linked lists, join...
1- Use FirstLastList: Write method public void join(FirstLastList SecondList) such that given two linked lists, join them together to give one. So if the lists lst1= [1,3,7,4] and lst2=[2,4,5,8,6], the result of lst1.join(lst2) is lst1=[1,3,7,4,2,4,5,8,6] and lst2=[]. 2- Use FirstLastList: Write method public void swap(). It swaps the first and last elements of a FirstLastList. So if lst1= [1,3,7,4], lst1.swap() = [4,3,7,1]. Display or throw an exception if the list contains less than two elements. Demonstrate by displaying the list...
Use the set definitions A = {a, b} and B = {b, c} to express the elements in the power set P(A ∪ B).
Fill in Multiple BlanksUse the set definitions A = {a, b} and B = {b, c} to express the elements in the power set P(A ∪ B).Use {} for the empty setDo not add any spaces in your answer.  Example  {f}  not {  f  }    {e,f,g} not {e, f, g}{[1],[2], [3], [4], {a,b}, {b,c}, [5], [6]}
In C++ In this lab we will creating two linked list classes: one that is a...
In C++ In this lab we will creating two linked list classes: one that is a singly linked list, and another that is a doubly linked list ( This will be good practice for your next homework assignment where you will build your own string class using arrays and linked list ) . These LinkedList classes should both be generic classes. and should contain the following methods: Print Add - Adds element to the end of the linked list. IsEmpty...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT