Hi.
I am trying to insert a new node, but it isnĀ“t happening for me.
Can you please look over my code.
1 2 3 4 5 6 7 8 9 10 11 12
void insert(NodePtr& after_me, NodePtr& newNode)
// after_me is a pointer to the node after which newNode will be inserted
{
NodePtr temp_ptr;
temp_ptr = new Node;
temp_ptr -> forward_link = after_me;
newNode -> forward_link = after_me;
after_me -> forward_link = temp_ptr;
newNode -> forward_link = NULL;
// You have to program the body of this function
}
what is newNode? Do you need to create an object for this?
Assuming that you need to do that:
1 2 3 4 5 6 7 8 9
void insert(NodePtr& after_me, NodePtr& newNode)
// after_me is a pointer to the node after which newNode will be inserted
{
newNode = new Node;
newNode -> forward_link = after_me->forward_link;
after_me -> forward_link = newNode;
// You have to program the body of this function
}
Hi.
thanks for the answer.
This is a singly linked list.
I have a create new node function as well. I would assume that this newNode comes from there.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
NodePtr createNode(int the_number) {
NodePtr new_Node;
newNode = new Node;
newNode -> data = the_number;
newNode -> forward_link = NULL;
newNode -> back_link = NULL;
return newNode;
// Creates a new node with value the_number
// Both links are initialised to NULL
// A pointer to the new node is returned
// You have to program the body of this function
}