PROGRAM TO COPY ONE LINEAR LINKED LIST TO ANOTHER
|
Download Attachment
|
/****************************************************/ /* PROGRAM TO COPY ONE LINEAR LINKED LIST TO ANOTHER*/ /****************************************************/
#include < stdio.h> #include < conio.h> #include < malloc.h> #include < process.h> #include < ctype.h>
struct linear_list { int info; struct linear_list *next; }*start,*newnode,*ptr,*start1;
void main() { int item; int i; char ch; clrscr(); newnode=(struct linear_list*)malloc(sizeof(struct linear_list)); start=newnode; do { printf("\nEnter data: "); scanf("%d",&item); newnode->info=item; printf("\nDo you want to create another node:(y/n)"); fflush(stdin); scanf("%c",&ch); if(tolower(ch)=='y') { newnode->next=(struct linear_list*)malloc(sizeof(struct linear_list)); newnode=newnode->next; } else { newnode->next=NULL; } }while(tolower(ch)!='n'); printf(“\n Linked List is:\n”); ptr=start; i=1; while(ptr!=NULL) { printf("\nNode %d : %d",i,ptr->info); ptr=ptr->next; i++; }
ptr=start; newnode=(struct linear_list*)malloc(sizeof(struct linear_list)); start1=newnode; while(ptr!=NULL) { newnode->info=ptr->info; if(ptr->next==NULL) { newnode->next=NULL; } else { newnode->next=(struct linear_list*)malloc(sizeof(struct linear_list)); newnode=newnode->next; } ptr=ptr->next; }
printf("\nThe copied linked list is:\n"); ptr=start1; i=1; while(ptr!=NULL) { printf("\nNode %d : %d",i,ptr->info); ptr=ptr->next; i++; }
getch(); }
|
|
|
|
|
Share this article
| Print |
Article read by 10111 times
|
|
|