Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<conio.h>
- #include<stdlib.h>
- #include<stdio.h>
- void add(struct node **,int);
- void display(struct node*);
- int count(struct node*);
- void merge(struct node*, struct node*,struct node**);
- struct node
- {
- struct node *link;
- int data;
- };
- void main()
- {
- struct node *first, *second, *third;
- first = second = third = NULL;
- clrscr();
- add(&first,4);
- add(&first,5);
- add(&first,567);
- add(&first,115);
- add(&first,547);
- add(&first,567);
- printf("\nFirst Linked List is ::\n");
- display(first);
- printf("No of element in the 1st linked list are::%d",count(first));
- add(&second,51);
- add(&second,25);
- add(&second,-35);
- add(&second,45);
- printf("\nSecond Linked List is ::\n");
- display(second);
- printf("No of element in the 2nd linked list are::%d",count(second));
- merge(first,second,&third);
- printf("\nMerged Linked List is ::\n");
- display(third);
- printf("No of element in the linked list are::%d",count(third));
- getch();
- }
- void add(struct node**q, int num)
- {
- struct node *temp=*q,*r;
- r = (struct node*) malloc(sizeof(struct node));
- r->data = num;
- /*if the list is empty or the element is added to the begning*/
- if(*q==NULL || (*q)->data>num)
- {
- *q=r;
- (*q)->link=temp;
- }
- else
- {
- /*traverse the entire linked list to
- search the appropriated position*/
- while(temp!=NULL)
- {
- if(temp->data < num && (temp->link==NULL || temp->link->data>num))
- {
- r->link=temp->link;
- temp->link=r;
- return;
- }
- temp=temp->link;
- }
- r->link=NULL;
- temp->link=r;
- }
- }
- /*Displaying the content in the linked list*/
- void display(struct node *q)
- {
- /*Traverse the entire linked list*/
- while(q!=NULL)
- {
- printf("%d ",q->data);
- q=q->link;
- }
- printf("\n");
- }
- /*Count the linked list's element*/
- int count(struct node *q)
- {
- int c=0;
- while(q!=NULL)
- {
- q=q->link;
- c++;
- }
- return c;
- }
- /*Merging the both linked list and store in the 3rd linked list*/
- void merge(struct node *p, struct node *q, struct node **s)
- {
- struct node *z;
- z=NULL;
- /*if both the linked list is empty*/
- if(p==NULL && q==NULL)
- return;
- /*Traverse the both linked list till the end of atleast one linked list*/
- while(p!=NULL && q!=NULL)
- {
- /*if node is being added at the starting*/
- if(*s==NULL)
- {
- *s= (struct node *) malloc (sizeof(struct node));
- z=*s;
- }
- else
- {
- z->link= (struct node *)malloc (sizeof(struct node));
- z=z->link;
- }
- if(p->data < q->data)
- {
- z->data=p->data;
- p=p->link;
- }
- else
- {
- if(p->data > q->data)
- {
- z->data=q->data;
- q=q->link;
- }
- else
- {
- if(p->data == q->data)
- {
- z->data = q->data;
- p=p->link;
- q=q->link;
- }
- }
- }
- }
- /*if the end of the first node has not reached*/
- while(p!=NULL)
- {
- z->link=(struct node*) malloc (sizeof(struct node));
- z=z->link;
- z->data = p->data;
- p=p->link;
- }
- /*if the end of the second node has not reached*/
- while(q!=NULL)
- {
- z->link=(struct node*) malloc (sizeof(struct node));
- z=z->link;
- z->data = q->data;
- q=q->link;
- }
- z->link=NULL;
- }
Advertisement
Add Comment
Please, Sign In to add comment