Node Assignment

I have a node assignment that I'm having trouble with. Below are the instructions and the test code I need to past. After that I'll put down my own code and the current problem.

The purpose of this assignment is to continuing practicing creating dynamic structures. You will be creating a doubly-linked list.

Class 1-DLList: This class is for your doubly-linked list.

Private Data Members: The size of your list. Pointer to the head node. Pointer to the tail node

Constructor: Initializes head and tail to NULL, size to 0.

Destructor: Calls the Clear() Function

Member Function 1: Named GetSize. Returns the size of the list. Const function.

Member Function 2: Named PushFront. Has one parameter, an integer to add to the list. Creates a DLNode with the contents of the parameter and adds that node to the front of the list.

Member Function 3: Named PushBack. Has one parameter, an integer to add to the list.
Creates a DLNode with the contents of the parameter and adds that node to the end of the list.

Member Function 4: Named GetFront. Returns the integer value of the node at the beginning of the list. Const function. Output "List Empty" to standard error output if the list is empty and return 0.

Member Function 5: Named GetBack. Returns the integer value of the node at the end of the list.
Const function. Output "List Empty" to standard error output if the list is empty and return 0.

Member Function 6: Named PopFront. Removes the first node in the list. Output "List Empty" to standard error output if the list was already empty.

Member Function 7: Named PopBack. Removes the last node in the list. Output "List Empty" to standard error output if the list was already empty.

Member Function 8: Named RemoveFirst. Has one parameter, an integer to find. If the value is found, remove that node from the list. Will only remove the first node that matches the value. If the value is not found, output "Not Found" to standard error output.

Member Function 9: Named RemoveAll. Has one parameter, an integer to find. If the value is found, remove that node from the list. Will remove all nodes that match the value. If the value is not found, output "Not Found" to standard error output.

Member Function 10: Named Exists. Has one parameter, an integer to find. If the value is found, return true, else return false.

Member Function 11: Named Clear. Clears all nodes from the list, resets the size to 0 and head and tail to NULL

Member Function 12: Named ToStringForwards. Outputs the contents of the list as a comma separated list ("1, 2, 3, etc") starting at the first node and ending at the last node. If the list is empty return the empty string and output "List Empty" to standard error output.

Member Function 13: Named ToStringBackwards. Outputs the contents of the list as a comma separated list ("1, 2, 3, etc") starting at the last node and ending at the first node. If the list is empty return the empty string and output "List Empty"to standard error output.

---

Class 2-DLNode: Models information about a single node in the DLList.

Private Data Members: An integer for the contents of the node. A pointer to the previous node. A pointer to the next node.

Constructor#1: No parameters. Sets the integer to 0Sets the previous node to NULL. Sets the next pointer to NULL.

Destructor: Is empty.

Mutator 1: Named SetContents. Has one integer parameter. Sets the internal integer to the given parameter's value.

Mutator 2: Named SetNext. Has one parameter which is a pointer to a DLNode. Sets the internal next pointer to the given parameter's value.

Mutator 3: Named SetPrevious. Has one parameter which is a pointer to a DLNode. Sets the internal previous pointer to the given parameter's value.

Accessor 1: Named GetContents. Returns the value of the internal integer. Const function.

Accessor 2: Named GetNext. Returns the pointer to the next node. Const function.

Accessor 3: Named GetPrevious. Returns the pointer to the previous node. Const function.
This is the unit test provided:

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
#include "dl_list.h"
#include "dl_node.h"
#include <sstream>
#include <iostream>
using std::cout;
using std::endl;
using std::string;

// For testing (DO NOT ALTER)
void Test(bool test, string more_info = "");
void UnitTest();
unsigned int ut_passed = 0, ut_failed = 0, ut_total = 0;

// 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() {
  string temp = "This unit test will test some of your code:\n";
  cout << temp << string(temp.length() - 1, '-') << endl;
  cout << "There are 112 tests!" << endl;
  // Tests
  DLNode *node1 = new DLNode;

  Test(node1->GetContents() == 0, "Default Constructor & GetContents()");
  Test(node1->GetNext() == NULL, "Default Constructor & GetNext()");
  Test(node1->GetPrevious() == NULL, "Default Constructor & GetPrevious()");

  DLNode *node2 = new DLNode;
  node1->SetNext(node2);
  Test(node1->GetNext() == node2, "SetNext() & GetNext()");
  node2->SetPrevious(node1);
  Test(node2->GetPrevious() == node1, "SetPrevious() & GetPrevious()");

  node1->SetNext(NULL);
  Test(node1->GetNext() == NULL, "SetNext(NULL) & GetNext()");
  node2->SetPrevious(NULL);
  Test(node2->GetPrevious() == NULL, "SetPrevious(NULL) & GetPrevious()");

  delete node1;
  delete node2;
  Test(true, "Destructor");

  DLList list;
  std::stringstream sserr;
  std::streambuf* old_cerr = std::cerr.rdbuf(sserr.rdbuf());
  string empty = "List Empty";
  string no_value = "Not Found";
  std::stringstream full_head_list, half_head_list, full_tail_list,
      half_tail_list;
  ;
  for (int i = 999; i > 0; i--) {
    full_head_list << i << ", ";
    if (i < 500)
      half_head_list << i << ", ";
  }
  full_head_list << 0;
  half_head_list << 0;

  for (int i = 0; i < 999; i++) {
    full_tail_list << i << ", ";
    if (i < 499)
      half_tail_list << i << ", ";
  }
  full_tail_list << 999;
  half_tail_list << 499;

  Test(list.GetSize() == 0, "Default Constructor & GetSize()");
  Test(list.ToStringForwards() == "" && sserr.str() == empty,
       "ToStringForwards()");
  sserr.str("");
  Test(list.ToStringBackwards() == "" && sserr.str() == empty,
       "ToStringBackwards()");
  sserr.str("");
  Test(list.GetFront() == 0 && sserr.str() == empty, "GetFront()");
  sserr.str("");
  Test(list.GetBack() == 0 && sserr.str() == empty, "GetBack()");
  sserr.str("");
  Test(list.Exists(10) == false, "Exists(10)");

  list.RemoveFirst(0);
  Test(sserr.str() == no_value, "RemoveFirst(0)");
  sserr.str("");
  list.RemoveAll(0);
  Test(sserr.str() == no_value, "RemoveAll(0)");
  sserr.str("");

  list.PopFront();
  Test(list.GetSize() == 0 && sserr.str() == empty, "PopFront() & GetSize()");
  sserr.str("");

  list.PopBack();
  Test(list.GetSize() == 0 && sserr.str() == empty, "PopBack() & GetSize()");
  sserr.str("");

  list.PushFront(1);
  Test(list.GetSize() == 1, "PushFront(1) & GetSize()");
  Test(list.ToStringForwards() == "1", "ToStringForwards()");
  Test(list.ToStringBackwards() == "1", "ToStringBackwards()");
  Test(list.Exists(1) == true, "Exists(1)");

  list.PopFront();
  Test(list.GetSize() == 0, "PopFront() & GetSize()");
  Test(list.ToStringForwards() == "", "ToStringForwards()");
  Test(list.ToStringBackwards() == "", "ToStringBackwards()");

  list.PushBack(5);
  Test(list.GetSize() == 1, "PushBack(5) & GetSize()");
  Test(list.ToStringForwards() == "5", "ToStringForwards()");
  Test(list.ToStringBackwards() == "5", "ToStringBackwards()");

  list.PopBack();
  Test(list.GetSize() == 0, "PopBack() & GetSize()");
  Test(list.ToStringForwards() == "", "ToStringForwards()");
  Test(list.ToStringBackwards() == "", "ToStringBackwards()");

  list.PushFront(10);
  list.PushFront(20);
  Test(list.GetSize() == 2, "PushFront(10), PushFront(20) & GetSize()");
  Test(list.ToStringForwards() == "20, 10", "ToStringForwards()");
  Test(list.ToStringBackwards() == "10, 20", "ToStringBackwards()");
  Test(list.Exists(10) == true, "Exists(10)");
  Test(list.Exists(1) == false, "Exists(1)");

  list.PopFront();
  Test(list.GetSize() == 1, "PopFront() & GetSize()");
  Test(list.ToStringForwards() == "10", "ToStringForwards()");
  Test(list.ToStringBackwards() == "10", "ToStringBackwards()");

  list.PushFront(20);
  list.PopBack();
  Test(list.GetSize() == 1, "PushFront(20), PopBack() & GetSize()");
  Test(list.ToStringForwards() == "20", "ToStringForwards()");
  Test(list.ToStringBackwards() == "20", "ToStringBackwards()");

  list.PushFront(5);
  Test(list.GetSize() == 2, "PushFront(5) & GetSize()");
  Test(list.ToStringForwards() == "5, 20", "ToStringForwards()");
  Test(list.ToStringBackwards() == "20, 5", "ToStringBackwards()");

  list.Clear();
  Test(list.GetSize() == 0, "Clear() & GetSize()");
  Test(list.ToStringForwards() == "", "ToStringForwards()");
  Test(list.ToStringBackwards() == "", "ToStringBackwards()");

  list.PushFront(10);
  list.PushFront(5);
  list.PushBack(20);
  list.PushBack(25);
  Test(list.GetSize() == 4,
       "PushFront(10), PushFront(5), PushBack(20), PushBack(25) & GetSize()");
  Test(list.ToStringForwards() == "5, 10, 20, 25", "ToStringForwards()");
  Test(list.ToStringBackwards() == "25, 20, 10, 5", "ToStringBackwards()");
  Test(list.Exists(10) == true, "Exists(10)");

  Test(list.GetFront() == 5, "GetFront()");
  Test(list.GetBack() == 25, "GetBack()");
  Test(list.GetSize() == 4, "GetSize()");
  Test(list.ToStringForwards() == "5, 10, 20, 25", "ToStringForwards()");
  Test(list.ToStringBackwards() == "25, 20, 10, 5", "ToStringBackwards()");

  list.PopFront();
  list.PopBack();
  list.PopFront();
  list.PopBack();
  Test(list.GetSize() == 0,
       "PopFront(), PopBack(), PopFront(), PopBack() & GetSize()");
  Test(list.ToStringForwards() == "", "ToStringForwards()");
  Test(list.ToStringBackwards() == "", "ToStringBackwards()");
  Test(list.Exists(10) == false, "Exists(10)");
//length too big 

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
//continued
  list.Clear();
  list.PushFront(20);
  list.PushFront(30);
  list.PushFront(40);
  list.PushFront(50);
  list.PushBack(60);
  list.PushBack(70);
  list.PushBack(80);
  list.PushBack(10);\
  Test(list.GetSize() == 8,
       "PushFront(20, 30, 40 ,50), PushBack(60, 70, 80, 10");
  Test(list.ToStringForwards() == "50, 40, 30, 20, 60, 70, 80, 10",
       "ToStringForwards()");
  Test(list.ToStringBackwards() == "10, 80, 70, 60, 20, 30, 40, 50",
       "ToStringBackwards()");

  sserr.str("");
  list.RemoveFirst(1);
  Test(sserr.str() == no_value, "RemoveFirst(1)");
  sserr.str("");
  list.RemoveFirst(50);
  Test(sserr.str() == "", "RemoveFirst(50)");
  Test(list.GetSize() == 7, "GetSize()");
  Test(list.ToStringForwards() == "40, 30, 20, 60, 70, 80, 10",
       "ToStringForwards()");
  Test(list.ToStringBackwards() == "10, 80, 70, 60, 20, 30, 40",
       "ToStringBackwards()");
  sserr.str("");
  list.RemoveFirst(10);
  Test(sserr.str() == "", "RemoveFirst(10)");
  Test(list.GetSize() == 6, "GetSize()");
  Test(list.ToStringForwards() == "40, 30, 20, 60, 70, 80",
       "ToStringForwards()");
  Test(list.ToStringBackwards() == "80, 70, 60, 20, 30, 40",
       "ToStringBackwards()");
  sserr.str("");
  list.RemoveFirst(30);
  Test(sserr.str() == "", "RemoveFirst(30)");
  Test(list.GetSize() == 5, "GetSize()");
  Test(list.ToStringForwards() == "40, 20, 60, 70, 80", "ToStringForwards()");
  Test(list.ToStringBackwards() == "80, 70, 60, 20, 40", "ToStringBackwards()");
  list.Clear();
  list.PushFront(20);
  list.PushFront(30);
  list.PushFront(40);
  list.PushFront(50);
  list.PushBack(50);
  list.PushBack(70);
  list.PushBack(80);
  list.PushBack(20);
  Test(list.GetSize() == 8,
       "PushFront(20, 30, 40 ,50), PushBack(50, 70, 80, 20");
  Test(list.ToStringForwards() == "50, 40, 30, 20, 50, 70, 80, 20",
       "ToStringForwards()");
  Test(list.ToStringBackwards() == "20, 80, 70, 50, 20, 30, 40, 50",
       "ToStringBackwards()");
  list.RemoveAll(20);
  Test(list.GetSize() == 6, "RemoveAll(20), GetSize()");
  Test(list.ToStringForwards() == "50, 40, 30, 50, 70, 80",
       "ToStringForwards()");
  Test(list.ToStringBackwards() == "80, 70, 50, 30, 40, 50",
       "ToStringBackwards()");

  sserr.str("");
  list.RemoveAll(15);
  Test(sserr.str() == no_value, "RemoveAll(15)");
  sserr.str("");

  list.RemoveAll(80);
  Test(list.GetSize() == 5, "RemoveAll(80), GetSize()");
  Test(list.ToStringForwards() == "50, 40, 30, 50, 70", "ToStringForwards()");
  Test(list.ToStringBackwards() == "70, 50, 30, 40, 50", "ToStringBackwards()");

  list.RemoveAll(50);
  Test(list.GetSize() == 3, "RemoveAll(50), GetSize()");
  Test(list.ToStringForwards() == "40, 30, 70", "ToStringForwards()");
  Test(list.ToStringBackwards() == "70, 30, 40", "ToStringBackwards()");

  list.Clear();
  for (unsigned int i = 0; i < 1000; i++)
    list.PushFront(i);
  Test(list.GetSize() == 1000, "PushFront() \"HIGH LOAD\" & GetSize()");
  Test(list.ToStringForwards() == full_head_list.str(), "ToStringForwards()");
  Test(list.ToStringBackwards() == full_tail_list.str(), "ToStringBackwards()");
  Test(list.Exists(500) == true, "Exists(500)");
  Test(list.Exists(-1) == false, "Exists(-1)");

  for (unsigned int i = 0; i < 500; i++)
    list.PopFront();
  Test(list.GetSize() == 500, "PopFront() \"HIGH LOAD / 2\" & GetSize()");
  Test(list.ToStringForwards() == half_head_list.str(), "ToStringForwards()");
  Test(list.ToStringBackwards() == half_tail_list.str(), "ToStringBackwards()");
  for (unsigned int i = 0; i < 600; i++)
    list.PopFront();
  Test(list.GetSize() == 0, "PopFront() \"HIGH LOAD / 2\" & GetSize()");
  Test(list.ToStringForwards() == "", "ToStringForwards()");
  Test(list.ToStringBackwards() == "", "ToStringBackwards()");

  for (unsigned int i = 0; i < 1000; i++)
    list.PushBack(i);
  Test(list.GetSize() == 1000, "PushBack() \"HIGH LOAD\" & GetSize()");
  Test(list.ToStringForwards() == full_tail_list.str(), "ToStringForwards()");
  Test(list.ToStringBackwards() == full_head_list.str(), "ToStringBackwards()");
  Test(list.Exists(500) == true, "Exists(500)");
  Test(list.Exists(-1) == false, "Exists(-1)");
  for (unsigned int i = 0; i < 500; i++)
    list.PopBack();
  Test(list.GetSize() == 500, "PopBack() \"HIGH LOAD / 2\" & GetSize()");
  Test(list.ToStringForwards() == half_tail_list.str(), "ToStringForwards()");
  Test(list.ToStringBackwards() == half_head_list.str(), "ToStringBackwards()");
  for (unsigned int i = 0; i < 600; i++)
    list.PopBack();
  Test(list.GetSize() == 0, "PopBack() \"HIGH LOAD / 2\" & GetSize()");
  Test(list.ToStringForwards() == "", "ToStringForwards()");
  Test(list.ToStringBackwards() == "", "ToStringBackwards()");

  // Restore cerr
  std::cerr.rdbuf(old_cerr);
  cout << string(temp.length() - 1, '-') << endl;
  cout << "Unit Test Complete!\n" << "Passed: " << ut_passed << " / "
       << ut_total << endl << "Failed: " << ut_failed << " / " << ut_total
       << endl << endl;
}

// For testing (DO NOT ALTER)
void Test(bool test, string more_info) {
  static int test_number = 1;
  if (test) {
    cout << "PASSSED TEST ";
    ut_passed++;
  } else {
    cout << "FAILED  TEST ";
    ut_failed++;
  }
  cout << test_number << " " << more_info << "!" << endl;
  test_number++;
  ut_total++;
}
This is my current code: two CPP files and two headers:

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
#include "dl_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;

DLNode::DLNode() {
  previous_node_ = NULL;
  next_node_ = NULL;
  contents_ = 0;
}

DLNode::~DLNode() {
}

void DLNode::SetContents(int num) {
  contents_ = num;
}

void DLNode::SetNext(DLNode* node) {
  next_node_ = node;
}

void DLNode::SetPrevious(DLNode* node) {
  previous_node_ = node;
}

int DLNode::GetContents() const {
  return contents_;
}

DLNode* DLNode::GetNext() const {
  return next_node_;
}

DLNode* DLNode::GetPrevious() const {
  return previous_node_;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef DL_NODE_H
#define DL_NODE_H
#include <string>
#include <climits>
  
class DLNode {
public:
  DLNode();
  ~DLNode();
  void SetContents(int num);
  void SetNext(DLNode* node);
  void SetPrevious(DLNode* node);
  int GetContents() const;
  DLNode* GetNext() const;
  DLNode* GetPrevious() const;
private:
  DLNode * previous_node_;
  DLNode * next_node_;
  int contents_;
};

#endif /* DL_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
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 "dl_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;

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

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

int DLList::GetSize() const {
  int i = 0;
  DLNode * node;
  node = head_;
  while (node != NULL) {
    node = node->GetNext();
    i++;
  }
  return i;
}

void DLList::PushFront(int i) {
  DLNode * node;
  node = new DLNode();
  if (head_ == NULL) {
    tail_ = node;
  }
  node->SetNext(head_);
  head_ = node;
  size_++;
}

void DLList::PushBack(int i) {
  if (size_ == 0) {
    PushFront(i);
  } else {
    DLNode * node;
    node = new DLNode();
    tail_->SetNext(node);
    tail_ = node;
    size_++;
  }
}

int DLList::GetFront() const {
  if (size_ == 0) {
    cout << "List Empty" << endl;
    return 0;
  } else {
    return head_->GetContents();
  }
}

int DLList::GetBack() const {
  if (size_ == 0) {
    cout << "List Empty" << endl;
    return 0;
  } else {
    return tail_->GetContents();
  }
}

void DLList::PopFront() {
  if (size_ != 0) {
    DLNode * node;
    node = head_;
    head_ = node->GetNext();
    delete node;
    size_--;
    if (size_ == 0) {
      tail_ = NULL;
    }
  } else {
    cout << "List Empty" << endl;
  } 
}

void DLList::PopBack() {
  if (size_ == 1) {
    PopFront();
  } else if (size_ > 1) {
    DLNode * node = head_;
    while (node->GetNext() != tail_) {
      node = node->GetNext();
    }
    node->SetNext(NULL);
    delete tail_;
    tail_ = node;
    size_--;
  } else {
    cout << "List Empty" << endl;
  }
}

void DLList::RemoveFirst(int i) {
  DLNode * node;
  node = head_;
  if (Exists(i) == true) {
    while ((node != NULL) && (Exists(i) == false)) {
      node = node->GetNext();
      if (node->GetContents() == i) {
        delete node;
      }
    }
  } else {
    cout << "Not Found" << endl;
  }
}

void DLList::RemoveAll(int i) {
  DLNode * node;
  node = head_;
  if (Exists(i) == true) {
    while (node != NULL) {
      node = node->GetNext();
      if (node->GetContents() == i) {
        delete node;
      }
    }
  } else {
    cout << "Not Found" << endl;
  }
}

bool DLList::Exists (int i) {
  DLNode * node;
  node = head_;
  bool found = true;
  while (node != NULL) {
    node = node->GetNext();
    if (node->GetContents() == i) {
      found = true;
    }
  }
  return found;
}

void DLList::Clear() {
  DLNode * node;
  node = head_;
  head_ = NULL;
  tail_ = NULL;
  size_ = 0;
  while (node != NULL) {
    delete node;
    node = node->GetNext();
  }
}

string DLList::ToStringForwards() const {
  DLNode * node;
  node = head_;
  std::ostringstream ss;
  if (size_ != 0) {
    while (node != NULL) {
      ss << node->GetContents();
      if (node->GetNext() != NULL) {
        ss << ", ";
      }
      node = node->GetNext();
    }
  } else {
    cout << "List Empty" << endl;
  }
  return ss.str();
}

string DLList::ToStringBackwards() const {
  DLNode * node;
  node = head_;
  std::ostringstream ss;
  if (size_ != 0) {
    while (node != NULL) {
      ss << node->GetContents();
      if (node->GetPrevious() != NULL) {
        ss << ", ";
      }
      node = node->GetPrevious();
    }
  } else {
    cout << "List Empty" << endl;
  }
  return ss.str();
}


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

class DLList {
public:
  DLList();
  ~DLList();
  int GetSize() const;
  void PushFront(int i);
  void PushBack(int i);
  int GetFront() const;
  int GetBack() const;
  void PopFront();
  void PopBack();
  void RemoveFirst(int i);
  void RemoveAll(int i);
  bool Exists (int i);
  void Clear();
  std::string ToStringForwards() const;
  std::string ToStringBackwards() const;
private:
  DLNode * head_;
  DLNode * tail_;
  unsigned int size_;
};

#endif /* DL_LIST_H */ 


And right now this is the output the test is giving me:

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
This unit test will test some of your code:
There are 112 tests!
PASSSED TEST 1 Default Constructor & GetContents()!
PASSSED TEST 2 Default Constructor & GetNext()!
PASSSED TEST 3 Default Constructor & GetPrevious()!
PASSSED TEST 4 SetNext() & GetNext()!
PASSSED TEST 5 SetPrevious() & GetPrevious()!
PASSSED TEST 6 SetNext(NULL) & GetNext()!
PASSSED TEST 7 SetPrevious(NULL) & GetPrevious()!
PASSSED TEST 8 Destructor!
PASSSED TEST 9 Default Constructor & GetSize()!
List Empty
FAILED  TEST 10 ToStringForwards()!
List Empty
FAILED  TEST 11 ToStringBackwards()!
List Empty
FAILED  TEST 12 GetFront()!
List Empty
FAILED  TEST 13 GetBack()!
FAILED  TEST 14 Exists(10)!
FAILED  TEST 15 RemoveFirst(0)!
FAILED  TEST 16 RemoveAll(0)!
List Empty
FAILED  TEST 17 PopFront() & GetSize()!
List Empty
FAILED  TEST 18 PopBack() & GetSize()!
PASSSED TEST 19 PushFront(1) & GetSize()!
FAILED  TEST 20 ToStringForwards()!
FAILED  TEST 21 ToStringBackwards()!
Segmentation fault (core dumped)


So starting with Test 10, can anyone see what's wrong with ToStringForwards?
You are supposed to return an empty string. When the instructions say" standard error output", it is referring to cerr.

http://en.cppreference.com/w/cpp/io/cerr
Ahhh OK...I changed all the couts. There's still an issue though.

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
$ ./a.exe
This unit test will test some of your code:
There are 112 tests!
PASSSED TEST 1 Default Constructor & GetContents()!
PASSSED TEST 2 Default Constructor & GetNext()!
PASSSED TEST 3 Default Constructor & GetPrevious()!
PASSSED TEST 4 SetNext() & GetNext()!
PASSSED TEST 5 SetPrevious() & GetPrevious()!
PASSSED TEST 6 SetNext(NULL) & GetNext()!
PASSSED TEST 7 SetPrevious(NULL) & GetPrevious()!
PASSSED TEST 8 Destructor!
PASSSED TEST 9 Default Constructor & GetSize()!
FAILED  TEST 10 ToStringForwards()!
FAILED  TEST 11 ToStringBackwards()!
List Empty
FAILED  TEST 12 GetFront()!
List Empty
FAILED  TEST 13 GetBack()!
FAILED  TEST 14 Exists(10)!
FAILED  TEST 15 RemoveFirst(0)!
FAILED  TEST 16 RemoveAll(0)!
FAILED  TEST 17 PopFront() & GetSize()!
FAILED  TEST 18 PopBack() & GetSize()!
PASSSED TEST 19 PushFront(1) & GetSize()!
FAILED  TEST 20 ToStringForwards()!
FAILED  TEST 21 ToStringBackwards()!
Segmentation fault (core dumped)


Starting with ToStringForwards, this is my code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
string DLList::ToStringForwards() const {
  DLNode * node;
  node = head_;
  std::ostringstream ss;
  if (size_ != 0) {
    while (node != NULL) {
      ss << node->GetContents();
      if (node->GetNext() != NULL) {
        ss << ", ";
      }
      node = node->GetNext();
    }
  } else {
    std::cerr << "List Empty" << endl;
    ss << "";
  }
  return ss.str();
}


What's wrong with it now?
Last edited on
I don't know, lines 74 and 75 of what you posted before is the test for it. From what I can tell, you should have passed.
Topic archived. No new replies allowed.