c++ - Segmentation fault with nodes -


on code keep running segmentation fault when try insert front of linked list. assume has head not being changed on properly. in program baskets nodes. debugger used pointed last line in function issue not sure originates.

include <cstdlib> #include <iostream> #include <ctime> #include "basketlist.h"  using namespace std;  basket::basket(int _datum, basket * _next):    egg_num(_datum), nextbasket(_next) {}   int basket::geteggs() const {    return egg_num; }   basket const* basket::getnextbasket() const {    return nextbasket; }   basketlist::basketlist() : head (null) {}  void basketlist::insertbasket(int eggs) {    basket *currptr = head;    basket *prevptr = null;    basket *newbasketptr;     if(eggs  < head->egg_num)    {    currptr->nextbasket = head;    head = currptr;    }      while(currptr != null && eggs > currptr->egg_num)    {       prevptr = currptr;       currptr = currptr->nextbasket;    }     newbasketptr = new basket(eggs, currptr);    prevptr->nextbasket = newbasketptr;  } 

you need test head being null before use it.

your constructor sets head null, when call insertbasket first thing dereference pointer head.

i think can rid of first if statement (and contents) in insertbasket altogether - loop should want. need check @ end if prevptr null, if so, set head newbasketptr


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -