Question

In: Computer Science

Write a C++ code to insert the following numbers in two Linked Lists. Insert numbers of...

Write a C++ code to insert the following numbers in two Linked Lists. Insert numbers of first list in Linked List#1, and numbers of second list in Linked List#2. Do not insert both lists in a single Linked List.

List#1.

5, 78, 45, 23, 11, 89, 10, 78, 6, 99, 876, 5, 67, 13

List#2.

5, 89, 688, 52, 557, 953, 5, 7, 55, 35, 89, 99, 99, 6, 557, 89, 5, 99, 6, 2, 45, 12, 7, 6, 94, 93, 99, 67

After inserting numbers in linked lists. Write a function to print only those numbers that appear in List#1 and more than 2 times in List#2.


For example, the number “5” appears in List#1 and 3 times in List#2. So your code should print “5” and similar other numbers.


Solutions

Expert Solution

#include <iostream>

using namespace std;
struct Node
{
int data;
struct Node *next;
};

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

new_node->data = new_data;
  
new_node->next = NULL;
  
if (*head_ref == NULL)
{
*head_ref = new_node;
return;
}   

while (last->next != NULL)
last = last->next;

last->next = new_node;
return;   
}

void print_common_element(Node *head1,Node *head2){
while(head1){
int count = 0;
int search_element = head1->data;
Node *tmp = head2;
while(tmp){
if(tmp->data == search_element)
count++;
tmp = tmp->next;
}
if (count > 2)
printf("The number %d appears in list1 and more than twice in list2\n",search_element);
head1 = head1->next;
}
}

int main()
{
//5, 78, 45, 23, 11, 89, 10, 78, 6, 99, 876, 5, 67, 13
struct Node* head1 = NULL;
append(&head1, 5);
append(&head1, 78);
append(&head1, 45);
append(&head1, 23);
append(&head1, 11);
append(&head1, 89);
append(&head1, 10);
append(&head1, 78);
append(&head1, 6);
append(&head1, 99);
append(&head1, 876);
append(&head1, 5);
append(&head1, 67);
append(&head1, 13);
  
//5, 89, 688, 52, 557, 953, 5, 7, 55, 35, 89, 99, 99, 6, 557, 89, 5, 99, 6, 2, 45, 12, 7, 6, 94, 93, 99, 67
struct Node* head2 = NULL;
append(&head2, 5);
append(&head2, 89);
append(&head2, 688);
append(&head2, 52);
append(&head2, 557);
append(&head2, 953);
append(&head2, 5);
append(&head2, 7);
append(&head2, 55);
append(&head2, 35);
append(&head2, 89);
append(&head2, 99);
append(&head2, 99);
append(&head2, 6);
append(&head2, 557);
append(&head2, 89);
append(&head2, 5);
append(&head2, 99);
append(&head2, 6);
append(&head2, 2);
append(&head2, 45);
append(&head2, 12);
append(&head2, 7);
append(&head2, 6);
append(&head2, 94);
append(&head2, 93);
append(&head2, 99);
append(&head2, 67);
  
print_common_element(head1,head2);
  
return 0;
}


Related Solutions

C++ Question Create two circular linked lists and find their maximum numbers. Merge the two circular...
C++ Question Create two circular linked lists and find their maximum numbers. Merge the two circular linked lists such that the maximum number of 2nd circular linked list immediately follows the maximum number of the 1st circular linked list. Input: 12 -> 28 -> 18 -> 25 -> 19-> NULL 5 -> 24 -> 12 -> 6 -> 15-> NULL Output: 28 -> 24-> 25 -> 15 -> 19 -> 15->5-> 18 -> 25 -> 19->NULL Note:- Code should work...
Write the code for postfix expression in C++ using a linked stack that can take numbers...
Write the code for postfix expression in C++ using a linked stack that can take numbers bigger than 9 (any size the user gives) and pushes the final result onto the top of the stack
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...
Python linked lists ● Insert: this method takes a value as a parameter, and adds a...
Python linked lists ● Insert: this method takes a value as a parameter, and adds a node which contains the value to the end of the linked list ● Delete: this method deletes a node from the linked list. If an index is passed as a parameter, then the method should delete the node at this index. If no index is passed, then delete the first item in the list ● Find: this method takes a value as a parameter,...
Write a recursive function in C++ that creates a copy of an array of linked lists....
Write a recursive function in C++ that creates a copy of an array of linked lists. Assuming: struct node { int data; node * next; }; class arrayList { public: arrayList(); ~arrayList(); private: node ** head; int size; //(this can equal 10) }
Modify this linked list code to work with string. Insert the following items into the list...
Modify this linked list code to work with string. Insert the following items into the list and display the list. The items are: Pepsi, Coke, DrPepper, Sprite, Fanta. Insert them in that order. Display the list. Then delete DrPepper and redisplay the list. Then insert 7-UP and redisplay the list. Then append Water and redisplay the list. c++ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ #include <iostream> using namespace std; class ListNode { public:     int value;     ListNode *next;     ListNode(int nodeValue) {       value...
Given two lists, write python code to print “True” if the two lists have at least...
Given two lists, write python code to print “True” if the two lists have at least one common element. For example, x = [1,2,3], y=[3,4,5], then the program should print “True” since there is a common element 3.
Given two sorted lists, L1 and L2, write an efficient C++ code to compute L1 ∩...
Given two sorted lists, L1 and L2, write an efficient C++ code to compute L1 ∩ L2 using only the basic STL list operations. What is the running time of your algorithm?
Please edit the code with linked lists in C++ Please provide your BookData.txt file as well...
Please edit the code with linked lists in C++ Please provide your BookData.txt file as well BookRecord.cpp file #include "BookRecord.h" #include <stdio.h> #include <string.h> #include <iostream> #include <fstream> using namespace std; BookRecord::BookRecord() {    strcpy_s(m_sName, "");    m_lStockNum = 0;    m_iClassification = 0;    m_dCost = 0.0;    m_iCount = 0; } BookRecord::BookRecord(const char* name, long sn, int cl, double cost) {    strcpy_s(m_sName, name);    m_lStockNum = sn;    m_iClassification = cl;    m_dCost = cost;    m_iCount...
Can you write a program for the card game WAR using linked lists in c++!
Can you write a program for the card game WAR using linked lists in c++!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT