Question

Merge two ordered singly linked lists of integers into one ordered list in C++

Merge two ordered singly linked lists of integers into one ordered list

in C++

Homework Answers

Answer #1

Please find the required C++ script as the following:

//===============================================================

#include <bits/stdc++.h>

using namespace std;

// Node class to create linked list nodes

class Node

{

public:

int data;

Node* next;

};

// Shifting node from one list's front to other

void MoveNode(Node** destRef, Node** sourceRef);

// The main merging program

Node* SortedMerge(Node* a, Node* b)

{

Node dummy;

Node* tail = &dummy;

/* so tail->next is the place to

add new nodes to the result. */

dummy.next = NULL;

while (1)

{

if (a == NULL)

{

/* if either list runs out, use the

other list */

tail->next = b;

break;

}

else if (b == NULL)

{

tail->next = a;

break;

}

if (a->data <= b->data)

MoveNode(&(tail->next), &a);

else

MoveNode(&(tail->next), &b);

tail = tail->next;

}

return(dummy.next);

}

// MoveNode() moved node from the front of the source to the front of the destination

void MoveNode(Node** destRef, Node** sourceRef)

{

Node* newNode = *sourceRef; // The front node

assert(newNode != NULL);

*sourceRef = newNode->next; // Moving the soruce node

newNode->next = *destRef; // Link onld to new

*destRef = newNode; // Point destination to new node

}

// Add a node at the beginning of the linked list

void push(Node** head_ref, int new_data)

{

Node* new_node = new Node(); // Create node

new_node->data = new_data; // Add data

new_node->next = (*head_ref); // Add new node to the list

(*head_ref) = new_node; // Make new node as the head

}

// Print nodes of a list

void printList(Node *node)

{

while (node!=NULL)

{

cout<<node->data<<" ";

node = node->next;

}

}

// Driver program

int main()

{

// Empty initialization

Node* res = NULL;

Node* a = NULL;

Node* b = NULL;

// Two sample sorted list creation

// a: 5->10->15, b: 2->3->20 */

push(&a, 15);

push(&a, 10);

push(&a, 5);

push(&b, 20);

push(&b, 3);

push(&b, 2);

// Merge removing duplicates

res = SortedMerge(a, b);

cout << "Merged Linked List is: \n";

printList(res);

return 0;

}

//=================================================================

output:

Merged Linked List is:
2 3 5 10 15 20

Know the answer?
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
Similar Questions
Write a program that uses Standard Library algorithm to merge two ordered lists of strings into...
Write a program that uses Standard Library algorithm to merge two ordered lists of strings into a single ordered list of strings, then displays the resulting list.
Write a C++ recursive function that counts the number of nodes in a singly linked list....
Write a C++ recursive function that counts the number of nodes in a singly linked list. (a) Test your function using different singly linked lists. Include your code. (b) Write a recurrence relation that represents your algorithm. (c) Solve the recurrence relation using the iterating or recursive tree method to obtain the running time of the algorithm in Big-O notation.
Implementing Polynomials using Singly Linked List in C++
Implementing Polynomials using Singly Linked List in C++
Machine Problem 3 - Linked List C++ For this assignment you will write a program that...
Machine Problem 3 - Linked List C++ For this assignment you will write a program that inserts 20 random integers from 0 to 100 in order in a linked list object. The program will create another linked list, but with 15 random integers from 0 – 100 in order. The program then will merge those two ordered linked list into a single ordered list. The function merge should receive references to each of the list objects to be merged and...
Implement a function that reverses a singly linked list in one pass of the list. The...
Implement a function that reverses a singly linked list in one pass of the list. The function accepts a pointer to the beginning of the list and returns a pointer to the beginning of the reversed list. The function must not create any new nodes. Be sure to test your function! You may start with code that you have written for other labs. This is a common interview question!
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question....
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question. Thank you! Here’s the contents of a file called example.cpp: // example.cpp #include "LinkedList.h" #include <iostream> #include <string> using namespace std; int main() { cout << "Please enter some words (ctrl-d to stop):\n"; LinkedList lst; int count = 0; string s; while (cin >> s) { count++; lst.add(remove_non_letters(s)); } // while cout << "\n" << count << " total words read in\n"; cout <<...
JAVA QUESTION: A singly linked list method that will: -A method that will find the largest...
JAVA QUESTION: A singly linked list method that will: -A method that will find the largest number in the list -A method that will find the smallest number in the list THESE ARE TWO SEPARATE BASIC METHODS
Implement the dictionary operations INSERT, DELETE and SEARCH using singly linked, circular lists. Explain the running...
Implement the dictionary operations INSERT, DELETE and SEARCH using singly linked, circular lists. Explain the running times of your procedures.
PYTHON : Describe a fast recursive algorithm for reversing a singly linked list
PYTHON : Describe a fast recursive algorithm for reversing a singly linked list
Singly Linked List In Javascript - How to implement when given functions and tests to pass...
Singly Linked List In Javascript - How to implement when given functions and tests to pass ------------------------------------------------------------------------------------------------------------------------- So I am trying to implement a singly linked list that given the below function will pass the bottom four tests when ran in Node.js with the 'runTests()' command. Please help me generate the singly linked list that will work and pass the tests at the bottom. I will only use this as a reference so please add comments to explain what you...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • For Parts #1 through #9, consider the following information: The superintendent’s office for the Jersey Dunes...
    asked 2 minutes ago
  • 1. Find the intersections and unions below, and give a proof for each. (a) ∩r∈(0,∞)[−r,r]. (b)...
    asked 12 minutes ago
  • Using the South University Online Library or the Internet, research a health issue in the world....
    asked 13 minutes ago
  • Complete the java program. /* Note: Do not add any additional methods, attributes. Do not modify...
    asked 38 minutes ago
  • Find a commercial, product, or advertisement and fill in the conditioning procedure. Unconditioned Stimulus → Unconditioned...
    asked 38 minutes ago
  • In a conversation with someone who you feel may have faced discrimination. Examples include someone with...
    asked 45 minutes ago
  • One measure of the meat quality of pigs is backfat thickness. Suppose two researchers, Jones and...
    asked 58 minutes ago
  • Polychlorinated biphenyls (PCBs), used in the manufacture of large electrical transformers and capacitors, are extremely hazardous...
    asked 58 minutes ago
  • 3. (10 marks) Describe a recursive algorithm for finding the maximum element in a array A...
    asked 1 hour ago
  • Three identical very small 50-kg masses are held at the corners of an equilateral triangle, 0,30m...
    asked 1 hour ago
  • C programming 1. Create a file function.c that contains a function called printNumbers() that prints the...
    asked 1 hour ago
  • You visit a local Starbucks to buy a Mocha Frappuccino. The barista explains that this blended...
    asked 1 hour ago