Segmentation Fault in linkedlists

Whenever I try to do and Application function I am returned with "Segmentation Fault" Core dumped, I don't quite know where the error is coming from. So any help on where it is or how to find it would be appreciated
LinkedList.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
 #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 

linkedListApp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "linkedList.h"
#include "myDate.h"
#include <iostream>
using namespace std;
#include <fstream>


//FillList
//Description: opens a file and fills the list from the file
//Parameters:  indexList
//Return:      none
void FillList(linkedList<myDate> &list);


//DisplayList
//Description: displays the list to the monitor
//Parameters: indexList
//Return:     none
void DisplayList(linkedList<myDate> list);

//AddToList
//Description: adds to the list by ordered insert
//Parameters: indexList,num
//Return:     none
void AddToList(linkedList<myDate> &list);

//GetTodaysAppointments
//Description: Asks the user for today's date, and looks for a matching date from the list
//Parameters: indexList,num
//Return: none
void GetTodaysAppointments(linkedList<myDate> list);

//ChangeAppointment
//Description: Lets the user choose an appointment to change, and changes as 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 from file" << endl;
        cout << "2. Add to the list" << endl;
        cout << "3. Get Todays Appointments" << endl;
        cout << "4. Change an appointment" << endl;
        cout << "5. Display list" << endl;
        cout << "6. Quit"<<endl;
        cout << "Choice: ";
        cin >> ch;
        return ch;
}

//FillList
//Description: opens a file and fills the list from the 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 to open" << endl;
      return;
    }
  list.begin();
  f >> aDate;
  while (result && !f.eof())
    {
      f>>aTime;
      aDate.setTime(aTime);
      list.orderedInsert(aDate);
      f>>aDate;

    }

  f.close();
}


The rest of my code is here https://gist.github.com/anonymous/d5456fe057b169840ffe but the code I posted is where I believe the error is located
Probably line 93. You tried to make a comparison to current->data, except the linked list is empty. In other words, current is a null pointer, and you just tried to ask what a null pointer points to. Hence, segfault. Consider throwing in an empty check to your orderedInsert function to avoid that problem easily.
Topic archived. No new replies allowed.