Can you show me a simple code to add a node after the current one? Please I need your help.
Printable View
Can you show me a simple code to add a node after the current one? Please I need your help.
heres one from something i was doing earlier. Pervert it to your needs.
Code:Node* NewNode(char* Value,size_t Len)
{
Node* NodePtr=malloc(sizeof (Node));
if(!NodePtr) Bail();
NodePtr->Value=malloc(Len);
if(!NodePtr->Value)
{
free(NodePtr);
Bail();
}
strcpy(NodePtr->Value,Value);
NodePtr->Next=NULL;
return NodePtr;
}
/* Adds a node to the tail of the list so list will act as a queue */
Node* AddNode(Node* Head,char* Value,size_t Len)
{
Node* Current;
Node* New=NewNode(Value,Len);
if (!Head)Head=New;
else
{
Current=Head;
while(Current->Next != NULL) Current=Current->Next;
Current->Next=New;
}
return Head;
}
Here's some simple code to insert node t after node x.
-PreludeCode:void insertNode ( node *x, node *t )
{
t->next = x->next; x->next = t;
}
Ok, I am back to square one the same problem I had when I was reading the tutorial........take a look at the image I've attached where I've tried to recreate that linked list, I have a question on that one.
-PreludeCode:Start:
Node-->x-->Node-->Node
t-->NULL
t->next = x->next;
Node-->x-->Node-->Node
|
t---^
x->next = t;
Node-->x Node-->Node
| |
t---^
End:
Node-->x-->t-->Node-->Node
sorry but that got me a little confused but I mean is the graphic that I posted the right idea? or am I going all wrong about i?
>or am I going all wrong about i?
Got it in one. Don't think of t as already being a part of the list, it's completely separated so the first statement will assign t's next pointer to point at the node that x points to. The second statement causes t to be fully inserted in the list by pointing x's next pointer to t.
The end result is that t is insterted right between x and the node that x points to. I promise it's not as hard as it sounds, just difficult to explain. Get out some change from your pocket/purse and make a list out of them, then insert a coin.
Quarter--Nickel--Penny
Work it out so that you insert a dime between the quarter and nickel using the code I gave you. The quarter is x and the dime is t, that's how I figured out linked lists. :)
-Prelude
Hold one this is read from LEFT TO RIGHT or FROM RIGHT TO LEFT?
Left to right
-Prelude
oooooooooooooooooooooooh damn now I know that.
house->driveway=apartment->driveway;
apartment->driveway=house;//for some reason I see this as being the joining link between the house and the apartment, this being the driveway.
Please I just want to know if this is right before I go to bed and see if I can get some sleep.
>house->driveway=apartment->driveway;
>apartment->driveway=house;
The apartment is first, the apartment's driveway leads to the house. The house's driveway leads to whatever the apartment's driveway lead to before the house got in the way.
This is not a very good abstraction because you think in construction terms and no sane contractor would build things like this. :D
>//for some reason I see this as being the joining link between the house and the apartment
It is, the joining link from the apartment to the house to be precise. But driveways aren't good as links because a driveway should be a link from the house to the road or the apartment to the road, not the apartment to the house. But if this confuses you just ignore it because I'm blabbering now.
-Prelude
Thanks both Stoned_Coder and Prelude