SlideShare a Scribd company logo
1 of 4
Download to read offline
Use C++ Write a function to merge two doubly linked lists. The input lists have their elements in
sorted order, from lowest to highest. The output list should also be sorted from lowest to highest.
Your algorithm should run in linear time on the length of the output list. Provide an algorithm for
your function Implement and show some samples of your running function
Solution
/* This is a C++ program to merge two sorted linked lists and produce a list in a
Sorted order */
#include
#include
#include
/* Link list node */
struct node
{
int data;
struct node* next;
};
/* This function is used to pull off the front node of the source and put it in destnation */
void letusMoveNode(struct node** destRef, struct node** sourceRef);
/* This is a function used for performing merging of two linked lists.It takes two lists sorted in
increasing order, and splices
their nodes together to make one big sorted list which
is returned. */
struct node* letssortlists(struct node* a, struct node* b)
{
/* Let us first have dummyone first node to hang the result on */
struct node dummyone;
/*next the tail points to the last result node */
struct node* tail = &dummyone;
/* As a result, tail->next is the place to add new nodes
to the result. */
dummyone.next = NULL;
while (1)
{
if (a == NULL)
{
/*we will check that 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)
letusMoveNode(&(tail->next), &a);
else
letusMoveNode(&(tail->next), &b);
tail = tail->next;
}
return(dummyone.next);
}
/* letusMoveNode() function is a function which takes the node from the front of the
source, and move it to the front of the dest.
Before calling letusMoveNode():
source == {1, 2, 3}
dest == {1, 2, 3}
Affter calling letusMoveNode():
source == {3, 4}
dest == {1, 2, 3, 4} */
void letusMoveNode(struct node** destReference, struct node** sourceReference)
{
/*This is the front source node */
struct node* newNode = *sourceReference;
assert(newNode != NULL);
/* now we will advance the source pointer */
*sourceReference = newNode->next;
/*next we will link the old dest off the new node */
newNode->next = *destReference;
/* Fineally we will move dest to point to the new node */
*destReference = newNode;
}
/* This IS A function which is used to insert a node at the beginning of the linked list */
void insertpush(struct node** head_reference, int new_data)
{
/* This is used to allocate node */
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
/* now we will put in the data */
new_node->data = new_data;
/* The next step is to link the old list off the new node */
new_node->next = (*head_reference);
/*Finally we will move the head to point to the new node */
(*head_reference) = new_node;
}
/* This is a function to print nodes in a given linked list */
void printList(struct node *node)
{
while (node!=NULL)
{
printf("%d ", node->data);
node = node->next;
}
}
/*This is the driver program to test above functions*/
int main()
{
/* let us start with the empty list */
struct node* result = NULL;
struct node* a = NULL;
struct node* b = NULL;
/* Firstly we will create two sorted linked lists to test
the functions
Created lists, a: 5->12->15, b: 2->4->18 */
insertpush(&a, 15);
insertpush(&a, 12);
insertpush(&a, 5);
push(&b, 20);
push(&b, 3);
push(&b, 2);
/* now we will be removing duplicates from linked list */
res = letssortlists(a, b);
printf("Merged Linked List is:  ");
printList(result);
return 0;
}
OUTPUT:
Merged linked list is:
2 3 5 12 15 18

More Related Content

Similar to Use C++ Write a function to merge two doubly linked lists. The input.pdf

in Java (ignore the last line thats hidden) Create a doubly linked l.pdf
in Java (ignore the last line thats hidden) Create a doubly linked l.pdfin Java (ignore the last line thats hidden) Create a doubly linked l.pdf
in Java (ignore the last line thats hidden) Create a doubly linked l.pdfsauravmanwanicp
 
Program to insert in a sorted list #includestdio.h#include.pdf
 Program to insert in a sorted list #includestdio.h#include.pdf Program to insert in a sorted list #includestdio.h#include.pdf
Program to insert in a sorted list #includestdio.h#include.pdfsudhirchourasia86
 
LabProgram.javaimport java.util.NoSuchElementException;public .pdf
LabProgram.javaimport java.util.NoSuchElementException;public .pdfLabProgram.javaimport java.util.NoSuchElementException;public .pdf
LabProgram.javaimport java.util.NoSuchElementException;public .pdffantasiatheoutofthef
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdffeelinggift
 
File LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdfFile LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdfConint29
 
Copy your completed LinkedList class from Lab 3 into the LinkedList..pdf
Copy your completed LinkedList class from Lab 3 into the LinkedList..pdfCopy your completed LinkedList class from Lab 3 into the LinkedList..pdf
Copy your completed LinkedList class from Lab 3 into the LinkedList..pdffacevenky
 
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfC++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfpoblettesedanoree498
 
Write a program in C that does the followinga) Builds a simple li.pdf
Write a program in C that does the followinga) Builds a simple li.pdfWrite a program in C that does the followinga) Builds a simple li.pdf
Write a program in C that does the followinga) Builds a simple li.pdfkavithaarp
 
write recursive function that calculates and returns the length of a.pdf
write recursive function that calculates and returns the length of a.pdfwrite recursive function that calculates and returns the length of a.pdf
write recursive function that calculates and returns the length of a.pdfarpitcomputronics
 
Write a C++ function that delete nodes in a doubly linkedlist- It shou.docx
Write a C++ function that delete nodes in a doubly linkedlist- It shou.docxWrite a C++ function that delete nodes in a doubly linkedlist- It shou.docx
Write a C++ function that delete nodes in a doubly linkedlist- It shou.docxnoreendchesterton753
 
C++ Please write the whole code that is needed for this assignment- wr.docx
C++ Please write the whole code that is needed for this assignment- wr.docxC++ Please write the whole code that is needed for this assignment- wr.docx
C++ Please write the whole code that is needed for this assignment- wr.docxBrianGHiNewmanv
 
A)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdfA)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdfanton291
 
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdfC++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdfcallawaycorb73779
 
C program to insert a node in doubly linked list
C program to insert a node in doubly linked listC program to insert a node in doubly linked list
C program to insert a node in doubly linked listSourav Gayen
 
Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfLec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfKylaMaeGarcia1
 
package linkedLists- import java-util-Iterator- --- A class representi.pdf
package linkedLists- import java-util-Iterator- --- A class representi.pdfpackage linkedLists- import java-util-Iterator- --- A class representi.pdf
package linkedLists- import java-util-Iterator- --- A class representi.pdfarcellzone
 

Similar to Use C++ Write a function to merge two doubly linked lists. The input.pdf (20)

in Java (ignore the last line thats hidden) Create a doubly linked l.pdf
in Java (ignore the last line thats hidden) Create a doubly linked l.pdfin Java (ignore the last line thats hidden) Create a doubly linked l.pdf
in Java (ignore the last line thats hidden) Create a doubly linked l.pdf
 
Program to insert in a sorted list #includestdio.h#include.pdf
 Program to insert in a sorted list #includestdio.h#include.pdf Program to insert in a sorted list #includestdio.h#include.pdf
Program to insert in a sorted list #includestdio.h#include.pdf
 
LabProgram.javaimport java.util.NoSuchElementException;public .pdf
LabProgram.javaimport java.util.NoSuchElementException;public .pdfLabProgram.javaimport java.util.NoSuchElementException;public .pdf
LabProgram.javaimport java.util.NoSuchElementException;public .pdf
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
 
File LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdfFile LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdf
 
Copy your completed LinkedList class from Lab 3 into the LinkedList..pdf
Copy your completed LinkedList class from Lab 3 into the LinkedList..pdfCopy your completed LinkedList class from Lab 3 into the LinkedList..pdf
Copy your completed LinkedList class from Lab 3 into the LinkedList..pdf
 
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfC++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
 
Write a program in C that does the followinga) Builds a simple li.pdf
Write a program in C that does the followinga) Builds a simple li.pdfWrite a program in C that does the followinga) Builds a simple li.pdf
Write a program in C that does the followinga) Builds a simple li.pdf
 
write recursive function that calculates and returns the length of a.pdf
write recursive function that calculates and returns the length of a.pdfwrite recursive function that calculates and returns the length of a.pdf
write recursive function that calculates and returns the length of a.pdf
 
Write a C++ function that delete nodes in a doubly linkedlist- It shou.docx
Write a C++ function that delete nodes in a doubly linkedlist- It shou.docxWrite a C++ function that delete nodes in a doubly linkedlist- It shou.docx
Write a C++ function that delete nodes in a doubly linkedlist- It shou.docx
 
C++ Please write the whole code that is needed for this assignment- wr.docx
C++ Please write the whole code that is needed for this assignment- wr.docxC++ Please write the whole code that is needed for this assignment- wr.docx
C++ Please write the whole code that is needed for this assignment- wr.docx
 
Linked list
Linked listLinked list
Linked list
 
A)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdfA)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdf
 
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdfC++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
 
C program to insert a node in doubly linked list
C program to insert a node in doubly linked listC program to insert a node in doubly linked list
C program to insert a node in doubly linked list
 
Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfLec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdf
 
Unit II Data Structure 2hr topic - List - Operations.pptx
Unit II  Data Structure 2hr topic - List - Operations.pptxUnit II  Data Structure 2hr topic - List - Operations.pptx
Unit II Data Structure 2hr topic - List - Operations.pptx
 
C Exam Help
C Exam Help C Exam Help
C Exam Help
 
C Homework Help
C Homework HelpC Homework Help
C Homework Help
 
package linkedLists- import java-util-Iterator- --- A class representi.pdf
package linkedLists- import java-util-Iterator- --- A class representi.pdfpackage linkedLists- import java-util-Iterator- --- A class representi.pdf
package linkedLists- import java-util-Iterator- --- A class representi.pdf
 

More from shalins6

Describe two benefits of the industrial revolution and two proble.pdf
Describe two benefits of the industrial revolution and two proble.pdfDescribe two benefits of the industrial revolution and two proble.pdf
Describe two benefits of the industrial revolution and two proble.pdfshalins6
 
Dee Dees parents place a high value on academic achievement, but h.pdf
Dee Dees parents place a high value on academic achievement, but h.pdfDee Dees parents place a high value on academic achievement, but h.pdf
Dee Dees parents place a high value on academic achievement, but h.pdfshalins6
 
An inter satellite link is established between two satellites. One sa.pdf
An inter satellite link is established between two satellites. One sa.pdfAn inter satellite link is established between two satellites. One sa.pdf
An inter satellite link is established between two satellites. One sa.pdfshalins6
 
Circle the correct words At fertilization, eggs and sperm carrying s.pdf
Circle the correct words  At fertilization, eggs and sperm carrying s.pdfCircle the correct words  At fertilization, eggs and sperm carrying s.pdf
Circle the correct words At fertilization, eggs and sperm carrying s.pdfshalins6
 
A small object is 25.0 cm from a diverging lens as shown in the figu.pdf
A small object is 25.0 cm from a diverging lens as shown in the figu.pdfA small object is 25.0 cm from a diverging lens as shown in the figu.pdf
A small object is 25.0 cm from a diverging lens as shown in the figu.pdfshalins6
 
Cautions about experimentation and samplingSolutionWhen exper.pdf
Cautions about experimentation and samplingSolutionWhen exper.pdfCautions about experimentation and samplingSolutionWhen exper.pdf
Cautions about experimentation and samplingSolutionWhen exper.pdfshalins6
 
CH30uz Lecture Which of the f.pdf
CH30uz Lecture Which of the f.pdfCH30uz Lecture Which of the f.pdf
CH30uz Lecture Which of the f.pdfshalins6
 
Below is a code segment for the Deleteltem function in an unsorted li.pdf
Below is a code segment for the Deleteltem function in an unsorted li.pdfBelow is a code segment for the Deleteltem function in an unsorted li.pdf
Below is a code segment for the Deleteltem function in an unsorted li.pdfshalins6
 
A parts assembly station on a production line exhibits a severe vibra.pdf
A parts assembly station on a production line exhibits a severe vibra.pdfA parts assembly station on a production line exhibits a severe vibra.pdf
A parts assembly station on a production line exhibits a severe vibra.pdfshalins6
 
You are conducting a study investigating the effects of a high-fat d.pdf
You are conducting a study investigating the effects of a high-fat d.pdfYou are conducting a study investigating the effects of a high-fat d.pdf
You are conducting a study investigating the effects of a high-fat d.pdfshalins6
 
Which of the following is not a premise of the Koch’s postulatesA.pdf
Which of the following is not a premise of the Koch’s postulatesA.pdfWhich of the following is not a premise of the Koch’s postulatesA.pdf
Which of the following is not a premise of the Koch’s postulatesA.pdfshalins6
 
What is inclusionSolutioninclusions in biology means the foll.pdf
What is inclusionSolutioninclusions in biology means the foll.pdfWhat is inclusionSolutioninclusions in biology means the foll.pdf
What is inclusionSolutioninclusions in biology means the foll.pdfshalins6
 
What organ systems that work with the digestive system to deliver nu.pdf
What organ systems that work with the digestive system to deliver nu.pdfWhat organ systems that work with the digestive system to deliver nu.pdf
What organ systems that work with the digestive system to deliver nu.pdfshalins6
 
What are the major design considerations for secondary clarifers fol.pdf
What are the major design considerations for secondary clarifers fol.pdfWhat are the major design considerations for secondary clarifers fol.pdf
What are the major design considerations for secondary clarifers fol.pdfshalins6
 
What is meant by composite pavement What is meant by composit.pdf
What is meant by composite pavement  What is meant by composit.pdfWhat is meant by composite pavement  What is meant by composit.pdf
What is meant by composite pavement What is meant by composit.pdfshalins6
 
Using an intensity of 1 × 10-12 Wm2 as a reference, the threshold o.pdf
Using an intensity of 1 × 10-12 Wm2 as a reference, the threshold o.pdfUsing an intensity of 1 × 10-12 Wm2 as a reference, the threshold o.pdf
Using an intensity of 1 × 10-12 Wm2 as a reference, the threshold o.pdfshalins6
 
Tissue regeneration is an emerging and exciting biomedical field. As.pdf
Tissue regeneration is an emerging and exciting biomedical field. As.pdfTissue regeneration is an emerging and exciting biomedical field. As.pdf
Tissue regeneration is an emerging and exciting biomedical field. As.pdfshalins6
 
The preeminent American naturalist of the late 1800s was Charles Dar.pdf
The preeminent American naturalist of the late 1800s was  Charles Dar.pdfThe preeminent American naturalist of the late 1800s was  Charles Dar.pdf
The preeminent American naturalist of the late 1800s was Charles Dar.pdfshalins6
 
The LS and LF are calculated using thebackward pass through the ne.pdf
The LS and LF are calculated using thebackward pass through the ne.pdfThe LS and LF are calculated using thebackward pass through the ne.pdf
The LS and LF are calculated using thebackward pass through the ne.pdfshalins6
 
the politics of the gilded age failed to deal with the critical soci.pdf
the politics of the gilded age failed to deal with the critical soci.pdfthe politics of the gilded age failed to deal with the critical soci.pdf
the politics of the gilded age failed to deal with the critical soci.pdfshalins6
 

More from shalins6 (20)

Describe two benefits of the industrial revolution and two proble.pdf
Describe two benefits of the industrial revolution and two proble.pdfDescribe two benefits of the industrial revolution and two proble.pdf
Describe two benefits of the industrial revolution and two proble.pdf
 
Dee Dees parents place a high value on academic achievement, but h.pdf
Dee Dees parents place a high value on academic achievement, but h.pdfDee Dees parents place a high value on academic achievement, but h.pdf
Dee Dees parents place a high value on academic achievement, but h.pdf
 
An inter satellite link is established between two satellites. One sa.pdf
An inter satellite link is established between two satellites. One sa.pdfAn inter satellite link is established between two satellites. One sa.pdf
An inter satellite link is established between two satellites. One sa.pdf
 
Circle the correct words At fertilization, eggs and sperm carrying s.pdf
Circle the correct words  At fertilization, eggs and sperm carrying s.pdfCircle the correct words  At fertilization, eggs and sperm carrying s.pdf
Circle the correct words At fertilization, eggs and sperm carrying s.pdf
 
A small object is 25.0 cm from a diverging lens as shown in the figu.pdf
A small object is 25.0 cm from a diverging lens as shown in the figu.pdfA small object is 25.0 cm from a diverging lens as shown in the figu.pdf
A small object is 25.0 cm from a diverging lens as shown in the figu.pdf
 
Cautions about experimentation and samplingSolutionWhen exper.pdf
Cautions about experimentation and samplingSolutionWhen exper.pdfCautions about experimentation and samplingSolutionWhen exper.pdf
Cautions about experimentation and samplingSolutionWhen exper.pdf
 
CH30uz Lecture Which of the f.pdf
CH30uz Lecture Which of the f.pdfCH30uz Lecture Which of the f.pdf
CH30uz Lecture Which of the f.pdf
 
Below is a code segment for the Deleteltem function in an unsorted li.pdf
Below is a code segment for the Deleteltem function in an unsorted li.pdfBelow is a code segment for the Deleteltem function in an unsorted li.pdf
Below is a code segment for the Deleteltem function in an unsorted li.pdf
 
A parts assembly station on a production line exhibits a severe vibra.pdf
A parts assembly station on a production line exhibits a severe vibra.pdfA parts assembly station on a production line exhibits a severe vibra.pdf
A parts assembly station on a production line exhibits a severe vibra.pdf
 
You are conducting a study investigating the effects of a high-fat d.pdf
You are conducting a study investigating the effects of a high-fat d.pdfYou are conducting a study investigating the effects of a high-fat d.pdf
You are conducting a study investigating the effects of a high-fat d.pdf
 
Which of the following is not a premise of the Koch’s postulatesA.pdf
Which of the following is not a premise of the Koch’s postulatesA.pdfWhich of the following is not a premise of the Koch’s postulatesA.pdf
Which of the following is not a premise of the Koch’s postulatesA.pdf
 
What is inclusionSolutioninclusions in biology means the foll.pdf
What is inclusionSolutioninclusions in biology means the foll.pdfWhat is inclusionSolutioninclusions in biology means the foll.pdf
What is inclusionSolutioninclusions in biology means the foll.pdf
 
What organ systems that work with the digestive system to deliver nu.pdf
What organ systems that work with the digestive system to deliver nu.pdfWhat organ systems that work with the digestive system to deliver nu.pdf
What organ systems that work with the digestive system to deliver nu.pdf
 
What are the major design considerations for secondary clarifers fol.pdf
What are the major design considerations for secondary clarifers fol.pdfWhat are the major design considerations for secondary clarifers fol.pdf
What are the major design considerations for secondary clarifers fol.pdf
 
What is meant by composite pavement What is meant by composit.pdf
What is meant by composite pavement  What is meant by composit.pdfWhat is meant by composite pavement  What is meant by composit.pdf
What is meant by composite pavement What is meant by composit.pdf
 
Using an intensity of 1 × 10-12 Wm2 as a reference, the threshold o.pdf
Using an intensity of 1 × 10-12 Wm2 as a reference, the threshold o.pdfUsing an intensity of 1 × 10-12 Wm2 as a reference, the threshold o.pdf
Using an intensity of 1 × 10-12 Wm2 as a reference, the threshold o.pdf
 
Tissue regeneration is an emerging and exciting biomedical field. As.pdf
Tissue regeneration is an emerging and exciting biomedical field. As.pdfTissue regeneration is an emerging and exciting biomedical field. As.pdf
Tissue regeneration is an emerging and exciting biomedical field. As.pdf
 
The preeminent American naturalist of the late 1800s was Charles Dar.pdf
The preeminent American naturalist of the late 1800s was  Charles Dar.pdfThe preeminent American naturalist of the late 1800s was  Charles Dar.pdf
The preeminent American naturalist of the late 1800s was Charles Dar.pdf
 
The LS and LF are calculated using thebackward pass through the ne.pdf
The LS and LF are calculated using thebackward pass through the ne.pdfThe LS and LF are calculated using thebackward pass through the ne.pdf
The LS and LF are calculated using thebackward pass through the ne.pdf
 
the politics of the gilded age failed to deal with the critical soci.pdf
the politics of the gilded age failed to deal with the critical soci.pdfthe politics of the gilded age failed to deal with the critical soci.pdf
the politics of the gilded age failed to deal with the critical soci.pdf
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonhttgc7rh9c
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfstareducators107
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningMarc Dusseiller Dusjagr
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17Celine George
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111GangaMaiya1
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxakanksha16arora
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfNirmal Dwivedi
 

Recently uploaded (20)

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
Our Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdfOur Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdf
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
 

Use C++ Write a function to merge two doubly linked lists. The input.pdf

  • 1. Use C++ Write a function to merge two doubly linked lists. The input lists have their elements in sorted order, from lowest to highest. The output list should also be sorted from lowest to highest. Your algorithm should run in linear time on the length of the output list. Provide an algorithm for your function Implement and show some samples of your running function Solution /* This is a C++ program to merge two sorted linked lists and produce a list in a Sorted order */ #include #include #include /* Link list node */ struct node { int data; struct node* next; }; /* This function is used to pull off the front node of the source and put it in destnation */ void letusMoveNode(struct node** destRef, struct node** sourceRef); /* This is a function used for performing merging of two linked lists.It takes two lists sorted in increasing order, and splices their nodes together to make one big sorted list which is returned. */ struct node* letssortlists(struct node* a, struct node* b) { /* Let us first have dummyone first node to hang the result on */ struct node dummyone; /*next the tail points to the last result node */ struct node* tail = &dummyone; /* As a result, tail->next is the place to add new nodes to the result. */ dummyone.next = NULL; while (1) {
  • 2. if (a == NULL) { /*we will check that 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) letusMoveNode(&(tail->next), &a); else letusMoveNode(&(tail->next), &b); tail = tail->next; } return(dummyone.next); } /* letusMoveNode() function is a function which takes the node from the front of the source, and move it to the front of the dest. Before calling letusMoveNode(): source == {1, 2, 3} dest == {1, 2, 3} Affter calling letusMoveNode(): source == {3, 4} dest == {1, 2, 3, 4} */ void letusMoveNode(struct node** destReference, struct node** sourceReference) { /*This is the front source node */ struct node* newNode = *sourceReference; assert(newNode != NULL); /* now we will advance the source pointer */ *sourceReference = newNode->next; /*next we will link the old dest off the new node */
  • 3. newNode->next = *destReference; /* Fineally we will move dest to point to the new node */ *destReference = newNode; } /* This IS A function which is used to insert a node at the beginning of the linked list */ void insertpush(struct node** head_reference, int new_data) { /* This is used to allocate node */ struct node* new_node = (struct node*) malloc(sizeof(struct node)); /* now we will put in the data */ new_node->data = new_data; /* The next step is to link the old list off the new node */ new_node->next = (*head_reference); /*Finally we will move the head to point to the new node */ (*head_reference) = new_node; } /* This is a function to print nodes in a given linked list */ void printList(struct node *node) { while (node!=NULL) { printf("%d ", node->data); node = node->next; } } /*This is the driver program to test above functions*/ int main() { /* let us start with the empty list */ struct node* result = NULL; struct node* a = NULL; struct node* b = NULL; /* Firstly we will create two sorted linked lists to test the functions Created lists, a: 5->12->15, b: 2->4->18 */
  • 4. insertpush(&a, 15); insertpush(&a, 12); insertpush(&a, 5); push(&b, 20); push(&b, 3); push(&b, 2); /* now we will be removing duplicates from linked list */ res = letssortlists(a, b); printf("Merged Linked List is: "); printList(result); return 0; } OUTPUT: Merged linked list is: 2 3 5 12 15 18