Exforsys

Online Training

Swapping Operation

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 ...


Go Back   Exforsys > Programming Talk > C and C++

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-25-2006, 10:55 PM
Senior Member
 
Join Date: Apr 2006
Posts: 122
rachelle is on a distinguished road
Swapping Operation

I am planning to try swapping of two nodes in the linked list using the programming language C.I want to know whether swapping in linked list has the same concept as Swapping in normal arrays.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 05-01-2007, 09:34 AM
Senior Member
 
Join Date: Apr 2006
Posts: 145
Rahulbatra is on a distinguished road
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-01-2007, 10:05 AM
Senior Member
 
Join Date: Apr 2006
Posts: 153
cyrus is on a distinguished road
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);
}
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads

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


All times are GMT -4. The time now is 11:25 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2004 - 2007 Exforsys Inc. All rights reserved.