The code below swaps two nodes in the linked list. The swap operation can be helpful in sorting a linked list and so its very important how to swap the nodes of linked list. Swapping in normal arrays is swap of the data but in linked list its the pointers change and so there is just change in the index of the linked list where the node will remain. Code: node* swap(node *current) { int rno; /* Roll number for swaping node*/ int t; /* Total number of nodes */ node *temp; /* Temporary copy of current */ node *tmp; /* Temporary variable */ t=number(current); if(t<=1) { printf("\nYou cannot swap the only node\n"); return(current); } printf("\nEnter roll number whose node you want to swap with the next\n"); scanf("%d",&rno); temp=current; if(current->roll_no==rno) { tmp=current->next; current->next=current->next->next; tmp->next=current; current=tmp; return(current); } else { while(temp->next->next!=NULL) { if(temp->next->roll_no==rno) { tmp=temp->next->next; temp->next->next=temp->next->next->next; tmp->next=temp->next; temp->next=tmp; break; } temp=temp->next; } return(current); } } You need to call the function as below. Code: head=swap(head); // head is the first node of the linked list And last but not the least you should also know how the structure of the linked list is defined. Code: struct list{ int roll_no; /* Storing roll number of a node */ char name[N]; /* Storing name of node */ float marks; /* Storing marks of a node */ struct list *next; /* Storing next address */ }; /***** Redefining struct list as node *****/ typedef struct list node;
Was really a nice try. Ya, what you stated is very correct that manipulating pointers is effective than changing values. But i would like to suggest you that once you have swapped toe nodes, you should try swapping a complete list. When i tries it for the first time was a very difficult job. you better use DEBUG.EXE for having faster results..... Coding techniques were good.....
If you know what you are doing its easy to extend but there will be something interesting when implementing it and if you don't get that you will not enjoy the coding.
Hey I have written following code but its giving me error ....can u correct that Code: #include<stdio.h> #include<conio.h> struct list{ int t1; /* Storing roll number of a node */ struct list *next, *head; /* Storing next address */ }; /***** Redefining struct list as node *****/ typedef struct list node; void main() { node *head; head*= void swap(head); getch(); } node* swap(node *current) { int t; /* Total number of nodes */ int rno; node *temp; /* Temporary copy of current */ node *tmp; node *roll_no; /* Temporary variable */ t=number(current); if(t<=1) { printf("\nYou cannot swap the only node\n"); return(current); } printf("\nEnter number whose node you want to swap with the next\n"); scanf("%d",&rno); temp=current; if(t==rno) { tmp=current->next; current->next=current->next->next; tmp->next=current; current=tmp; return(current); } else { while(temp->next->next!=NULL) { if(temp->next->t==rno) { tmp=temp->next->next; temp->next->next=temp->next->next->next; tmp->next=temp->next; temp->next=tmp; break; } temp=temp->next; } return(current); } }
I would not do that as we already have the code in the articles for you to use it and debug your own code. If you help us to know where you are stuck we can definitely help you out.