-3

Why does the above error occur when calling showNode()?

#include<iostream>
#define NULL 0

using namespace std;

class myNode{

private:
    int data;
    myNode* link;
    myNode* first;

public:
    myNode(){

        data=0;
        link=NULL;
        first=NULL;

    }

    void insertNode(int value, int iposition){

        myNode n;

        if (iposition==1)
        {
            first=&n;
            cout<<first<<endl;
            n.data=value;
            n.link=NULL;
        }

        if (iposition>1)
        {
            int nodeCounter=1;

            myNode* temp=first;

            while (temp->link != NULL)
            {
                nodeCounter++;
            }// this while counts the number of inserted nodes.

            if (iposition>nodeCounter)//if the position of the new node is greater than number of inserted node,
                                        //it will be inserted at the end.
            {
                cout<<"Node will be inserted at end."<<endl;

                myNode* ieTemp=first;

                while (ieTemp->link != NULL)
                {
                    ieTemp=ieTemp->link;
                    cout<<ieTemp->data<<endl;
                }

                ieTemp->link=&n;
                n.data=value;
                n.link=NULL;
                cout<<&n<<"     ";

            }

            else
            {
                myNode* imTemp=first;

                while (iposition-1)
                {
                    imTemp=imTemp->link;
                    iposition--;
                }

                n.link=imTemp->link;
                n.data=value;
                imTemp->link=&n;

            }
        }

    }//end insertNode

    void showNode(){

        myNode* sTemp=first;

        while (sTemp != NULL)
        {
            cout<<sTemp->data<<"   ";
            sTemp=sTemp->link;
        }

    }//end showNode

};

int main(){

    myNode a;
    a.insertNode(10,1);
    a.insertNode(20,2);
    a.insertNode(25,3);
    a.insertNode(30,4);
    a.insertNode(40,5);


    a.showNode();


system("pause");
}
Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
Mehran
  • 73
  • 7
  • 4
    What happens to 'n' when insertNode() exits? – Martin James Jul 18 '15 at 16:04
  • I can't figure out the close votes here. This is a clear, reproducible, and very answerable question. – Drew Dormann Jul 18 '15 at 16:36
  • Another hint: is `n` created on stack or heap in `insertNode()`? – Chris O Jul 18 '15 at 17:07
  • 2
    @DrewDormann - no aparrent attempt at debugging? – Martin James Jul 18 '15 at 17:17
  • Also, any linked-list question draws an almost instinctive down/close vote. Finding one where some debugging has been done is such a shock that I tend to link in chatrooms when it happens. That, and LL questions/problems all seem to fall into the same categories - updating local vars in functions and expecting them to proagate, failing to take start/end edge-cases into account, failing to update head, dereferencing null. RAIIing away a node is different enough that I would not have DV it if any sign of debugging was shown, unfortunately.. – Martin James Jul 18 '15 at 17:23
  • @MartinJames there is no close vote related to how much debugging has been done. Perhaps you're thinking of downvotes. – Drew Dormann Jul 18 '15 at 17:23
  • @DrewDormann - yeah, I did not CV, just DV 'cos no debugging. The OP did provide a SSCCE and the result obtained, (if AV can be considered a result:), so I did not think that a CV was merited. – Martin James Jul 18 '15 at 17:26
  • if you're seeing 0xCCCCCCCC there's an extremely high chance that you're [reading uninitialized memory](http://stackoverflow.com/q/370195/995714) – phuclv May 06 '18 at 16:34

1 Answers1

0

First you must declare myNode *n = new myNode(); because you use it outside of insertNode function. Second you may want to check if first node exist to avoid error on inserting first node on any other position than 1 (in your case if your first insert will be like inserNode(x, y != 1) will throw an error because you try to access first node (here: while (temp->link != NULL))) but this node don't exist.
This is what I think you want:

void insertNode(int value, int iposition){

    myNode *n = new myNode();
    myNode *cur = first;

    //insert first node or on first position
    if (cur == NULL || iposition <= 1) {
        n->data = value;
        if (cur == NULL) { //first node
            n->link = NULL;
        }
        else { //first position
            n->link = first;
        }
        first = n;
        return;
    }
    for (int i = 1; i < iposition - 1; i++) { //find given position
        if (cur->link == NULL) { //if end
            n->data = value;
            n->link = NULL;
            cur->link = n;
            return;
        }
        cur = cur->link;
    }
    //here we are on position = iposition-1
    n->data = value;
    n->link = cur->link;
    cur->link = n;

}//end insertNode
Jorj
  • 1,291
  • 1
  • 11
  • 32