473,396 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

C: swap function (linked list)

36
need to write function swap(*list,char X , char Y) that swap beteein items in liked list ;

supose list: A ->B->C->D->NULL

swap(list,A,B)

result:


list : B->A->C->D->NULL


changing value of items is forbidden .


TNX . ..........
Sep 14 '09 #1
13 10769
Tassos Souris
152 100+
Make a function that takes pointers to the pointers that point to these two nodes... then swap those pointers..
Sep 14 '09 #2
sedaw
36
but it`s singly listed .

it`s alright ?
Sep 14 '09 #3
JosAH
11,448 Expert 8TB
@sedaw
Forbidden by who? I guess this is homework again; you show us what you have tried and explain to us where you got stuck and then we might try to help you out. We are not going to give you a copyable and pastable spoonfed solution.

kind regards,

Jos
Sep 14 '09 #4
sedaw
36
hello Jos !

that`s not homework i just learning C by myself , i want to sort list but dont know how to swap beteen nodes in list .

i tryin do it without change the values cause i want to learn linked lists , the option of change values is simple and not useful for learning .

TNX , wish you all the best.
Sep 14 '09 #5
Banfa
9,065 Expert Mod 8TB
You can easily write a swap function in terms of an insert function and a remove function (that does not delete the removed node just returns it.
Sep 14 '09 #6
donbock
2,426 Expert 2GB
@sedaw
Your swap prototype hints that the list items are single characters. In general, a linked list node can contain a lot of information (ie, fields). Typical practice is to characterize one field as the key field. You can then specify a swap function that searches for two nodes whose key fields match the function arguments, and then swap the position of those nodes in the list. You need to consider how you want to handle the corner cases:
  • More than one node matches the key value.
  • No node matches the key value.
  • The caller specifies the same key value twice (swap a node with itself).
  • The *list argument is NULL.
Sep 15 '09 #7
sedaw
36
ok.... my solution :

Expand|Select|Wrap|Line Numbers
  1. void sort_list(Node*item)
  2. {
  3.     Node *ptr1, *ptr2, *current, *temp;
  4.     while(item!=NULL)
  5.     {
  6.         current=item;
  7.         while(current!=NULL)
  8.         {
  9.             if(current->value < item->value)
  10.             {
  11.                 ptr1=item->next;
  12.                 ptr2=current->next;
  13.                 temp=item;
  14.                 item=current;
  15.                 item->next=ptr1;
  16.                 current=temp;
  17.                 current->next=ptr2;
  18.             }
  19.             current=current->next;
  20.         }
  21.         item=item->next;
  22.     }
  23. }
  24.  
the program not workin alright although theres no compilation error .



TNX ..........
Sep 15 '09 #8
Try to put the printf statment and check the flow.

In 9th line ur comparing same pointer value.
Sep 16 '09 #9
Banfa
9,065 Expert Mod 8TB
That is not a swap algorithm that is a sort algorithm (or would be if it worked). Since many sorts need to swap you should write a swap algorithm.

What you have written comes no where near to a swap, for instance these lines

temp=item;
item=current;
...
current=temp;

May look like a swap but they operate on local variables and therefore have absolutely no effect on your list.

You need to change the list members taht are pointing to "current" and "item".

However the code does change the items that "current" and "item" point to because you change the data that they point to not the pointers themselves. So current-.next is swapped with item->next. This has the effect of partitioning you list as follows: Assume this list

A -> B -> C -> D -> E -> F

Assume current points to B and item points to D, you don't change the nodes pointing to current and item but you do swap where current and item point to so after you operation you have 2 chains

A -> B -> E -> F
D -> C -> D -> C -> D -> C -> D -> ...

The D/C chain is infinite and is also lost (assuming you have a head pointer to A).

1. Start by writing a successful swap function to swap 2 nodes in a list.
2. Then write your sort algorithm.
Sep 16 '09 #10
Tassos Souris
152 100+
And again consider using pointers to the pointers that point to each node :)
Sep 16 '09 #11
sedaw
36
line 11-18 the swap code.

the pointers of items saved as ptr1 and ptr2 for backup

after changing the content of item by current

ptr1=item->next;
ptr2=current->next;

this is whats goin on:

E->D->C->B->A->NULL


supose swap E,C

save ptr1 = Node{E}->next = D->C->B->A->NULL


after change the content E &C :

Node{E}->value = C
Node{E}->next = B->A->NULL

now cangin the pointer :
Node{E}->next = ptr1


the new list C->D->C->B->A->NULL


same proces to Node{C} while data of Node{E} saved as temp .

Node{C}=temp;
Node{C}->next=ptr2 ;
Sep 16 '09 #12
Banfa
9,065 Expert Mod 8TB
If you have a list of N nodes and you want to swap the positions of 2 arbitrary nodes P and Q in the list then you have to change the following nodes

node[P-1]
node[P]
node[Q-1]
node[Q]

That is you need to change the nodes before the 2 nodes you want swap because they need to point somewhere different after the swap.

Since your code on changes the pointers in 2 actual nodes it can not properly swap 2 nodes.
Sep 16 '09 #13
sedaw
36
is there any option to perform it on unidirectional list ?
Sep 16 '09 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Andrew Koenig | last post by:
I am looking for the most effective way to exchange v and v, and expected to find something like v.swap(i, j) but it doesn't seem to exist. Is there anything better than v, v = v, v ...
8
by: surrealtrauma | last post by:
if i have circular linked list like: 14->12->10->8->6->4->2->0->// how can i revises it as 0->2->4->6->8->10->12->14->// is there any other method besides swap? Thank you
7
by: Kieran Simkin | last post by:
Hi all, I'm having some trouble with a linked list function and was wondering if anyone could shed any light on it. Basically I have a singly-linked list which stores pid numbers of a process's...
12
by: Eugen J. Sobchenko | last post by:
Hi! I'm writing function which swaps two arbitrary elements of double-linked list. References to the next element of list must be unique or NULL (even during swap procedure), the same condition...
10
by: Atlas | last post by:
To swap to nodes in list, changing pointers is enough. But the stl swap method seems to copy their values, doesn't it?
4
by: bridgemanusa | last post by:
Hi All: I have a very long page of html that I want to take portions and hide them in divs, then show when a link is clicked. I have the hide show part working when the link is clicked, however...
19
by: Krishanu Debnath | last post by:
Hello, I have a call to hash_map::clear() function which takes long time. someClass::someFunction() { // typedef hash_map<name_id, uintMp; // Mp p; // assuming proper namespace, hash...
2
by: Billy | last post by:
Hi, It has to be some stupid high school home task , that can be solved in rather straightforward manner. The pain in the @ss is the requirement not to touch the original list, that is: no swap...
3
by: lrsweene | last post by:
How to swap elements in a singly linked list?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.