Question: The code must be done in C++. Please read the question very carefully and write just according to the question.I repet exactly according to the
The code must be done in C++. Please read the question very carefully and write just according to the question.I repet exactly according to the question.
4.[21 points] Suppose there is two single linked list- ListA (contains five elemets) and ListB(contains 4 elemets).
- Write a boolean function to check if ListB is subset of ListA.
- If the Boolean function returns true delete the elemets of ListB from ListA.
- Example: If List A and ListB is
ListA: 10->7->6->9->2->NULL
ListB: 7->6->10->9->NULL
- B is a subset of A. So Boolean function will return true.
- As Boolean function returned true elements of ListB will be deleted from ListA and finally ListA will be-
ListA: 2->Null
You have to answer both (a) and (b) in same code calling from main function. The base code is given bellow. You can use either (1) or (2). Modify and write only the main function and the additional function needed to solve the problem in the answer script.
1)
struct node{
int data;
node* next;
};
void add(node* &sll, int data){
if(sll==NULL){
node* newNode = new node;
newNode->data = data;
newNode->next = sll;
sll = newNode;
}
else{
node* current = sll;
while(current->next!=NULL){
current = current->next;
}
node* newNode = new node;
newNode->data = data;
newNode->next = current->next;
current->next = newNode;
}
}
int main()
{ node* List1 = NULL;
node* List2 = NULL;
int n1,n2;
cout << Enter how many nodes you want to enter at list1:
cin >> n1; // n=5
for int (int i = 0; i < n1; i++)
{
int newData = rand()%100;
add(List1, newData);
}
cout << Enter how many nodes you want to enter at list2:
cin >> n2; // n=4
for int (int i = 0; i < n2; i++)
{
int newData = rand()%100;
add(List2, newData);
}
return 0;
}
2)
struct node
{
int data;
node* next;
};
class linked_list
{
private:
node* head= NULL;
node* tail= NULL;
public:
void add_node(int n)
{
node* tmp = new node;
tmp->data = n;
tmp->next = NULL;
if(head == NULL)
{
head = tmp;
tail = tmp;
}
else
{
tail->next = tmp;
tail = tail->next;
}
}
};
int main()
{
linked_list a;
linked_list b;
for int (int i = 0; i < 6; i++)
{
int newData = rand()%100;
a.add_node(newData);
}
for int (int i = 0; i < 5; i++)
{
int newData = rand()%100;
b.add_node(newData);
}
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
