Slip 30_2 : Write a program that merges two ordered linked lists into third new list. When two lists are merged the data in the resulting list are also ordered. The two original lists should be left unchanged. That is merged list should be new one. Use linked implementation.

 Solution :

#include<stdio.h>

struct node

{

 int data;

 struct node *next;

};

struct node* create();

void display(struct node*);

struct node* create()

{ int n,i;

 struct node *s,*f;

 printf("Enter how many nodes ");

 scanf("%d",&n);

 f= (struct node *)malloc(sizeof(struct node));

 printf("Enter data ");

 scanf("%d",&f->data);

 s=f;

 for(i=1;i<n;i++)

 {

 s->next=(struct node*)malloc(sizeof(struct node));

 s=s->next;

 printf("Enter data ");

 scanf("%d",&s->data);

 }

 s->next=NULL;

 return f;

}

void display(struct node *f)

{ struct node *s;

 for(s=f;s!=NULL;s=s->next)

 {

 printf("| %d |-> ",s->data);

 }

}

struct node* merge(struct node *f1,struct node *f2)

{ struct node *s;

 for(s=f1;s->next!=NULL;s=s->next)

 {

 }

 s->next=f2;

 return f1;

}

main()

{

 struct node *f1,*f2,*f3;

 f1=create();

 f2=create();

 printf("1st linked list ");

 display(f1);

 printf("\n2nd linked list ");

display(f2);


 f3=merge(f1,f2) ;

 printf("\nAfter merging LL is ");

 display(f3);

}

Comments

Popular posts from this blog

Slip 22_2: Read the data from file 'cities.txt' containing names of cities and their STD codes. Accept a name of the city from user and use sentinel linear search algorithm to check whether the name is present in the file and output the STD code, otherwise output “city not in the list”. Solution :

Slip10_2, 30_1 : Read the data from the file “employee.txt” and sort on names in alphabetical order (use strcmp) using bubble sort or selection sort

Slip 10_1 ,22_1: Implement a linear queue library (st_queue.h) of integers using a static implementation of the queue and implementing the init(Q), add(Q) and peek(Q) operations. Write a program that includes queue library and calls different queue operations