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.
given two descending sorted linked list merge them into a third list in descending order. Assume...
given two descending sorted linked list merge them into a third list in descending order. Assume you have a function named merge list which takes two linked lists heads as input input and merge them somehow. Implement Merge_list() only
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
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT