This is a discussion on Swapping Operation within the C and C++ forums, part of the Programming Talk category; I am planning to try swapping of two nodes in the linked list using the programming language C.I want ...
|
|||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
No the concept differs for the operation of swapping of two nodes in the linked list and Swapping in normal arrays using C programming language. Swapping in normal arrays is swap of the data but in linked list it’s the pointers change and so there is just change in the index of the linked list where the node will remain.
|
|
|||
|
You can make use of the following piece of code for swapping two nodes using linked list in C program.
node* swapsam(node *current) { int no; /* Number for swaping node*/ int s; /* Total number of nodes */ node *temp; /* Temporary copy of current */ node *tmp; /* Temporary variable */ s=number(current); if(s<=1) { printf("\nCannot swap Single node\n"); return(current); } printf("\nEnter number whose node to be swapped with the next\n"); scanf("%d",&no); temp=current; if(current->sno==no) { tmp=current->next; current->next=current->next->next; tmp->next=current; current=tmp; return(current); } else { while(temp->next->next!=NULL) { if(temp->next->sno==no) { tmp=temp->next->next; temp->next->next=temp->next->next->next; tmp->next=temp->next; temp->next=tmp; break; } temp=temp->next; } return(current); } } |
![]() |
| Thread Tools | |
|
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Swapping Two Variables without Temp Variable | Adrian | C and C++ | 5 | 12-05-2006 06:53 AM |
| Comp.Sys.Acorn.FAQ part 1 of 4 | Paul Vigay | Tech FAQ | 0 | 05-31-2004 09:00 PM |
| Comp.Sys.Acorn.FAQ part 1 of 4 | Paul Vigay | Tech FAQ | 0 | 04-30-2004 09:00 PM |
| Apple II Csa2 FAQs: Telecom Hardware & Transfers, Part 20/25 | rubywand@swbell.net | Tech FAQ | 0 | 04-04-2004 08:29 AM |