answersLogoWhite

0


Best Answer

#include<stdio.h>

#include<conio.h>

#define null 0

void create();

void display();

void insert();

void delet();

void erase();

void create2();

void display2();

void insert2();

void delet2();

void erase2();

void link1();

void link2();

void merge();

struct node

{

int info;

struct node *next;

}*start,*ptr,*temp,*prev;

struct node2

{

int info;

struct node2 *nex;

}*start2,*ptr2,*temp2,*prev2;

void main()

{

int op;

clrscr();

do

{

printf("\n1.link1\n2.link2\n3.merge\n4.exit");

printf("\nEnter your option");

scanf("%d",&op);

switch(op)

{

case 1:

link1();

break;

case 2:

link2();

break;

case 3:

merge();

break;

case 4:

break;

default:

printf("\nEnter correct option");

}

}while(op!=4);

getch();

}

void create()

{

int data;

printf("\nEnter the data");

scanf("%d",&data);

do

{

ptr=malloc(sizeof(struct node));

ptr->info=data;

ptr->next=NULL;

if(start==NULL)

start=ptr;

else

{

temp=start;

while(temp->next!=NULL)

{

temp=temp->next;

}

temp->next=ptr;

}

printf("\nEnter the data & Press 0 to terminate");

scanf("%d",&data);

}while(data!=0);

}

void display()

{

temp=start;

if(start==NULL)

printf("\nList is empty");

else

{

printf("\nElements of list:\n");

while(temp->next!=NULL)

{

printf("%d\t",temp->info);

temp=temp->next;

}

printf("%d\t",temp->info);

}

}

void insert()

{

int data,pos;

printf("\nEnter data & position to insert the element");

scanf("d",&data,&pos);

temp=start;

ptr=malloc(sizeof(struct node));

ptr->info=data;

while(temp->next!=NULL)

{

if(temp->info==pos)

{

ptr->next=temp->next;

temp->next=ptr;

break;

}

else

temp=temp->next;

}

if(temp->next==NULL)

{

temp->next=ptr;

ptr->next=NULL;

}

}

void delet()

{

int pos,flag=0;

if(start==NULL)

printf("List is empty");

else

{

printf("Enter element to be deleted");

scanf("%d",&pos);

temp=start;

if(start->info==pos)

{

flag=1;

start=start->next;

free(temp);

}

else

{

while(temp->next!=NULL)

{

prev=temp;

temp=temp->next;

if(temp->info==pos)

{

flag=1;

prev->next=temp->next;

free(temp);

break;

}

}

}

if(flag==0)

printf("\nElement is not present in list");

}

}

void erase()

{

if(start==NULL)

printf("\nList is empty");

else

{

while(start->next!=NULL)

{

temp=start;

start=start->next;

free(temp);

}

temp=start;

start=start->next;

free(temp);

printf("list is erased");

}

}

void create2()

{

int data;

printf("\nEnter the data");

scanf("%d",&data);

do

{

ptr2=malloc(sizeof(struct node2));

ptr2->info=data;

ptr2->nex=NULL;

if(start2==NULL)

start2=ptr2;

else

{

temp2=start2;

while(temp2->nex!=NULL)

{

temp2=temp2->nex;

}

temp2->nex=ptr2;

}

printf("\nEnter the data & Press 0 to terminate");

scanf("%d",&data);

}while(data!=0);

}

void display2()

{

temp2=start2;

if(start2==NULL)

printf("\nList is empty");

else

{

printf("\nElements of list:\n");

while(temp2->nex!=NULL)

{

printf("%d\t",temp2->info);

temp2=temp2->nex;

}

printf("%d\t",temp2->info);

}

}

void insert2()

{

int data,pos;

printf("\nEnter data & position to insert the element");

scanf("d",&data,&pos);

temp2=start2;

ptr2=malloc(sizeof(struct node2));

ptr2->info=data;

while(temp2->nex!=NULL)

{

if(temp2->info==pos)

{

ptr2->nex=temp2->nex;

temp2->nex=ptr2;

break;

}

else

temp2=temp2->nex;

}

if(temp2->nex==NULL)

{

temp2->nex=ptr2;

ptr2->nex=NULL;

}

}

void delet2()

{

int pos,flag=0;

if(start2==NULL)

printf("List is empty");

else

{

printf("Enter element to be deleted");

scanf("%d",&pos);

temp2=start2;

if(start2->info==pos)

{

flag=1;

start2=start2->nex;

free(temp2);

}

else

{

while(temp2->nex!=NULL)

{

prev2=temp2;

temp2=temp2->nex;

if(temp2->info==pos)

{

flag=1;

prev2->nex=temp2->nex;

free(temp2);

break;

}

}

}

if(flag==0)

printf("\nElement is not present in list");

}

}

void erase2()

{

if(start2==NULL)

printf("\nList is empty");

else

{

while(start2->nex!=NULL)

{

temp2=start2;

start2=start2->nex;

free(temp2);

}

temp2=start2;

start2=start2->nex;

free(temp2);

printf("list is erased");

}

}

void link1()

{

int op;

start=NULL;

do

{

printf("\n1.create\n2.display\n3.insert\n4.delete\n5.erase\n6.exit");

printf("\nEnter your option");

scanf("%d",&op);

switch(op)

{

case 1:

create();

break;

case 2:

display();

break;

case 3:

insert();

break;

case 4:

delet();

break;

case 5:

erase();

break;

case 6:

break;

default:

printf("\nEnter correct option");

}

}while(op!=6);

getch();

}

void link2()

{

int op;

start2=NULL;

do

{

printf("\n1.create\n2.display\n3.insert\n4.delete\n5.erase\n6.exit");

printf("\nEnter your option");

scanf("%d",&op);

switch(op)

{

case 1:

create2();

break;

case 2:

display2();

break;

case 3:

insert2();

break;

case 4:

delet2();

break;

case 5:

erase2();

break;

case 6:

break;

default:

printf("\nEnter correct option");

}

}while(op!=6);

getch();

}

void merge()

{

if(start==NULL)

{

start=start2;

}

else

{

temp=start;

while(temp->next!=NULL)

{

temp=temp->next;

}

temp->next=start2;

}

temp=start;

if(start==NULL)

printf("\nList is empty");

else

{

printf("\nElements of list:\n");

while(temp!=NULL)

{

printf("%d\t",temp->info);

temp=temp->next;

}

}

getch();

}

the above listed program is definitely good one, here is a program which just does merging job, http://www.refcode.net/2013/02/merging-linked-lists.html

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

15y ago

Well the answer to this question would depend on what you mean by "merge," but if you simply want a new list which contains all elements of the two originals:

LinkedList a,b; // our two original lists

// shove some data into a and b

LinkedList ab = new LinkedList(a);

ab.addAll(b);

By the end of this code, list ab will contain all the elements of list a (as returned by its iterator) followed by all the elements of list b (also as returned by its iterator).

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a program to merge two linked list and find out the running time?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Which is preferred for writing a Merge Sort routine contiguous or linked memory?

Linked memory because its very useful primarily when the lists to be sorted are very large and the size of data to be moved is small.


Where is the mail merge feature located in the main menu?

It would depend on the program you are trying to use.


What is the code for c and c plus plus program to merge two circular linked list?

Circular linked lists are really no different to ordinary linked lists, other than that the tail node points back to the head node (and vice versa if the list is doubly-linked). Therefore the merge process is exactly the same: iterate through the second list and insert each node's data into the first list. Since lists are un-associated containers, it doesn't matter where the insertions occur but, by convention, insertions typically occur at the tail of the list. If an order must be maintain, an insertion sort should be employed instead. Note that if you need to maintain the original two lists (in their un-merged state), simply copy the first and insert the second into the copy instead.


How do you merge 2 Visual C programs to get a single output?

You cannot. A C program can only have one global main function but you'd be trying to compile a program that has two main functions. The only way to merge the two programs is to modularise both programs so that neither is dependent upon their main functions. Once modularised, you can include those modules in any program. Alternatively, you can create binary libraries from the modules and link them into any program.


What are the disadvantages of using mail merge?

Advantages: It is quick and easy. It saves time. You can address a large number of letters without having to do it yourself as mail merge inserts it for you. Disadvantages: It can be used as a scam. It runs slowly or doesn't run at all when more than one software is running. If it is email merge all recipients will be able to view all data and information.

Related questions

How to write aC program to merge two singly linked list?

write a c program to circular queue


How would you sort a linked list?

Use merge sortUse tree sort


How do you write a sentence with the word merge?

Please be very careful when you merge with traffic on the expressway.


If WikiTree is supposed to be a single lineage-linked database why does there appear to be a tool to merge entire trees but not a tool to link individuals in other trees?

WikiTree is intended to be a single, worldwide family tree. Ideally, there should be one lineage-linked profile for every individual who ever lived. The merge tool does not merge entire trees, it merges individuals. When duplicate profiles of the same individuals are merged, they aren't just linked. They are fully combined into one profile.


When a program combines a document with a recipient address it is performing a what?

Mail merge


When a program combines a document with a recipient address it is performing a?

Mail merge


Which is preferred for writing a Merge Sort routine contiguous or linked memory?

Linked memory because its very useful primarily when the lists to be sorted are very large and the size of data to be moved is small.


How write a draft of birthday invitations and use mail merge features?

&Igrave;


Does mail merge give you the opportunity to type and create your own data?

No, but a text-editor program does.


Where is the mail merge feature located in the main menu?

It would depend on the program you are trying to use.


When would a business use mail merge?

to write lots of letters to people etc


What is used to combine cells together in the program excel?

You can use the Merge and Center icon on the formatting toolbar or do it through the Format menu by going to Cells and then picking the Merge cells option.