Segmentation fault.

I'm trying to get four files through this test:

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
#include "sl_list.h"
#include "sl_node.h"

#include <iostream>
#include <sstream>
using std::cout;
using std::endl;
using std::string;

// For testing (DO NOT ALTER)
#include <cctype>
#include <vector>
void UnitTest();
void Test(bool test, int line_number, string more_info = "", string yours = "!",
          string actual = "!");
void OutputFailedTests();
unsigned int ut_passed = 0, ut_failed = 0, ut_total = 0, num_of_tests = 46;
std::vector<int> failed_tests;

// Program Execution Starts Here
int main() {
  // To test your code (DO NOT ALTER)
  UnitTest();
  // This ends program execution
  return 0;
}

// For testing (DO NOT ALTER)
void UnitTest() {
  cout << string(40, '-') << endl;
  cout << "UNIT TEST:\n" << string(40, '-') << endl;
  if (num_of_tests != 0)
    cout << "Total Number of Tests: " << num_of_tests << endl;
  string yours = "", actual = "";
  // Tests
  SLList list;

  Test(list.size() == 0, __LINE__, "Default Constructor & size()");
  yours = list.ToString();
  actual = "";
  Test(yours == actual, __LINE__, "ToString()", yours, actual);
  Test(list.GetHead() == 0, __LINE__, "GetHead()");
  Test(list.GetTail() == 0, __LINE__, "GetTail()");

  list.Insert(10);
  Test(list.size() == 1, __LINE__, "Insert(10) & size()");
  yours = list.ToString();
  actual = "10";
  Test(yours == actual, __LINE__, "ToString()", yours, actual);

  list.Insert(50);
  Test(list.size() == 2, __LINE__, "Insert(50) & size()");
  yours = list.ToString();
  actual = "10, 50";
  Test(yours == actual, __LINE__, "ToString()", yours, actual);

  list.Insert(30);
  Test(list.size() == 3, __LINE__, "Insert(30) & size()");
  yours = list.ToString();
  actual = "10, 30, 50";
  Test(yours == actual, __LINE__, "ToString()", yours, actual);
  Test(list.GetHead() == 10, __LINE__, "GetHead()");
  Test(list.GetTail() == 50, __LINE__, "GetTail()");

  list.Insert(5);
  Test(list.size() == 4, __LINE__, "Insert(5) & size()");
  yours = list.ToString();
  actual = "5, 10, 30, 50";
  Test(yours == actual, __LINE__, "ToString()", yours, actual);

  list.Insert(55);
  Test(list.size() == 5, __LINE__, "Insert(55) & size()");
  yours = list.ToString();
  actual = "5, 10, 30, 50, 55";
  Test(yours == actual, __LINE__, "ToString()", yours, actual);

  list.Insert(20);
  Test(list.size() == 6, __LINE__, "Insert(20) & size()");
  yours = list.ToString();
  actual = "5, 10, 20, 30, 50, 55";
  Test(yours == actual, __LINE__, "ToString()", yours, actual);

  list.Insert(40);
  Test(list.size() == 7, __LINE__, "Insert(40) & size()");
  yours = list.ToString();
  actual = "5, 10, 20, 30, 40, 50, 55";
  Test(yours == actual, __LINE__, "ToString()", yours, actual);

  list.Insert(30);
  Test(list.size() == 8, __LINE__, "Insert(30) & size()");
  yours = list.ToString();
  actual = "5, 10, 20, 30, 30, 40, 50, 55";
  Test(yours == actual, __LINE__, "ToString()", yours, actual);

  list.Insert(5);
  Test(list.size() == 9, __LINE__, "Insert(5) & size()");
  yours = list.ToString();
  actual = "5, 5, 10, 20, 30, 30, 40, 50, 55";
  Test(yours == actual, __LINE__, "ToString()", yours, actual);

  list.Insert(55);
  Test(list.size() == 10, __LINE__, "Insert(55) & size()");
  yours = list.ToString();
  actual = "5, 5, 10, 20, 30, 30, 40, 50, 55, 55";
  Test(yours == actual, __LINE__, "ToString()", yours, actual);

  cout << "\nYour List: " << list.ToString() << endl << endl;

  Test(list.RemoveFirstOccurence(1) == false, __LINE__,
       "RemoveFirstOccurence(1)");

  Test(list.RemoveFirstOccurence(5) == true, __LINE__,
       "RemoveFirstOccurence(5)");
  Test(list.size() == 9, __LINE__, "size()");
  yours = list.ToString();
  actual = "5, 10, 20, 30, 30, 40, 50, 55, 55";
  Test(yours == actual, __LINE__, "ToString()", yours, actual);

  Test(list.RemoveFirstOccurence(30) == true, __LINE__,
       "RemoveFirstOccurence(30)");
  Test(list.size() == 8, __LINE__, "size()");
  yours = list.ToString();
  actual = "5, 10, 20, 30, 40, 50, 55, 55";
  Test(yours == actual, __LINE__, "ToString()", yours, actual);

  Test(list.RemoveFirstOccurence(30) == true, __LINE__,
       "RemoveFirstOccurence(30)");
  Test(list.size() == 7, __LINE__, "size()");
  yours = list.ToString();
  actual = "5, 10, 20, 40, 50, 55, 55";
  Test(yours == actual, __LINE__, "ToString()", yours, actual);

  Test(list.RemoveFirstOccurence(55) == true, __LINE__,
       "RemoveFirstOccurence(55)");
  Test(list.size() == 6, __LINE__, "size()");
  yours = list.ToString();
  actual = "5, 10, 20, 40, 50, 55";
  Test(yours == actual, __LINE__, "ToString()", yours, actual);

  Test(list.RemoveFirstOccurence(10) == true, __LINE__,
       "RemoveFirstOccurence(10)");
  Test(list.size() == 5, __LINE__, "size()");
  yours = list.ToString();
  actual = "5, 20, 40, 50, 55";
  Test(yours == actual, __LINE__, "ToString()", yours, actual);

  Test(list.GetHead() == 5, __LINE__, "GetHead()");
  Test(list.GetTail() == 55, __LINE__, "GetTail()");

  list.Clear();
  Test(list.size() == 0, __LINE__, "Clear() & size()");
  yours = list.ToString();
  actual = "";
  Test(yours == actual, __LINE__, "ToString()", yours, actual);

  cout << string(40, '-') << endl;
  cout << "Passed: " << ut_passed << " / " << ut_total << endl;
  OutputFailedTests();
  cout << string(40, '-') << endl;
  cout << "END OF UNIT TEST!\n";
  cout << string(40, '-') << endl;
  cout << "Be sure to run 'make style' to check for any style errors.\n"
       << "Please note that 'make style' does NOT check variable names or"
       << " indentation" << endl << endl;
}

// For testing (DO NOT ALTER)
void Test(bool test, int line_number, string more_info, string yours,
          string actual) {
  ut_total++;
  if (test) {
    cout << "PASSED TEST ";
    ut_passed++;
  } else {
    cout << "FAILED TEST ";
    ut_failed++;
    failed_tests.push_back(ut_total);
  }
  cout << ut_total << " " << more_info << "!" << endl;
  if (!test) {
    if (yours != "!")
      cout << "Yours:  \"" << yours << '"' << endl;
    if (actual != "!")
      cout << "Actual: \"" << actual << '"' << endl;
    cout << "  Check Line " << line_number << " for more info" << endl;
  }
}

void OutputFailedTests() {
  if (failed_tests.size()) {
    cout << "Failed test number(s): ";
    for (unsigned int i = 0; i < failed_tests.size() - 1; i++)
      cout << failed_tests.at(i) << ", ";
    cout << failed_tests.at(failed_tests.size() - 1) << endl;
  }
}


These are my two headers and CPP files.

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
#include "sl_node.h"
#include <iostream>
#include <cmath>
#include <string>
#include <cctype>
#include <sstream>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::stringstream;

SLNode::SLNode() {
  next_node_ = NULL;
  contents_ = 0;
}

SLNode::SLNode(int size) {
  next_node_ = NULL;
  contents_ = size;
}

SLNode::~SLNode() {
  delete next_node_;
  contents_ = 0;
}

void SLNode::set_contents(int num) {
  contents_ = num;
}

int SLNode::contents() const {
  return contents_;
}

void SLNode::set_next_node(SLNode* node) {
  next_node_ = node;
}

SLNode* SLNode::next_node() const {
  return next_node_;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef SL_NODE_H
#define SL_NODE_H
#include <string>
#include <climits>

class SLNode {
 public:
  SLNode();
  SLNode(int size);
  ~SLNode();
  void set_contents(int num);
  int contents() const;
  void set_next_node(SLNode* node);
  SLNode* next_node() const;
 private:
  SLNode * next_node_;
  int contents_;
};

#endif /* SL_NODE_H */ 


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
#include "sl_list.h"
#include <iostream>
#include <cmath>
#include <string>
#include <cctype>
#include <sstream>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::stringstream;

SLList::SLList() {
  head_ = NULL;
  tail_ = NULL;
  size_ = 0;
}

SLList::~SLList() {
  Clear();
}

void SLList::Insert(int i) {
  SLNode * node;
  SLNode * node2;
  SLNode * node3;
  node = head_;
  if ((head_ == NULL) || (head_->contents() >= i)) {
    InsertHead(i);
  } else if (tail_->contents() < i) {
    InsertTail(i);
  } else {
    while (node->contents() < i) {
      node2 = node;
      node = node->next_node();
    }
    node3 = new SLNode(i);
    node3->set_next_node(node);
    node2->set_next_node(node3);
    size_++;
  }
}

bool SLList::RemoveFirstOccurence(int i) {
  SLNode * node;
  SLNode * prev;
  node = head_;
  prev = head_;
  while (node != NULL && node->contents() != i) {
    prev = node;
    node = node->next_node();
  }
  if (node != NULL) {
    if (node == head_) {
      RemoveHead();
    } else if (node == tail_) {
      RemoveTail();
    } else {
      prev->set_next_node(node->next_node());
      delete node;
      size_ --;
    }
    return true;
  }
  return false;
}

int SLList::GetHead() {
  if (size_ != 0) {
    return head_->contents();
  } else {
    return 0;
  }
}

int SLList::GetTail() {
  if (size_ != 0) {
    return tail_->contents();;
  } else {
    return 0;
  }
}

void SLList::Clear() {
  head_ = NULL;
  tail_ = NULL;
  size_ = 0;
}

unsigned int SLList::size() const {
  return size_;
}

string SLList::ToString() const {
  SLNode * node;
  node = head_;
  std::ostringstream ss;
  if (size_ != 0) {
    while (node != NULL) {
      ss << node->contents();
      if (node->next_node() != NULL) {
        ss << ", ";
      }
      node = node->next_node();
    }
  } else {
    ss << "";
  }
  return ss.str();
}

void SLList::InsertHead(int i) {
  SLNode * node;
  node = new SLNode(i);
  if (head_ == NULL) {
    tail_ = node;
  }
  node->set_next_node(head_);
  head_ = node;
  size_++;
}

void SLList::InsertTail(int i) {
  if (size_ == 0) {
    InsertHead(i);
  } else {
    SLNode * node;
    node = new SLNode(i);
    tail_->set_next_node(node);
    tail_ = node;
    size_++;
  }
}

void SLList::RemoveHead() {
  if (size_ != 0) {
    SLNode *node = head_;
    head_ = head_->next_node();
    delete node;
    size_--;
    if (size_ == 0) {
      tail_ = NULL;
    }
  }
}

void SLList::RemoveTail() {
  if (size_ == 1) {
    RemoveHead();
  } else if (size_ > 1) {
    SLNode * node = head_;
    while (node->next_node() != tail_) {
      node = node->next_node();
    }
    node->set_next_node(NULL);
    delete tail_;
    tail_ = node;
    size_--;
  }
}


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
#ifndef SL_LIST_H
#define SL_LIST_H
#include <string>
#include <climits>
#include "sl_node.h"

class SLList {
public:
  SLList();
  ~SLList();
  void Insert(int i);
  bool RemoveFirstOccurence(int i);
  int GetHead();
  int GetTail();
  void Clear();
  unsigned int size() const;
  std::string ToString() const;
private:
  SLNode * head_;
  SLNode * tail_;
  unsigned int size_;
  void InsertHead(int i);
  void InsertTail(int i);
  void RemoveHead();
  void RemoveTail();
};

#endif /* SL_LIST_H */ 


This is my compiler:

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
$ ./lab_20
UNIT TEST:
----------------------------------------
Total Number of Tests: 46
PASSED TEST 1 Default Constructor & size()!
PASSED TEST 2 ToString()!
PASSED TEST 3 GetHead()!
PASSED TEST 4 GetTail()!
PASSED TEST 5 Insert(10) & size()!
PASSED TEST 6 ToString()!
PASSED TEST 7 Insert(50) & size()!
PASSED TEST 8 ToString()!
PASSED TEST 9 Insert(30) & size()!
PASSED TEST 10 ToString()!
PASSED TEST 11 GetHead()!
PASSED TEST 12 GetTail()!
PASSED TEST 13 Insert(5) & size()!
PASSED TEST 14 ToString()!
PASSED TEST 15 Insert(55) & size()!
PASSED TEST 16 ToString()!
PASSED TEST 17 Insert(20) & size()!
PASSED TEST 18 ToString()!
PASSED TEST 19 Insert(40) & size()!
PASSED TEST 20 ToString()!
PASSED TEST 21 Insert(30) & size()!
PASSED TEST 22 ToString()!
PASSED TEST 23 Insert(5) & size()!
PASSED TEST 24 ToString()!
PASSED TEST 25 Insert(55) & size()!
PASSED TEST 26 ToString()!

Your List: 5, 5, 10, 20, 30, 30, 40, 50, 55, 55

PASSED TEST 27 RemoveFirstOccurence(1)!
Head
PASSED TEST 28 RemoveFirstOccurence(5)!
PASSED TEST 29 size()!
Segmentation fault (core dumped)


I'm really struggling as to why this is happening. Please help!
Topic archived. No new replies allowed.