Add to Favorites    Make Home Page 87 Online  

 Language Categories  
 Our Services  

Home » C++ Home » Data Structures Home » Merge Two Sorted Linked List To Form A Third LInked List

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

Search Projects & Source Codes:

Title Merge Two Sorted Linked List To Form A Third LInked List
Author Arun Vishnu
Author Email arun_vishnu [at] hotmail.com
Description Merge Two Sorted Linked List To Form A Third LInked List

Category C++ » Data Structures
Hits 11833
Code Select and Copy the Code
Code : /* Merge two oredered linked list to form a 3rd sorted list */ #include<iostream.h> #include<conio.h> #include<process.h> // Creating a NODE Structure struct node { int data; // data struct node *next; // link to next node and previous node }; // Creating a class LIST class list { struct node *start; public: void create(); // to create a list void show(); // show void merge(list,list); // Merge two list's }; // Main function int main() { clrscr(); list l1,l2,l3; cout<<"Enter the First List in ascending order. "; l1.create(); // to create a first list cout<<" Enter the Second List in ascending order. "; l2.create(); // to create a second list cout<<" The first list is "; l1.show(); cout<<" The second list is "; l2.show(); l3.merge(l1,l2); l3.show(); getch(); return (0); } // Functions // Creating a new node void list::create() { struct node *nxt_node,*pre_node; int value,no,i; start=nxt_node=pre_node=NULL; cout<<" How many nodes : "; cin>>no; cout<<"Enter "<<no<<" Elements: "; for(i=1;i<=no;i++) { cin>>value; nxt_node=new node; nxt_node->data=value; nxt_node->next=NULL; if(start==NULL) start=nxt_node; else pre_node->next=nxt_node; pre_node=nxt_node; } cout<<" The list is created! "; } // Displaying LIST void list::show() { struct node *ptr=start; cout<<" The List is "; while(ptr!=NULL) { cout<<ptr->data<<" -> "; ptr=ptr->next; } cout<<" "; } void list::merge(list l1,list l2) { struct node *nxt_node,*pre_node,*pptr,*qptr; int dat; pptr=l1.start; qptr=l2.start; start=nxt_node=pre_node=NULL; while(pptr!=NULL && qptr!=NULL) { if(pptr->data<=qptr->data) { dat=pptr->data; pptr=pptr->next; } else { dat=qptr->data; qptr=qptr->next; } nxt_node=new node; nxt_node->data=dat; nxt_node->next=NULL; if(start==NULL) start=nxt_node; else pre_node->next=nxt_node; pre_node=nxt_node; } if(pptr==NULL) { while(qptr!=NULL) { nxt_node=new node; nxt_node->data=qptr->data; nxt_node->next=NULL; if(start==NULL) start=nxt_node; else pre_node->next=nxt_node; pre_node=nxt_node; qptr=qptr->next; } } else if(qptr==NULL) { while(pptr!=NULL) { nxt_node=new node; nxt_node->data=pptr->data; nxt_node->next=NULL; if(start==NULL) start=nxt_node; else pre_node->next=nxt_node; pre_node=nxt_node; pptr=pptr->next; } } cout<<" The lists are merged."; return; }

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
Copyright ©2003-2014 SourceCodesWorld.com, All Rights Reserved.
Page URL: http://www.sourcecodesworld.com/source/show.asp?ScriptID=869


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