c++ - Linked List Segmentation Fault -



c++ - Linked List Segmentation Fault -

why cause segfault error? i've tried run backtrace gdb, has given me no help. help appreciated, i've been pulling hair out on hours.

my node.h

#ifndef node_h #define node_h #include <string> using namespace std; class node { public: node(const string, const int) ; ~node() { } void setnext(node *);//setter next variable node * getnext();// getter next variable string getkey();// getter key variable int getdistance(); // getter dist variable private: node *next; int dist; string key; }; #endif

my node.cpp

#include "node.h" #include <string> node::node(string k, int d){ key = k; dist = d; } void node::setnext(node * n){ next = n; } node * node::getnext(){ homecoming next; } string node::getkey(){ homecoming key; } int node::getdistance(){ homecoming dist; }

my list.h

#ifndef list_h #define list_h #include "node.h" class sll { public: sll(); ~sll() { } void insert (string searchkey, int distance); bool delete (string searchkey); void print(); int search(string searchkey); private: int count; node *head; node *iterator; node *temp; }; #endif

my list.cpp

#include "list.h" #include <iostream> sll::sll():head(0){} void sll::insert(string searchkey, int distance){ node * temp = new node(searchkey, distance); if(head == 0){ head = temp; } else{ temp->setnext(head); head = temp; } } bool sll::delete(string searchkey){ if(head == 0){ cout << "an effort made delete node empty list" << endl; } else{ node* iterator = head; node* lastly = 0; while(iterator != 0){ if (iterator->getkey() == searchkey){ break; } else{ lastly = iterator; iterator = iterator->getnext(); } } if (iterator == 0){ homecoming false; } else{ if(head == iterator){ head = head->getnext(); } else { last->setnext(iterator->getnext()); } delete iterator; } } } void sll:: print(){ iterator = head; while(iterator != 0){ cout << iterator->getkey() << "-" << iterator->getdistance() << endl; iterator = iterator->getnext(); } } int sll::search(string searchkey){ }

my main.cpp

#include "list.h" #include "node.h" #include <iostream> using namespace std; int main(int argc, char* argv[]) { sll * sll; sll->insert("test", 1); sll->insert("test2", 2); sll->delete("test"); sll->print(); }

hint: segfault happens here:

int main(int argc, char* argv[]) { sll * sll; sll->insert("test", 1); // big segfault here. ...

(no total answers looks homework.)

c++ linked-list singly-linked-list

Comments

Popular posts from this blog

javascript - mongodb won't find my schema method in nested container -

Hibernate criteria by a list of natural ids -

ios - Lagging ScrollView with UIWebview inside -