Add to Favorites    Make Home Page 1043 Online  
 Language Categories  
 Our Services  

Home » C++ Home » Data Structures Home » Merging two doubly-linked-lists into another

A D V E R T I S E M E N T

Search Projects & Source Codes:

Title Merging two doubly-linked-lists into another
Author Mokopela Tsepe
Author Email eliatsepemokopela [at] yahoo.com
Description This program will take two doubly-linked lists of nodes and merges them into another doubly-linked list of nodes.
Category C++ » Data Structures
Hits 396833
Code Select and Copy the Code
Code : #include<iostream> using namespace std; class Node { public: int data; Node *next; Node *prev; Node() { data=0; next=prev=NULL; } }; class Node1 { public: int data; Node1 *next; Node1 *prev; Node1() { data=0; next=prev=NULL; } }; class Node2 { public: int data; Node2 *next; Node2 *prev; Node2() { data=0; next=prev=NULL; } }; class DoublyLinkedList { private: Node *headNode, *tailnode; Node1 *headNode1, *tailNode1; Node2 *headNode2, *tailNode2; public: DoublyLinkedList() { headNode=tailNode=NULL; headNode1=tailNode1=NULL; headNode2=tailNode2=NULL; } void Insert();//Insert data into the two lists void Merge() ;//Merging two lists into one void Display();//Display only the merged list }; void DoublyLinkedList::Insert() { char option; //This section inserts elements into the nodes of the first list do { Node *newnode = new Node(); cin>>newnode->data; newnode->next=NULL; newnode->prev=NULL; if(headNode==NULL) { headNode= tailNode=newnode; } else { Node *curr = headNode; while(curr->next!=NULL) { curr=curr->next; } curr->next=tailNode=newnode; newnode->prev=curr; } cout<<"Enter y to continue adding data into the first list :"; cin>>option; } while(option=='y'||option=='Y'); //The section inserts the elements into the nodes of the second list do { Node1 *newnode = new Node1(); cin>>newnode->data; newnode->next=NULL; newnode->prev=NULL; if(headNode1==NULL) { headNode1=tailNode1=newnode; } else { Node1 *curr = headNode1; while(curr->next!=NULL) { curr=curr->next; } curr->next= tailNode1=newnode; newnode->prev=curr; } cout<<"Enter y to continue adding data into the second list :"; cin>>option; } while(option=='y'||option=='Y'); } void DoublyLinkedList::Merge() { Node *currentNode=headNode; Node1 *currentNode1=headNode1; //This section of code copies all the data from list 1 into the new list while(currentNode!=NULL) { Node2 *newnode = new Node2(); newnode->data = currentNode->data; newnode->next = NULL; newnode->prev = NULL; if(headNode2==NULL) { headNode2=tailNode2=newnode; } else { Node2 *temp = headNode2; while(temp->next!=NULL) { temp=temp->next; } temp->next=tailNode2=newnode; newnode->prev=temp; } currentNode=currentNode->next; } //This section of code appends the new list with data from the second list while(currentNode1!=NULL) { Node2 *newnode = new Node2(); newnode->data = currentNode1->data; newnode->next = newnode->prev=NULL; if(tailNode2->next==NULL) { tailNode2->next=newnode; newnode->prev = tailNode2; tailNode2=newnode; } currentNode1= currentNode1->next; } } void DoublyLinkedList::Display() { Node2 *currentNode2 = headNode2; while(currentNode2!=NULL) { cout<<currentNode2->data<<" "; currentNode2=currentNode2->next; } cout<<endl; } void main() { DoublyLinkedList DLL; DLL.Insert(); DLL.Merge(); DLL.Display(); }

Related Source Codes

Script Name Author
Moving ball screen saver karlmarx
The Classic Game of Snake & Ladder Lakshmi Narayana .A
Railway seat reservation question which comes in sapient VyomWorld
To calculate percentile Ravi Mathur
Send to folder ANIMESH SAHU
Analog clock and calendar Nazia & Rida
HIGH/LOW GAME MOLLY ARORA
Data structure (stack Implimentation) Swapnil B Adsure
Memory Game AnirudhSanyal
Easy Calc Anirudh Sanyal
GK Quiz Anirudh Sanyal
Hangman Game Manish Jain
Snakeman Manish Jain
Full month Calendar Nigi
Cursor shapes nigi

A D V E R T I S E M E N T




Google Groups Subscribe to SourceCodesWorld - Techies Talk
Email:

Free eBook - Interview Questions: Get over 1,000 Interview Questions in an eBook for free when you join JobsAssist. Just click on the button below to join JobsAssist and you will immediately receive the Free eBook with thousands of Interview Questions in an ebook when you join.

New! Click here to Add your Code!


ASP Home | C Home | C++ Home | COBOL Home | Java Home | Pascal Home
Source Codes Home Page

 Advertisements  

Google Search

Google

Source Codes World.com is a part of Vyom Network.

Vyom Network : Web Hosting | Dedicated Server | Free SMS, GRE, GMAT, MBA | Online Exams | Freshers Jobs | Software Downloads | Interview Questions | Jobs, Discussions | Placement Papers | Free eBooks | Free eBooks | Free Business Info | Interview Questions | Free Tutorials | Arabic, French, German | IAS Preparation | Jokes, Songs, Fun | Free Classifieds | Free Recipes | Free Downloads | Bangalore Info | Tech Solutions | Project Outsourcing, Web Hosting | GATE Preparation | MBA Preparation | SAP Info | Software Testing | Google Logo Maker | Freshers Jobs

Sitemap | Privacy Policy | Terms and Conditions | Important Websites
Copyright ©2003-2024 SourceCodesWorld.com, All Rights Reserved.
Page URL: http://www.sourcecodesworld.com/source/show.asp?ScriptID=1212


Download Yahoo Messenger | Placement Papers | Free SMS | C Interview Questions | C++ Interview Questions | Quick2Host Review