pointers - C++ linked list implementation segmentation fault (core dumped) error -


i trying learn c++ on own , have been going through textbooks , trying problems. while learning pointers, decided try , implement linked list on own. have written program, keep getting error says: "segmentation error (core dumped)". have searched through other similar questions on website , although there lot on same topic, none have helped me fix problem. i'm pretty new programming , pointers, appreciated!

#include <iostream> #include <cstdio> #include <cstdlib>  using namespace std;   struct node {    int element;    struct node *next; }*start;   class pointerlist {         public:                 node* create(int num);                 void add(int num);                 int first();                 int end();                 int retrieve(int pos);                 int locate(int num);                 int next(int pos);                 int previous(int pos);                 void insert(int pos, int num);                 void delete(int pos);                 void makenull();                 pointerlist()                 {                         start = null;                 } };  main() {         pointerlist pl;         start = null;         pl.add(1);         cout << "added 1" << endl;         (int j=1; j<=5; j++)                 pl.add(j);         cout << "the pointer implemented list is: " << endl;         (int i=1; i<=5; i++)         {                 cout << pl.end() << "  " ;         }         cout << endl << endl; }    void pointerlist::add(int num) {         struct node *temp, *s;         temp = create(num);         s = start;         while (s->next != null)                 s = s->next;         temp->next = null;         s->next = temp; }  node *pointerlist::create(int num) {         struct node *temp, *s;         temp = new(struct node);         temp->element = num;         temp->next = null;         return temp; }  int pointerlist::first () {         int num;         struct node *s;         s = start;         num = s->element;         return num; }  int pointerlist::end() {         struct node *s;         s = start;         int num;         while (s != null);         {                 num = s->element;                 s = s->next;         }         return num; }  int pointerlist::retrieve(int pos) {         int counter = 0;         struct node *s;         s = start;         while (s != null)         {                 counter++;                 if (counter == pos)                 {                         return s->element;                 }                 s = s->next;         } }  int pointerlist::locate(int num) {         int pos = 0;         bool flag = false;         struct node *s;         s = start;         while (s != null)         {                 pos++;                 if (s->element == num)                 {                         flag == true;                         return pos;                 }                 s = s->next;         }         if (!flag)                 return -1; }  int pointerlist::next(int pos) {         int next;         int counter = 0;         struct node *s;         s = start;         while (s != null)         {                 counter++;                 if (counter == pos)                         break;                 s = s->next;         }         s = s->next;         next = s->element;         return next;  }  int pointerlist::previous(int pos) {         int previous;         int counter = 1;         struct node *s;         s = start;         while (s != null)         {                 previous = s->element;                 counter++;                 if (counter = pos)                         break;                 s = s->next;         }         return previous; }  void pointerlist::insert(int pos, int num) {         struct node *temp, *s, *ptr;         temp = create(num);         int i;         int counter = 0;         s = start;         while (s != null)         {                 s = s->next;                 counter++;         }         if (pos == 1)         {                 if (start = null)                 {                         start = temp;                         start->next = null;                 }                 else                 {                         ptr = start;                         start = temp;                         start->next = ptr;                 }         }         else if(pos>1 && pos <= counter)         {                 s = start;                 (i=1; i<pos; i++)                 {                         ptr = s;                         s = s->next;                 }                 ptr->next = temp;                 temp->next = s;         } }  void pointerlist::delete(int pos) {         int counter;         struct node *s, *ptr;         s = start;         if (pos == 1)         {                 start = s->next;         }         else         {                 while (s != null)                 {                         s = s->next;                         counter++;                 }                 if (pos >0 && pos <= counter)                 {                         s = start;                         for(int i=1; i<pos; i++)                         {                                 ptr = s;                                 s = s->next;                         }                         ptr->next = s->next;                 }                 free(s);         } }  void pointerlist::makenull() {         free(start); } 

the problem occurs in line 39 of code (inside main write pl.add(1)). in line trying start off list vale 1. there maybe problems rest of code too, have not been able past line check. please help!

list empty @ beginning, therefore fail on s-> next s == start ==null :

    s = start;     while (s->next != null) 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -