how exactly do i store b without overwriting it?
You seem to be doing this backwards. You don't seem to have any idea of what you want your function to be used for. You're just throwing random scenarios at some code and trying to make it fit. Don't you have anything in mind that it's supposed to do?
You get an idea of what you want some code to do, then you make it do it. You don't just write random test cases and try to have it fit them all without having any real goal in mind.
Quzah.
Hope is the first step on the road to disappointment.
quzah, i tried the physical object approach (with cards labelled A,B,C) in my last post i came up with:
I can't seem to store B without overwriting somethingCode:charNode *temp = a; charNode *temp2 = a->next; temp2 is b charNode *temp3 = b->next; temp3 is c a->next = b->next; /* form A, C works until here */ temp2 = temp3;
the strange thing is:
after the above line, that's when B goes missing. I don't know how to get it back after that. Any ideas?Code:a->next = b->next;
I'm still stuck on it(all day actually). Any other hints? I've tried following quzah's original code, i understand it perfectly. I can't apply the same approach to making ABC to ACB. After thinking about how to do it with 3 cards, i then try coding it only to get an endless loop or an unxpected result :|
now your list becomes {A,C} OR {B,C}Code:charNode *temp = a; charNode *temp2 = a->next; temp2 is b//actually no use of these two temp variables charNode *temp3 = b->next; temp3 is c // a->next = b->next; /* form A, C works until here */
Now your aim is {A,C,B}.how would you do it??.pointing the next of c to b and next of b to Null.
Long time no C. I need to learn the language again.
Help a man when he is in trouble and he will remember you when he is in trouble again.
You learn in life when you lose.
Complex problems have simple, easy to understand wrong answers.
"A ship in the harbour is safe, but that's not what ships are built
for"
are you saying my code is valid up until a->next = b->next;?
Because when i print it out without the last line only 3 elements are printed.
(edit) sorry i meant 2.
>>are you saying my code is valid up until a->next = b->next;?
yeah.
>>Because when i print it out without the last line only 3 elements are printed.
You are passing three elements a,b,c.your b is lost what are the 3 elements printed.n foremost how you are printing?
Long time no C. I need to learn the language again.
Help a man when he is in trouble and he will remember you when he is in trouble again.
You learn in life when you lose.
Complex problems have simple, easy to understand wrong answers.
"A ship in the harbour is safe, but that's not what ships are built
for"
thats beacause you set c->next to b which was earlier d.now you lost d.so make a statement atlast which does b->next = d.Originally Posted by Axel
Long time no C. I need to learn the language again.
Help a man when he is in trouble and he will remember you when he is in trouble again.
You learn in life when you lose.
Complex problems have simple, easy to understand wrong answers.
"A ship in the harbour is safe, but that's not what ships are built
for"
i tried b->next = a->next->next->next which gives an endless loop unless i put a NULL. I think i have to set the last element in the list to NULL?
my print function is something like:
Code:while (start != NULL ) { printf("\n %d %c %.2f %.2f\n", element , start->cityname, start->numberone, start->numbertwo); }
programming is little different from chemistry.One should make the diagram of variable on a notebook and see how pointers are moving instead of hit and trial method untill v get feeling of
EUREKA
Long time no C. I need to learn the language again.
Help a man when he is in trouble and he will remember you when he is in trouble again.
You learn in life when you lose.
Complex problems have simple, easy to understand wrong answers.
"A ship in the harbour is safe, but that's not what ships are built
for"
ok it doesn't seem like what i wanted. Say if i had
A, B, C, D, E
start with B and C
swap the two.
A, C, B, D, E
if the IF evaluates to true then everything is OK. Continue on...
go to D and E swap it.
A, C, B, E, D
The if evaluates to false so i need to unswap it.
A, C, B, D, E
at the moment here's what happens ( i have a while loop which just goes and runs the swap function till it hits the end)
A,B,C,D,E
A,C,B,D,E
A,C,D,B,E
A,C,D,E,B
i somehow need to UNSWAP if the if condition evaluates to false. Any hints ?
Last edited by Axel; 10-26-2005 at 08:27 AM.