deepak848207@gmail.com
#include<iostream>
using namespace std;
struct node
{
int data;
struct node *next;
};
class _list_
{
struct node *start1,*start2,*start3,*start4;
public:
_list_()
{
start1=NULL;
start2=NULL;
start3=NULL;
start4=NULL;
}
void insert(int);
void display();
void merge_list();
void insert1(int);
void reverse_list();
};
void _list_::reverse_list()
{
struct node *temp;
if(start3!=NULL)
{
while(start3!=NULL)
{
temp=start3;
start3=start3->next;
temp->next=start4;
start4=temp;
}
temp=start4;
while(temp!=NULL)
{
cout<<temp->data<<"->";
temp=temp->next;
}
}
}
void _list_::merge_list()
{
struct node *temp1,*temp2,*p;
temp1=start1;
temp2=start2;
while(temp1!=NULL&&temp2!=NULL)
{
cout<<"hello";
p=(struct node*)malloc(sizeof(struct node));
if(temp1->data>temp2->data)
{
p->data=temp2->data;
temp2=temp2->next;
}
else if(temp1->data<temp2->data)
{
cout<<"hello1";
p->data=temp1->data;
temp1=temp1->next;
}
else
{
cout<<"hello2";
p->data=temp1->data;
temp1=temp1->next;
temp2=temp2->next;
}
p->next=start3;
start3=p;
}
while(temp1!=NULL)
{
p=(struct node*)malloc(sizeof(struct node));
p->next=start3;
p->data=temp1->data;
start3=p;
temp1=temp1->next;
}
while(temp2!=NULL)
{
p=(struct node*)malloc(sizeof(struct node));
p->next=start3;
p->data=temp2->data;
start3=p;
temp2=temp2->next;
}
}
void _list_::display()
{
struct node *temp;
cout<<"\nList 1:\n";
temp=start1;
while(temp!=NULL)
{
cout<<temp->data<<"->";
temp=temp->next;
}
cout<<"\nList 2:\n";
temp=start2;
while(temp!=NULL)
{
cout<<temp->data<<"->";
temp=temp->next;
}
cout<<"\nList 3:\n";
reverse_list();
}
void _list_::insert(int item)
{
cout<<"inside insert\n";
struct node *temp,*p;
p=(struct node*)malloc(sizeof(struct node));
p->data=item;
p->next=NULL;
if(start1==NULL)
{
start1=p;
}
else
{
temp=start1;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=p;
}
}
void _list_::insert1(int item)
{
cout<<"inside insert\n";
struct node *temp,*p;
p=(struct node*)malloc(sizeof(struct node));
p->data=item;
p->next=NULL;
if(start2==NULL)
{
start2=p;
}
else
{
temp=start2;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=p;
}
}
int main()
{
_list_ obj;
int choice=0,item,pos;
while(choice!=3)
{
cout<<"Enter your choice\nEnter 1 for display the list\nEnter 2 for inserting an item in the first list\nEnter 3 for exit\nEnter 4 for inserting an item in the second list\nEnter 5 for Merging the two lists\n";
cin>>choice;
switch(choice)
{
case 1:
obj.display();
break;
case 2:
int item;
cout<<"enter the item\n";
cin>>item;
obj.insert(item);
break;
case 4:
cout<<"enter the item\n";
cin>>item;
obj.insert1(item);
break;
case 5:
obj.merge_list();
break;
}
}
return 0;
}
using namespace std;
struct node
{
int data;
struct node *next;
};
class _list_
{
struct node *start1,*start2,*start3,*start4;
public:
_list_()
{
start1=NULL;
start2=NULL;
start3=NULL;
start4=NULL;
}
void insert(int);
void display();
void merge_list();
void insert1(int);
void reverse_list();
};
void _list_::reverse_list()
{
struct node *temp;
if(start3!=NULL)
{
while(start3!=NULL)
{
temp=start3;
start3=start3->next;
temp->next=start4;
start4=temp;
}
temp=start4;
while(temp!=NULL)
{
cout<<temp->data<<"->";
temp=temp->next;
}
}
}
void _list_::merge_list()
{
struct node *temp1,*temp2,*p;
temp1=start1;
temp2=start2;
while(temp1!=NULL&&temp2!=NULL)
{
cout<<"hello";
p=(struct node*)malloc(sizeof(struct node));
if(temp1->data>temp2->data)
{
p->data=temp2->data;
temp2=temp2->next;
}
else if(temp1->data<temp2->data)
{
cout<<"hello1";
p->data=temp1->data;
temp1=temp1->next;
}
else
{
cout<<"hello2";
p->data=temp1->data;
temp1=temp1->next;
temp2=temp2->next;
}
p->next=start3;
start3=p;
}
while(temp1!=NULL)
{
p=(struct node*)malloc(sizeof(struct node));
p->next=start3;
p->data=temp1->data;
start3=p;
temp1=temp1->next;
}
while(temp2!=NULL)
{
p=(struct node*)malloc(sizeof(struct node));
p->next=start3;
p->data=temp2->data;
start3=p;
temp2=temp2->next;
}
}
void _list_::display()
{
struct node *temp;
cout<<"\nList 1:\n";
temp=start1;
while(temp!=NULL)
{
cout<<temp->data<<"->";
temp=temp->next;
}
cout<<"\nList 2:\n";
temp=start2;
while(temp!=NULL)
{
cout<<temp->data<<"->";
temp=temp->next;
}
cout<<"\nList 3:\n";
reverse_list();
}
void _list_::insert(int item)
{
cout<<"inside insert\n";
struct node *temp,*p;
p=(struct node*)malloc(sizeof(struct node));
p->data=item;
p->next=NULL;
if(start1==NULL)
{
start1=p;
}
else
{
temp=start1;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=p;
}
}
void _list_::insert1(int item)
{
cout<<"inside insert\n";
struct node *temp,*p;
p=(struct node*)malloc(sizeof(struct node));
p->data=item;
p->next=NULL;
if(start2==NULL)
{
start2=p;
}
else
{
temp=start2;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=p;
}
}
int main()
{
_list_ obj;
int choice=0,item,pos;
while(choice!=3)
{
cout<<"Enter your choice\nEnter 1 for display the list\nEnter 2 for inserting an item in the first list\nEnter 3 for exit\nEnter 4 for inserting an item in the second list\nEnter 5 for Merging the two lists\n";
cin>>choice;
switch(choice)
{
case 1:
obj.display();
break;
case 2:
int item;
cout<<"enter the item\n";
cin>>item;
obj.insert(item);
break;
case 4:
cout<<"enter the item\n";
cin>>item;
obj.insert1(item);
break;
case 5:
obj.merge_list();
break;
}
}
return 0;
}
Comments