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 . ..........
13 10769
Make a function that takes pointers to the pointers that point to these two nodes... then swap those pointers..
but it`s singly listed .
it`s alright ?
@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
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.
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.
@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.
ok.... my solution : - void sort_list(Node*item)
-
{
-
Node *ptr1, *ptr2, *current, *temp;
-
while(item!=NULL)
-
{
-
current=item;
-
while(current!=NULL)
-
{
-
if(current->value < item->value)
-
{
-
ptr1=item->next;
-
ptr2=current->next;
-
temp=item;
-
item=current;
-
item->next=ptr1;
-
current=temp;
-
current->next=ptr2;
-
}
-
current=current->next;
-
}
-
item=item->next;
-
}
-
}
-
the program not workin alright although theres no compilation error .
TNX ..........
Try to put the printf statment and check the flow.
In 9th line ur comparing same pointer value.
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.
And again consider using pointers to the pointers that point to each node :)
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 ;
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.
is there any option to perform it on unidirectional list ?
Sign in to post your reply or Sign up for a free account.
Similar topics
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
...
|
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
|
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...
|
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...
|
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?
|
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...
|
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...
|
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...
|
by: lrsweene |
last post by:
How to swap elements in a singly linked list?
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
|
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,...
| |