error in location

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xtoma
    New Member
    • Feb 2015
    • 11

    error in location

    i have this problem
    i insert 10 as first location then 20 and 30
    when i run the program it give me error in location
    10 30 20

    the error may be in insert can you give me hints for this
    thank you in advance

    Code:
     Node * List::InsertNode (int index , double x)
     {                
                          if (index < 0 )return NULL;
                          int currlndex = 1 ;
                          Node* currNode = head;
                          while (currNode!= NULL && index > currlndex ) 
                          {
                                currNode;
                                currlndex++;
                           }
                                if (index > 0 && currNode == NULL ) return NULL;
                                Node*newNode = new Node;
                                newNode -> data = x;
                                if (index ==0)
                                {
                                       newNode -> next = head;
                                      head = newNode;
                                       }
                                      
                          
                                else
                                               {
                                               newNode -> next = currNode->next;
                                               currNode->next = newNode ;
                                               }
                                               return newNode;
        }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Usually, the first node of a list is index 0. So 10 20 30 would be indexes 0 1 and 2. It's done this way so the list works like an array, which everyone is familiar with.

    Next, in what order did you enter your values? If you went 10,20,30 the all you need is append at end of list. But if you went 20 30 10 and you want to keep the list sorted then you need an insert node A before node B. That is usually a function:

    Code:
    void insertbefore(Node*A, Node* B);
    where A is inserted before B.

    Comment

    Working...