Appreciate if you can help:Problem in "merge list" for linked list.

I have done a program but i couldn't run it smoothly at the part of merge list.
Can anyone please help me ?

below is the program i did.


#include <iostream>

using namespace std;

class linkedList{
private:
struct node{
int info;
node *link;
}*first;


public:
void newData(int &);
void deleteData(int &);
void printData();
void splitData();
void mergeData();
linkedList();
node *list1;
node *list2;

};

int main(){
linkedList list;
int choice,num;
int p;

cout<<"1.Add one number."<<endl;
cout<<"2.Delete one number."<<endl;
cout<<"3.List forward."<<endl;
cout<<"4.Splitting list."<<endl;
cout<<"5.Merge list."<<endl;
cout<<"6.Exit"<<endl;

cout<<"Enter your choice: ";
cin>>choice;

while (choice!=6)
{
switch (choice)
{
case 1:
cout<<"Add an integer:";
cin>>num;
list.newData(num);
break;
case 2:
cout<<"Delete an integer:";
cin>>num;
list.deleteData(num);
break;
case 3:
list.printData();
break;
case 4:
list.splitData();
break;
case 5:
list.mergeData();
break;
default:
cout<<"Invalid choice!"<<endl;
}
cout<<"Enter your choice :";
cin>>choice;
}

return 0;
}

linkedList::linkedList()
{
first=NULL;
}

void linkedList::newData(int &nm){
node *newNode,*temp;

newNode=new node;
newNode->info=nm;
newNode->link=NULL;

if(first==NULL){
first=newNode;
}
else
{
temp=first;
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->link=newNode;
}
}

void linkedList::deleteData(int &nm){
node *temp,*temp2;

temp=first;
if(temp->info==nm)
{
first=first->link;
delete temp;
return;
}
while(temp!=NULL)
{
if(temp->info==nm)
{
temp2->link=temp->link;
delete temp;
return;
}
temp2=temp;
temp=temp->link;
}
cout<<"Integer "<<nm<<" not found"<<endl;
}

void linkedList::printData()
{ node *temp;

if(first==NULL)
{
cout<<"Empty"<<endl;
}

temp=first;
while(temp!=NULL)
{
cout<<temp->info<<endl;
temp=temp->link;
}
}

void linkedList::splitData()
{ int pos;

cout<<"Enter the position to split list:";
cin>>pos;

node *temp;
temp=first;
for (int i=0;i<pos;i++)
{
if(temp==NULL)
{
cout<<"No integer in the list"<<endl;
return;
}
else
{
temp=temp->link;
}
}
list2=temp->link;
temp->link=NULL;

list1=first;
cout<<"First list:"<<endl;

while(list1!=NULL)
{ cout<<list1->info<<endl;
list1=list1->link;
}
cout<<endl;

cout<<"Second list:"<<endl;
if(list2==NULL)
{
cout<<"No integer in the list"<<endl;
}
else{
while(list2!=NULL){
cout<<list2->info<<endl;
list2=list2->link;
}
}
}
void linkedList::mergeData()
{ node *temp;
temp=list1;
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->link=list2;
temp=list1;

while(temp!=NULL){
cout<<temp->info<<endl;
temp=temp->link;
}
}

Last edited on
Topic archived. No new replies allowed.