Linked List - Coder's point

Sunday, April 7, 2019

Linked List

 #include<stdio.h>  
 #include<conio.h>  
 #include<alloc.h>  
 struct node  
 {  
 int data;  
 struct node *next;  
 };  
 void main()  
 {  
 struct node *l1,*l2,*temp,*t;  
 int i,j,sp;  
 clrscr();  
 l1=(struct node *)malloc(sizeof(struct node));  
 printf("Enter data in first node of first linked list:");  
 scanf("%d",&l1->data);  
 temp=l1;  
 for(i=1;i<5;i++)  
 {  
 temp->next=(struct node *)malloc(sizeof(struct node));  
 temp=temp->next;  
 printf("Enter data in next node of first linked list:");  
 scanf("%d",&temp->data);  
 temp->next=NULL;  
 }  
 printf("The first linked list is:\n");  
 temp=l1;  
 for(i=0;i<5;i++)  
 {  
 printf("%d\t",temp->data);  
 temp=temp->next;  
 }  
 l2=(struct node*)malloc(sizeof(struct node));  
 printf("\nEnter data in first node of second linked list:");  
 scanf("%d",&l2->data);  
 temp=l2;  
 for(i=1;i<5;i++)  
 {  
 temp->next=(struct node*)malloc(sizeof(struct node));  
 temp=temp->next;  
 printf("Enter data in next node of second linked list:");  
 scanf("%d",&temp->data);  
 temp->next=NULL;  
 }  
 printf("The second linked list is:\n");  
 temp=l2;  
 for(i=0;i<5;i++)  
 {  
 printf("%d\t",temp->data);  
 temp=temp->next;  
 }  
 temp=l1;  
 for(i=0;i<4;i++)  
 temp=temp->next;  
 temp->next=l2;  
 printf("\nThe linked list after concatenation is:\n");  
 temp=l1;  
 for(i=0;i<10;i++)  
 {  
 printf("%d ",temp->data);  
 temp=temp->next;  
 }  
 printf("\nEnter Split Point:");  
 scanf("%d",&sp);  
 printf("\nBefore Split point:\n");  
 temp=l1;  
 for(i=0;i<sp;i++)  
 {printf("%d ",temp->data);  
 temp=temp->next;  
 }  
 printf("\nAfter Split point:\n");  
 for(i=sp;i<10;i++)  
 {printf("%d ",temp->data);  
 temp=temp->next;  
 }  
 printf("\nCopying Linked list\n");  
 l2=l1;  
 temp=l1;  
 t=l2;  
 for(i=0;i<10;i++)  
 {t->data=temp->data;  
 t=t->next;  
 temp=temp->next;  
 }  
 temp=l1;  
 for(i=0;i<10;i++)  
 {  
 printf("%d ",temp->data);  
 temp=temp->next;  
 }  
 printf("\nReverse of Linked list\n");  
 l2=(struct node *)malloc(sizeof(struct node));  
 t=l2;  
 for(i=0;i<10;i++)  
 {  
 temp=l1;  
 for(j=0;j<9-i;j++)  
 temp=temp->next;  
 t->data=temp->data;  
 t->next=(struct node *)malloc(sizeof(struct node));  
 t=t->next;  
 t->next=NULL;  
 }  
 temp=l2;  
 for(i=0;i<10;i++)  
 {  
 printf("%d ",temp->data);  
 temp=temp->next;  
 }  
 getch();  
 }  
OUTPUT:


Enter data in first node of first linked list:10
Enter data in next node of first linked list:9
Enter data in next node of first linked list:8
Enter data in next node of first linked list:7
Enter data in next node of first linked list:6
The first linked list is:
10        9          8          7          6         
Enter data in first node of second linked list:5
Enter data in next node of second linked list:4
Enter data in next node of second linked list:3
Enter data in next node of second linked list:2
Enter data in next node of second linked list:1
The second linked list is:
5          4          3          2          1         
The linked list after concatenation is:
10 9 8 7 6 5 4 3 2 1
Enter Split Point:5

Before Split point:
10 9 8 7 6
After Split point:
5 4 3 2 1
Copying Linked list
10 9 8 7 6 5 4 3 2 1
Reverse of Linked list
1 2 3 4 5 6 7 8 9 10

No comments:

Post a Comment