linked list - C++ Segmentation Fault linkedLists -


i have program compiles fine, when try , run function through menu, responded segmentation fault. can't quite seem find seg fault happening either.

linkedlist.cpp

 #include "linkedlist.h" #include <iostream> #ifndef linkedlist_cpp #define linkedlist_cpp using namespace std; //destructor template<class t> linkedlist<t>::~linkedlist() {   node *temp=new node;   while(head->!=null)     {       temp=head;       head=head->next;       delete temp;     } }  //copy constructor template <class t> linkedlist<t>::linkedlist(const linkedlist &list) {   node *temp =new node;   temp =list.head;   delete this;   while(temp->next!=null)     {       orderedinsert(temp->data);       temp=temp->next;     } }  //assignment operator template <class t> linkedlist<t> &linkedlist<t>::operator=(const linkedlist &list) {   if(this != &list)     {       current =head;       while(current->next!=null)         {           head=head->next;           delete  current;           current=head;         }       current=list.head;       while(current->!=null)         {           orderedinsert(current->data);           current=current->next;         }     }   return *this;   }  template <class t> bool linkedlist<t>::empty() const {   current=head;  if(current==null)     {       return true;     }   else     return false; } template <class t> void linkedlist<t>::clear() {   delete *this; }  template <class t> bool linkedlist<t>::search(const t &value) {   current=head;   while(current->data!=value||current->!=null)     {       current=current->next;     }   if(current->data==value)     {       return true;     }   else     return false; }  template <class t> void linkedlist<t>::orderedinsert(const t &value) {   current=head;   while(value>current->data)     {       trailcurrent=current;       current=current->next;     }   trailcurrent->next=new node(value,current);  }  template <class t> bool linkedlist<t>::remove(const t &value) {   node *temp;   temp =head;   if(search(value)!=true)     {       return false;     }   else     {     trailcurrent->next=current->next;     temp=current;     current=current->next;     delete temp;     return true;     } } template <class t> bool linkedlist<t>::replace(const t &olddata,const t &newdata) {   if(search(olddata)==false)     {       return false;     }   else     {       current=head;       while(current->data!=olddata)         {           current=current->next;         }       current->data=newdata;     }   return true; }  template <class t> void linkedlist<t>::insert(const t &value) {   if(trailcurrent==null)     {       trailcurrent=head;       head =new node(value,trailcurrent);       trailcurrent=head;     }   else if(trailcurrent->next->data!=current->data)     {  trailcurrent=head;       while(trailcurrent->next->data!=current->data)         {           trailcurrent=trailcurrent->next;         }       node *temp;       temp=trailcurrent;       temp=new node(value,current);       trailcurrent->next=temp;     }   return true; }  template <class t> bool linkedlist<t>::retrieve(t &value)const {   if(current==null)     {       return false;     }   else     {       current->data =value;       return true;     } }  template <class t> void linkedlist<t>::begin() {  current=head;   trailcurrent=null; }  template <class t> linkedlist linkedlist<t>::operator++() {   if(current!=null)     {       current=current->next;       trailcurrent=trailcurrent->next;       return *this;     } }  template<class t> linkedlist linkedlist<t>::operator++(int i) {   if(current!=null)     {       trailcurrent=current;       current=current->next;     }   return that; }  template <class t> ostream &operator<<(ostream &outstream,linkedlist<t> list) {  t element; this.current=this.head;  while(this.current->!=null)    {      element=this.retrieve(element);      outstream<<"["<<element<<"]"<<endl;      this.current++;    }  return outstream; } #endif 

there lot of nodes .cpp , can't seem find seg fault coming from.

linkedlistapp.cpp

#include "linkedlist.h" #include "mydate.h" #include <iostream> using namespace std; #include <fstream>   //filllist //description: opens file , fills list file //parameters:  indexlist //return:      none void filllist(linkedlist<mydate> &list);   //displaylist //description: displays list monitor //parameters: indexlist //return:     none void displaylist(linkedlist<mydate> list);  //addtolist //description: adds list ordered insert //parameters: indexlist,num //return:     none void addtolist(linkedlist<mydate> &list);  //gettodaysappointments //description: asks user today's date, , looks matching date list //parameters: indexlist,num //return: none void gettodaysappointments(linkedlist<mydate> list);  //changeappointment //description: lets user choose appointment change, , changes such //parameters: indexlist //return: none void changeappointment(linkedlist<mydate> &list);  //menu int menu();  int main() {         int choice;         int num;         linkedlist<mydate> l;          while ((choice = menu()) != 6)         {                 switch (choice)                 {                     case 1: filllist(l); break; case 2: addtolist(l); break;                     case 3: gettodaysappointments(l); break;                     case 4: changeappointment(l); break;                     case 5: displaylist(l); break;                 }         }           return 0; }   //menu int menu() {         int ch;         cout << endl;         cout << "1. fill file" << endl;         cout << "2. add list" << endl;         cout << "3. todays appointments" << endl;         cout << "4. change appointment" << endl;         cout << "5. display list" << endl;         cout << "6. quit"<<endl;         cout << "choice: ";         cin >> ch;         return ch; }  //filllist //description: opens file , fills list file //parameters:  indexlist //return:      none void filllist(linkedlist<mydate> &list) {   mydate adate;   mytime atime;   ifstream f;   bool result =true;   string fname;    cout << "file: ";   cin >> fname;   f.open(fname.c_str());   if (f.fail())     {       cout << "failed open" << endl;       return;     }   list.begin();   f >> adate;   while (result && !f.eof())     {       f>>atime;       adate.settime(atime);       list.orderedinsert(adate);       f>>adate;      }    f.close(); } 

whenever try "fill" end getting seg fault, rest of code here because have more files. think can find seg fault these 2 cant

this thoughts on code - not conclusive , it's not solution segfault.

you seem misunderstand how use pointers. example in destructor, allocated memory temp set head. leak. can set head. there's no reason allocate memory unless plan on using it. have problem throughout code. try learn how pointers work first that'll fundamental many other things.

template<class t> linkedlist<t>::~linkedlist() {   node *temp=new node;   while(head->!=null)     {       temp=head;       head=head->next;       delete temp;     } } 

a potential problem in search function:

while(current->data!=value||current->!=null) 

you have right idea here ordering wrong. if current null? take @ short-circuit evaluation.

your usage of current seems bad design many times use perhaps there's requirement don't know about.

there couple other things noticed if need review of code, there's other places ask that.


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -