top of page
Writer's pictureApurba Paul

How to Split a Singly Linked List into two separate Lists

#include<stdio.h> typedef struct mynode { int info; struct mynode *Link; }Node; Node *start=NULL,*start2=NULL; //Display Function void display() { Node *tptr=start; printf("\nDATA:\n"); while(tptr) { printf("->%d",tptr->info); tptr=tptr->Link; } } //Split void split() { int pos,step=1; Node *tptr=start,*First=start,*Second=NULL; printf("\nEnter the position from where you want to split:"); scanf("%d",&pos); while(step<pos-1) { tptr=tptr->Link; step++; } Second=tptr->Link; tptr->Link=NULL; Node *ptr1=First,*ptr2=Second; printf("\nList 1:\n"); while(ptr1) { printf("->%d",ptr1->info); ptr1=ptr1->Link; } printf("\nList 2:\n"); while(ptr2) { printf("->%d",ptr2->info); ptr2=ptr2->Link; } } //Insertion void add_node(int value) { Node *newp=NULL,*tptr=NULL; newp=(Node*)malloc(sizeof(Node)); newp->info=value; newp->Link=NULL; if(start==NULL) { start=newp; } else { tptr=start; while(tptr->Link!=NULL) { tptr=tptr->Link; } tptr->Link=newp; } } //Main int main() { int value; char choice; while(1) {   printf("Do you want to insert(y/n)?");   scanf("%c",&choice);   if(choice == 'y') {   printf("\nEnter the info:");   scanf("%d",&value); add_node(value);   fflush(stdin); } else { break; } } display(); split(); return 0; }

14 views0 comments

Recent Posts

See All

Comments


bottom of page