SlideShare a Scribd company logo
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.pdf
sauravmanwanicp
 
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
sudhirchourasia86
 
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
fantasiatheoutofthef
 
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
feelinggift
 
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
Conint29
 
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
facevenky
 
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
poblettesedanoree498
 
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
kavithaarp
 
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
arpitcomputronics
 
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
noreendchesterton753
 
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
BrianGHiNewmanv
 
Linked list
Linked listLinked list
Linked list
Md. Afif Al Mamun
 
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
anton291
 
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
callawaycorb73779
 
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
Sourav Gayen
 
Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfLec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdf
KylaMaeGarcia1
 
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
Mani .S (Specialization in Semantic Web)
 
C Exam Help
C Exam Help C Exam Help
C Exam Help
Programming Exam Help
 
C Homework Help
C Homework HelpC Homework Help
C Homework Help
Programming 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
arcellzone
 

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.pdf
shalins6
 
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
shalins6
 
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
shalins6
 
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
shalins6
 
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
shalins6
 
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
shalins6
 
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
shalins6
 
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
shalins6
 
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
shalins6
 
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
shalins6
 
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
shalins6
 
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
shalins6
 
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
shalins6
 
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
shalins6
 
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
shalins6
 
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
shalins6
 
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
shalins6
 
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
shalins6
 
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
shalins6
 
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
shalins6
 

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

Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 

Recently uploaded (20)

Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 

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