// MERGING TWO DOUBLY LINKED LISTS (ASCENDING ORDER)
# include<iostream.h>
# include <stdio.h>
# include <alloc.h>
struct Double
{
int info;
struct Double *next;
struct Double *previous;
};
class D_insert
{
public: int i;
Double start, start1, *new1, *local;
public:
void Doubly_insertion (Double * );
void Doubly_Create (Double * );
void Display (Double *);
};
// Function create a list of five nodes
void D_insert :: Doubly_Create (Double *node)
{
start.next = NULL; // Empty list
start.previous = NULL;
node = &start; // Point to the start of the list
for (i = 1; i < 10; i += 2)
{
node->next = (Double *) malloc(sizeof(Double ));
node->next->previous = node;
node = node->next;
node->info = i;
node->next = NULL;
}
}
void D_insert :: Doubly_insertion (Double *node)
{
Double *new1;
start1.next = NULL; // Empty list
start1.previous = NULL;
new1 = &start1; // Point to the start of the list
for (i = 2; i <= 10; i += 2)
{
new1->next = (Double *) malloc(sizeof(Double ));
new1->next->previous = new1;
new1= new1->next;
new1->info = i;
new1->next = NULL;
}
new1 = start1.next;
cout<<"\n Second list is as follows";
while(new1)
{
cout<<"\n "<<new1;
cout<<" "<<new1->info;
new1 = new1->next;
}
new1 = start1.next;
while(new1)
{
int found = 0;
local = (Double *) malloc(sizeof(Double));
local = new1;
new1 = new1->next;
node = start.next;
do
{
if ( node->info > local->info )
{
local->next = node;
local->previous = node->previous;
node->previous->next = local;
node->previous = local;
found = 1;
break;
}
else
node = node->next;
} while ((node->next) && (! found));
if (! found)
if (node->info> local->info)
{
local->next = node;
local->previous = node->previous;
node->previous->next = local;
node->previous = local;
}
else
{
local->next = NULL;
local->previous = node;
node->next = local;
}
}
}
// Display the list
void D_insert :: Display (Double *node)
{
node = start.next;
while (node)
{
cout<<"\n "<<node;
cout<<" " <<node->info;
node = node->next;
}
}
void main()
{
D_insert D_ins;
Double *node = (Double *) malloc(sizeof(Double));
D_ins.Doubly_Create (node);
cout<<"\n First list is as follows\n";
D_ins.Display (node);
D_ins.Doubly_insertion (node);
cout<<"\n List after merging above two lists (Ascending order)\n";
D_ins.Display (node);
}
# include<iostream.h>
# include <stdio.h>
# include <alloc.h>
struct Double
{
int info;
struct Double *next;
struct Double *previous;
};
class D_insert
{
public: int i;
Double start, start1, *new1, *local;
public:
void Doubly_insertion (Double * );
void Doubly_Create (Double * );
void Display (Double *);
};
// Function create a list of five nodes
void D_insert :: Doubly_Create (Double *node)
{
start.next = NULL; // Empty list
start.previous = NULL;
node = &start; // Point to the start of the list
for (i = 1; i < 10; i += 2)
{
node->next = (Double *) malloc(sizeof(Double ));
node->next->previous = node;
node = node->next;
node->info = i;
node->next = NULL;
}
}
void D_insert :: Doubly_insertion (Double *node)
{
Double *new1;
start1.next = NULL; // Empty list
start1.previous = NULL;
new1 = &start1; // Point to the start of the list
for (i = 2; i <= 10; i += 2)
{
new1->next = (Double *) malloc(sizeof(Double ));
new1->next->previous = new1;
new1= new1->next;
new1->info = i;
new1->next = NULL;
}
new1 = start1.next;
cout<<"\n Second list is as follows";
while(new1)
{
cout<<"\n "<<new1;
cout<<" "<<new1->info;
new1 = new1->next;
}
new1 = start1.next;
while(new1)
{
int found = 0;
local = (Double *) malloc(sizeof(Double));
local = new1;
new1 = new1->next;
node = start.next;
do
{
if ( node->info > local->info )
{
local->next = node;
local->previous = node->previous;
node->previous->next = local;
node->previous = local;
found = 1;
break;
}
else
node = node->next;
} while ((node->next) && (! found));
if (! found)
if (node->info> local->info)
{
local->next = node;
local->previous = node->previous;
node->previous->next = local;
node->previous = local;
}
else
{
local->next = NULL;
local->previous = node;
node->next = local;
}
}
}
// Display the list
void D_insert :: Display (Double *node)
{
node = start.next;
while (node)
{
cout<<"\n "<<node;
cout<<" " <<node->info;
node = node->next;
}
}
void main()
{
D_insert D_ins;
Double *node = (Double *) malloc(sizeof(Double));
D_ins.Doubly_Create (node);
cout<<"\n First list is as follows\n";
D_ins.Display (node);
D_ins.Doubly_insertion (node);
cout<<"\n List after merging above two lists (Ascending order)\n";
D_ins.Display (node);
}
No comments:
Post a Comment