Question

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


  1. 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.

  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.


0 0
Add a comment Improve this question Transcribed image text
Answer #1

    it is very Program in c++. i upload full code with output and output screenshot,so just read carefully.Thank You.

    ----------------------------------------------------------------------
   Program Code:

#include<iostream>
using namespace std;
void createList1();
void createList2();
void displayList1();
void displayList2();
void findWords();
struct Node{

    int data;

    Node *next;

};
Node *head1,*head2;

int main()
{

head1 = NULL ;
head2 = NULL ;
createList1();//here we create list 1

createList2();//here we create list 2

displayList1();//display all elements in the list 1
displayList2();//display all elements in the list 2

findWords();
}

void createList1()
{

/*in this function we create list one using given data*/
Node *newNode, *last;
last = head1;

int listData[]={5, 78, 45, 23, 11, 89, 10, 78, 6, 99, 876, 5, 67, 13};

for (int i = 0; i < sizeof(listData)/sizeof(listData[0]); i++)
{
    /*here we create newnode and add to list*/
      newNode = new Node;
      newNode->data=listData[i];
      newNode->next = NULL;
      if(head1 == NULL)
      {
        head1 = newNode;
        last = newNode;
        /*add newnode to head because list is empty*/

      }
      else
      {
        /*move last to newnode*/
       last->next = newNode;
       last = newNode;
     }   
}

}

void createList2()
{

/*in this function we create list two using given data*/
Node *newNode, *last;
last = head2;

int listData[]={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};

for (int i = 0; i < sizeof(listData)/sizeof(listData[0]); i++)
{
    /*here we create newnode and add to list*/
      newNode = new Node;
      newNode->data=listData[i];
      newNode->next = NULL;
      if(head2 == NULL)
      {
        head2 = newNode;
        last = newNode;
        /*add newnode to head because list is empty*/

      }
      else
      {
        /*move last to newnode*/
       last->next = newNode;
       last = newNode;
     }   
}

}
void displayList1()
{
/*
In this method we just display link list 1 until it empty
*/
Node *newnode;
newnode = head1;
cout<<"\n**Display list 1**\n";

    while(newnode != NULL)
    {
      cout<<newnode->data<<" ";
      newnode = newnode->next;
    }
        cout<<"\n";

}

void displayList2()
{
/*
In this method we just display link list 2 until it empty
*/
Node *newnode;
newnode = head2;
cout<<"\n**Display list 2**\n";

    while(newnode != NULL)
    {
      cout<<newnode->data<<" ";
      newnode = newnode->next;
    }
        cout<<"\n";

}


void findWords()
{

/*
this function find and print only those number have present in list 1 and also more than
2 times in list2*/
Node *node1,*node2;
node1 = head1;
node2 = head2;

int numarr[50];
int k=0,i,j,k1,numcount=0;

cout<<"\n**The number appear in List#1 and more than 2 times in List#2 =\n";
while(node1 != NULL)
    {
      numcount=0;
        while (node2 != NULL)
          {
              /*here we iterate full list 2
              for each number in list 1*/

              if (node2->data==node1->data)
                  numcount++;
            
              node2 = node2->next;
          }
          if(numcount>2)
            numarr[k++]=node1->data;
            /*added such number in array*/     

         
    
      node1 = node1->next;
       node2 = head2;
       /*here we set list2 at the begining*/
     }

/*here we remove the duplicate element in numarr
i.e in our question there is two time 5 in list 1
so that it will print two times in output so we remove such element here*/

for(i = 0; i < k; i++)
    {
      for(j = i+1; j < k; )
        {
          if(numarr[j] == numarr[i])
            {
                for(k1 = j; k1 < k; k1++)
                {
                    numarr[k1] = numarr[k1+1];
                }
                k--;
            }
            else
            {
                j++;
            }
        }
    }

/*here we print final array*/
    for (i = 0; i < k; ++i)
    {
     cout<<numarr[i]<<" ";
    }

    cout<<"\n";
}

/*
----------------------OUTPUT--------------------
**Display list 1**
5 78 45 23 11 89 10 78 6 99 876 5 67 13

**Display 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

**The number appear in List#1 and more than 2 times in List#2 =
5 89 6 99

*/


    -----------------------OUTPUT SCREENSHOT-----------------------


en Activities Terminal Sun 4:36 AM nawale@Nawale-HP: -/Chegg/C and C++ File Edit View Search Terminal Help nawale@Nawale-HP:-

    ------------------------------------------------------------------------

    Still you have any doubt related these question then please Feel free and ask question in Comment Section Otherwise Please Like Answer,Thank You........!

Add a comment
Know the answer?
Add Answer to:
Write a C++ code to insert the following numbers in two Linked Lists. Insert numbers of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 2) You are given the following lists. Write code that produce the following resulting list. (30...

    2) You are given the following lists. Write code that produce the following resulting list. (30 points) Exam2 [C 12.py-PyCharm Edu 3.0 Eile Edit Yiew Navigate Code Help a 2-Py × nanes # [ 'April', 'BL11','Cindy." "Dave', 'Emily'] testl [34,21,67,45,88 test2 [ 11, 67, 92, 35, 89] test3-[94,33,67,34,67 testi [27, 83, 43, 67, 93] tests 43,76,78,45,65 0 Project *÷ o-r 1.py × @3.py × 1.py nh External Libraries resultinglist 10 print (resultingList) Run2 ↑ [["Aprll', 34, 11, 94, 27, 43],...

  • C# code Arrays and Linked Lists: Write C++/Java/C#/Python code to declare an array of linked lists...

    C# code Arrays and Linked Lists: Write C++/Java/C#/Python code to declare an array of linked lists of any primitive type you want. (Array of size 2020) (This could be based on MSDN libraries or the lab) – you do not need to instantiate any of the linked lists to contain any actual values. Paste your code for that here (this should only be one line) Based on your code or the lab from 4 or your doubly linked list from...

  • C++ You're given the pointer to the head nodes of two linked lists. Compare the data...

    C++ You're given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. The lists are equal only if they have the same number of nodes and corresponding nodes contain the same data. Either head pointer given may be null meaning that the corresponding list is empty. Input Format You have to complete the int CompareLists (Node headA, Node* head B) method which takes...

  • Write a program that can: 1. Insert twenty random numbers into a linked list. The numbers...

    Write a program that can: 1. Insert twenty random numbers into a linked list. The numbers should be within a range (E.g., 1 to 7). The user should be prompted to enter the minimum number and maximum number of the range. Each number should be inserted at the end of the list. Section 7.8 of the textbook covers the random number generator. Examples of how to use the random number generator are in Fig 7.6 and 7.7. Here is a...

  • Arrays, Lists, Stacks and Queues: 1) Write C++ code to reverse a singly-linked list L using...

    Arrays, Lists, Stacks and Queues: 1) Write C++ code to reverse a singly-linked list L using only a constant amount of additional storage and no recursion. Assume the list object has a head pointer _head and consists of Node objects; a Node object has a pointer Node* _next

  • Codelab question! This is c++. Consider the following code: // Linked Lists: TRAVERSE struct ListNode {...

    Codelab question! This is c++. Consider the following code: // Linked Lists: TRAVERSE struct ListNode { int data; struct ListNode *next; }; Assume that a linked list has been created and head points to a sentinel node. A sentinel node is an empty data node in the beginning of the list. It sometimes holds a sentinel value. The use of sentinel nodes is a popular trick to simplify the insert and delete operations. You may also assume that the list...

  • C++ pointers and linked lists 1. Declare an integer pointer variable intPointer. Initialize it to point...

    C++ pointers and linked lists 1. Declare an integer pointer variable intPointer. Initialize it to point to an int variable named someInt. Assign the value 451 to someInt and output (cout) the variable someInt and output (cout) the value pointed to by intPointer. Write an assignment statement that indirectly stores 900 into the value pointed to by intPointer. Output (cout) the value pointed to by intPointer and output (cout) the variable someInt, 2. Declare a pointer variable charArrPointer and initialize...

  • In NetBeans Create a new Java Application to manage Linked Lists: (Note: Do not use java.util.LinkedList)...

    In NetBeans Create a new Java Application to manage Linked Lists: (Note: Do not use java.util.LinkedList) a) Create a DateTime class 1. Add day, month, year, hours, minutes as attributes 2. Add a constructor and a toString() method 3. Implement the Comparable interface, and add a CompareTo() method 4. Add methods to get and set all attributes. b) Add to MyLinkedList class the following methods: 1. Insert a Node to a particular position in the List 2. Insert a Node...

  • python Programming assignment: Let's think about doubly-linked lists. Define a class ListNode2, with three attributes: item,...

    python Programming assignment: Let's think about doubly-linked lists. Define a class ListNode2, with three attributes: item, left, and rightL. Left link points to the previous node in the list, right link points to the next node in the list. You can also add the display method to this class (like we did it in class for the ListNode class). Then test your class. For example, create a linked list of 5 values: 34,1, 23, 7, and 10. Display it. Then...

  • Write a program in C to make dictionary to add, delete and search words Using linked lists. MAKE THE SEARCHING IN THE pr...

    Write a program in C to make dictionary to add, delete and search words Using linked lists. MAKE THE SEARCHING IN THE program like the searching in the online dictionary(if we enter the 1 letter then it will show all the letters starting with the same number and if we enter 2 letters then it will show all the numbers starting with same two letters and so on up to the complete word.) make the following functions 1. insert 2....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT