Can someone help me with this code..
I have a function which is InsertNode()
it is use for inserting a new node after the index in the
double link list..

For example I want to add a node of value 6.0 after position 3
This is the code i write..but It seems like wrong...
can someone help me...correcting this code..
Thnks so much..

Node* List::InsertNode(int index, double x)
{
      if(index<0)
      return NULL;
      
      int currIndex=1;
      Node* currNode=head;
      
      while(currNode && index > currIndex)
      {
                     currNode = currNode->next;
                     currIndex++;
      }
      
      if(index >0 && currNode==NULL)
      return NULL;
      
      Node* newNode = new Node;
      newNode->data = x;
      
      if(index==0)
      {
      newNode->next = head;
      head->prev = newNode;
      
      head = newNode;
      newNode->prev = NULL;
      }
      
      else
      {
      newNode->next = currNode->next;
      currNode->next->prev = newNode;
      
      currNode->next = newNode;
      newNode->prev = currNode;
      }
      
      return newNode;
}

Recommended Answers

All 2 Replies

Can someone help me with this code..
I have a function which is InsertNode()
it is use for inserting a new node after the index in the
double link list..

For example I want to add a node of value 6.0 after position 3
This is the code i write..but It seems like wrong...

What seems wrong about it? Is it just a feeling that it's wrong? If so, run it and find out if it is wrong.

If your feeling proves to be correct, then repost and explain what actually happened and what should have happened. Details are necessary.

Do you have to insert after the index, before the index, or at the index.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.