Inserting a new node.

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
}
I further assume that it's a singly linked list
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
}
  
I have a create new node function as well. I would assume that this newNode comes from there.

What do you mean, "I would assume"? It's your code - surely you know where you've defined newNode?
Assume is of course a bad choice of word. The newNode is created in that function and inserted with the one i was asking about.
So what is newNode? A global variable? A data member?
It would be data which nodePtr1 will point to.

 
nodePtr1 = createNode(1);   // NodePtr1 points to a new node with data=1 
Topic archived. No new replies allowed.