Question

In: Computer Science

//C++ a. Add the following operation to the class orderedLinkedList: void mergeLists(orderedLinkedList &list1, orderedLinkedList &list2); //This...

//C++

a. Add the following operation to the class orderedLinkedList:

void mergeLists(orderedLinkedList &list1, orderedLinkedList &list2);

//This function creates a new list by merging the //elements of list1 and list2.

//Postcondition: first points to the merged list

// list1 and list2 are empty Consider the following statements:

orderedLinkedList newList;

orderedLinkedList list1;

orderedLinkedList list2;

Suppose list1 points to the list with the elements 2 6 7, and list2 points to the list with the elements 3 5 8.

The statement: newList.mergeLists(list1, list2); creates a new linked list with the elements in the order 2 3 5 6 7 8, and the object newList points to this list. Also, after the preceding statement executes, list1 and list2 are empty.

b. Write the definition of the function template mergeLists to implement the operation mergeLists.

Solutions

Expert Solution

//C++ program

#include<iostream>
using namespace std;

template <typename T>
class orderedLinkedList{
   private:
       struct node{
           T data;
           struct node*next;
       };
      
       struct node* head;
      
   public:
       orderedLinkedList(){
           head = NULL;
       }
       void add(T data){
           struct node* newNode = new node;
           newNode->data = data;
           newNode->next = NULL;
          
           if(head == NULL){
               head = newNode;
               return;
           }
           struct node* current = head;
           while(current->next)current = current->next;
          
           current->next = newNode;
          
       }
      
       void print(){
           struct node* current = head;
          
           while(current){
               cout<<current->data << " ";
               current = current->next;
           }
       }
       void mergeLists(orderedLinkedList<T> &list1,orderedLinkedList<T> &list2){
           struct node* left = list1.head;
           struct node* right= list2.head;
           struct node* newNode,*tail = head;
           int data;
          
           while(left != NULL && right != NULL){
               if(left->data < right->data){
                   data = left->data;
                   left = left->next;
               }
               else{
                   data = right->data;
                   right = right->next;
               }
               newNode = new node;
               newNode->data = data;
               newNode->next = NULL;
               if(head == NULL){
                   head = newNode;
               }
               else{
                   tail->next = newNode;
               }
               tail = newNode;
           }
          
           while(left != NULL){
               newNode = new node;
               newNode->data = left->data;
               newNode->next = NULL;
               if(head == NULL){
                   head = newNode;
               }
               else{
                   tail->next = newNode;
               }
               tail = newNode;
               left = left->next;
           }
          
           while(right != NULL){
               newNode = new node;
               newNode->data = right->data;
               newNode->next = NULL;
               if(head == NULL){
                   head = newNode;
               }
               else{
                   tail->next = newNode;
               }
               tail = newNode;
               right = right->next;
           }
       }
  
};

int main(){
   orderedLinkedList<int> newList;
   orderedLinkedList<int> list1;
   orderedLinkedList<int> list2;
  
   list1.add(2);
   list1.add(6);
   list1.add(7);
  
   list2.add(3);
   list2.add(5);
   list2.add(8);
  
   newList.mergeLists(list1, list2);
  
   cout<<"Merged list\n";
   newList.print();
  
   return 0;
}


Related Solutions

Python...Count the number of times a element of list1 occurs in in list2 list1 = ['a',...
Python...Count the number of times a element of list1 occurs in in list2 list1 = ['a', 'b', 'c','e','j']; list2 = ['a', 'c', 'd', 'b','e','z']; {'a': 1, 'c': 1, 'b': 1, 'e': 1, 'j': 0} How do I get to this to work without the Collections counter?
This program is about list comprehension. list1 = [2, 5, 7, 8] list2 = [1, 2]...
This program is about list comprehension. list1 = [2, 5, 7, 8] list2 = [1, 2] (e) Use nested list comprehension with list1 and list2 as input sequences to generate this list: [[3, 4], [6, 7], [8, 9], [9, 10]] Display the list. The following is the expected output. Part e: [[3, 4], [6, 7], [8, 9], [9, 10]]
By using Python code: 1. list1 = ['smith', 'john', 'andy'] list2 = ['kim', 'mary', 'paul'] Write...
By using Python code: 1. list1 = ['smith', 'john', 'andy'] list2 = ['kim', 'mary', 'paul'] Write a python program which does the following: a. add items in list2 to list1 b. Remove mary from list1 c. Insert sam at 5th position d. Sort the elements of list1 in ascending order 2. Write a python program that asks the user to enter a sentence and creates a list of words in that sentence. 3. friends = ('sam','andy','mary','andy','john','andy','mary') Write a python program...
C# Programming (Class Registration class) Add a Schedule property to the Person class. Add a new...
C# Programming (Class Registration class) Add a Schedule property to the Person class. Add a new behavior as well: add(Section s) this behavior will add a section to the Person’s schedule. The Person’s display() function will also need to be modified to display() the Person’s Schedule. Schedule class has Properties: An array of Sections and a Count. Constructors: only a default constructor is needed. Behaviors: add() and display(). This is my schedule class class Schedule     {         private int...
Method: ArrayList<Integer> diff(ArrayList<Integer> list1, ArrayList<Integer> list2) diff() method accepts two ArrayLists of Integer and returns the...
Method: ArrayList<Integer> diff(ArrayList<Integer> list1, ArrayList<Integer> list2) diff() method accepts two ArrayLists of Integer and returns the union of elements in two lists. For example: list1 contains elements [1, 2, 3, 4, 5] list2 contains elements [3, 4, 5, 6, 7] Diff() method should return an array list with elements [1, 2, 3, 4, 5, 6, 7].
////Fixme(1) add a statement to import ArrayList class public class ListManipulation { public static void main(String[]...
////Fixme(1) add a statement to import ArrayList class public class ListManipulation { public static void main(String[] args) { //Fixme(2) create an ArrayList of integers and name the ArrayList list. //Fixme(3) add the following numbers to the list: 10, 15, 7, -5, 73, -11, 100, 20, 5, -1    displayList(list); System.out.println(); displayListBackwards(list);       }    public static void displayList(ArrayList<Integer> list) { for(Integer i: list) System.out.print(i + " ");    } //Fixme(4) define a method displayListBackwards, which takes an ArrayList as...
#ifndef CCALC_HEADER #define CCALC_HEADER    class   CCalc { public:     // member functions     CCalc();     void    Add(double...
#ifndef CCALC_HEADER #define CCALC_HEADER    class   CCalc { public:     // member functions     CCalc();     void    Add(double value);     void    Clear();     void    Divide(double value);     double  GetValue() const;     void    Multiply(double value);     void    SetValue(double  newValue);     void    Subtract(double value);    private:     // data members     double  m_total; };    #endif // CCALC_HEADER int     main() {     CCalc       calculator;     char        choice;        // loop and let the user manipulate the calculator     do {         // display the menu and get the user selection         DisplayMenu();         cout <<...
C++ Program Overload the following operators to work with the Rational class and add test cases...
C++ Program Overload the following operators to work with the Rational class and add test cases in the driver program. Make sure your driver program now tests each of these symbols for your Rational Class. + – * / == != < <= > >= << (stream insertion operator, use the toString function) >> (stream extraction operator) Make sure you test all 12 operators Example Run (Bold is input), everything else is a print statement using the overloaded operators Enter...
Study the following class definition: class Car { public: Car(double speed); void start(); void accelerate(double speed);...
Study the following class definition: class Car { public: Car(double speed); void start(); void accelerate(double speed); void stop(); double get_speed() const; private: double speed; }; Which of the following options would make logical sense in the definition of the void accelerate(double speed)function? Group of answer choices this->speed = this->speed; this->speed = speed; this.speed = speed; speed1 = this->speed; Flag this Question Question 131 pts The Point class has a public function called display_point(). What is the correct way of calling...
WRITE IN C++ Add to the Coord class Edit the provided code in C++ Write a...
WRITE IN C++ Add to the Coord class Edit the provided code in C++ Write a member function named      int distance2origin() that calculates the distance from the (x, y, z) point to the origin (0, 0, 0) the ‘prototype’ in the class definition will be      int distance2origin() outside the class definition             int Coord :: distance2origin() {                         // your code } _______________________________________________________________________________________________________ /************************************************** * * program name: Coord02 * Author: * date due: 10/19/20 * remarks:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT