c++ - Linked List: Segmentation fault error when assigning a value to the next part of a node -


this first question here , english not good, please bear me.

i trying create linked list function insert element in position. need position predptr having trouble doing because whenever run program, segmentation fault. believe error in "predptr->next = first;" part inside insert() function, when set predptr (the line above) "predptr = new node()" works triggers second case in insert function.

here's code:

//class list class list { private:    //class node   class node   {   public:      string data;     node * next;      //node constructor     node()     {       data = "";       next = 0;     }      //node constructor value     node(string val)     {       data = val;       next = 0;     }   };    int mysize;   node * first;  public:    //list constructor   list()   {     mysize = 0;     first = 0;   } //insert function   void insert(string val, int pos)   {     node * newptr, * predptr;     newptr = new node(val);     predptr = new node();     predptr->next = first;      cout << "pred: " << newptr->data << endl;      //position predptr     for(int = 0; < pos; i++)       predptr = predptr->next;      //case 1: inserting @ middle or end of list     if(predptr != 0)     {       cout << "nf" << endl;       newptr->next = predptr->next;       predptr->next = newptr;     }     //case 2: inserting @ beginning of list     else     {       cout << "f"  << endl;       newptr->next = first;       first = newptr;     }      delete predptr;     mysize++;    } int main() {   list a;   cout << (a.empty() ? "yes" : "no") << endl;   cout << "inserting 5 elements..." << endl;   a.insert("asus", 1);   a.insert("acer", 2);   a.insert("sony", 3);   a.insert("toshiba", 4);   cout << "list a: ";   a.display();   cout << endl;    return 0; } 

the first clue might wrong here, in insert():

newptr = new node(val); predptr = new node(); 

rules of logic dictate if insert() expected add 1 value list, creating 2 new nodes, instead of one, wrong. expect create 1 new node, here. not two.

the logic in insert() wrong. can't salvage it. option here rewrite scratch:

node * newptr=new node(val);  node **predptr=&first;  while ( (*predptr) && pos) {     predptr= & (*predptr)->next;     --pos; }  newptr->next= *predptr; *predptr=newptr; 

that's it. if pos passed in 0, insert new node first position in list, if 1 second, etc... things of nature 0-based. if want position #1 instead of position #0 first element of list, decrement pos before loop.

if pos exceeds size of list, code won't crash, unlike attempt, add new node end of list.

i'll close quote:

"the more overthink plumbing, easier stop drain."

scotty, star trek iii.

insertion linked list simple operation. should not complicated process.


Comments

Popular posts from this blog

PHP while loop dynamic rowspan -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

Spring Boot + JPA + Hibernate: Unable to locate persister -