-1

Hello everyone, first post here. So its going into the weekend and I don't have access to my course instructor. This is our first project using linked lists and I'm mangling it pretty good. The fault is, I'm pretty sure, on line 53 of sl_list.cpp output << temp -> contents(); This is meant to access the contents of the node, in this case an int, and store it in a stringstream variable so that I can output the whole thing later. Clearly I'm not doing it right. I've been trying to fix it for the better part of a day and a half but I just don't know what I'm doing wrong. All of the files beginning with "sl" are mine, the exercise_18.cpp is testing code created by my instructor. If I comment out the offending function my code compiles and runs as expected. Can anyone here tell me what I've screwed up? Its probably something pretty easily spotted. Thank you.

#include "sl_list.h"
#include <iostream>
#include <sstream>
using namespace std;
using std::stringstream;

SLList::SLList() {  // Sets head_ to NULL and size_ to 0
  head_ = NULL;
  size_ = 0;
}
SLList::~SLList() {  // Destructor for SLList
}
void SLList::InsertHead(int number) {  // Creates new dynamic SLNode
  SLNode* to_insert = new SLNode(number);
  to_insert -> set_next_node(head_);
  head_ = to_insert;
  size_++;
}
void SLList::RemoveHead() {  // Remove the SLNode from the list
  if (head_ != NULL) {  // Check for NULL
    SLNode* temp = head_;  // Storage for temp location
    head_ = head_ -> next_node();  // Set head to next_node
    delete temp;  // Delete temp storage
    size_--;  // Decrement size
  }
}
void SLList::Clear() {  // Clear the entire contents of the list
  if (head_ != NULL) {  // Check for NULL
    while (head_ != NULL) {  // Check for NULL
      SLNode* temp = head_;  // Storage for temp location
      head_ = head_ -> next_node();  // Set head to next_node
      delete temp;  // Delete temp storage
      size_--;  // Decrement size
    }
  }
}
unsigned int SLList::size() const {  // return size_
  return size_;
}
string SLList::ToString() const {  // returns the contents of all nodes
  if (head_ != NULL) {  // Check for NULL
    stringstream output;  // Storage for stringstream
    SLNode* temp = head_;  // Storage for temp location
      while (temp != NULL) {  // Check for NULL
        temp = temp -> next_node();  // Set head to next_node
        output << temp -> contents();  // Dumping temp into output
        if (temp != NULL) {  // Add a space and comma to the end of output
          output << ", ";
        }
      }
    return output.str();  // Return output
  } else {
    return "";  // If head_ was NULL return empty string
  }
}

#ifndef SL_LIST_H
#define SL_LIST_H

#include "sl_node.h"
#include <iostream>
using std::string;

class SLList {
 public:
  SLList();  // Default constructor
  ~SLList();  // Default destructor
  void InsertHead(int number);  // Creates new dynamic SLNode
  void RemoveHead();  // Remove the SLNode from the list
  void Clear();  // clear the entire contents of the list
  unsigned int size() const;  // Return size_
  string ToString() const;  // returns the contents of all nodes

 private:
  SLNode* head_;  // Storage for the next node locations
  unsigned int size_;  // Storage for the size
};

#endif /* SL_LIST.H */

#include "sl_node.h"
#include <iostream>

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

SLNode::SLNode(unsigned int contents) {  // overloaded constructor
  next_node_ = NULL;
  contents_ = contents;
}

SLNode::~SLNode() {  // Empty destructor
}

void SLNode::set_contents(int contents) {  // Set contents_ to contents
  contents_ = contents;
}

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

void SLNode::set_next_node(SLNode* next_node) {  // Sets location of next node
  next_node_ = next_node;
}

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

#ifndef SL_NODE_H
#define SL_NODE_H

class SLNode {
 public:
  SLNode();  // Default constructor
  SLNode(unsigned int contents);  // Overloaded constructor
  ~SLNode();  // Empty destructor
  void set_contents(int contents);  // Set contents_ to contents
  int contents() const;  // Return contents_
  void set_next_node(SLNode* next_node);  // Sets location of next node
  SLNode* next_node() const;  // Return next_node_

 private:
  SLNode* next_node_;  // Storage for the next node locations
  int contents_;  // Storage for the contents of the node
};

#endif /* SL_NODE.H */

#include "sl_list.h"
#include "sl_node.h"
#include <sstream>
#include <iostream>
using std::cout;
using std::endl;
using std::string;
using std::stringstream;

// For testing (DO NOT ALTER)
void Test(bool test, string more_info = "");
void UnitTest();

// 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;
  // Tests
  SLList list;
  stringstream full_list, half_list;
  for (int i = 999; i > 0; i--) {
    full_list << i << ", ";
    if (i < 500)
      half_list << i << ", ";
  }
  full_list << 0;
  half_list << 0;

  Test(list.size() == 0, "Default Constructor & size()");
  Test(list.ToString() == "", "ToString()");

  list.RemoveHead();
  Test(list.size() == 0, "RemoveHead() & size()");

  list.InsertHead(1);
  Test(list.size() == 1, "InsertHead(1) & size()");
  Test(list.ToString() == "1", "ToString()");

  list.RemoveHead();
  Test(list.size() == 0, "RemoveHead() & size()");
  Test(list.ToString() == "", "ToString()");

  list.InsertHead(10);
  list.InsertHead(20);
  Test(list.size() == 2, "InsertHead(10), InsertHead(20) & size()");
  Test(list.ToString() == "20, 10", "ToString()");

  list.RemoveHead();
  Test(list.size() == 1, "RemoveHead() & size()");
  Test(list.ToString() == "10", "ToString()");

  list.InsertHead(5);
  Test(list.size() == 2, "InsertHead(5) & size()");
  Test(list.ToString() == "5, 10", "ToString()");

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

  for (unsigned int i = 0; i < 1000; i++)
    list.InsertHead(i);
  Test(list.size() == 1000, "InsertHead() \"HIGH LOAD\" & size()");
  Test(list.ToString() == full_list.str(), "ToString()");

  for (unsigned int i = 0; i < 500; i++)
    list.RemoveHead();
  Test(list.size() == 500, "RemoveHead() \"HIGH LOAD / 2\" & size()");
  Test(list.ToString() == half_list.str(), "ToString()");
  for (unsigned int i = 0; i < 600; i++)
    list.RemoveHead();
  Test(list.size() == 0, "RemoveHead() \"HIGH LOAD / 2\" & size()");
  Test(list.ToString() == "", "ToString()");

  cout << string(temp.length() - 1, '-') << endl;
  cout << "Unit Test Complete!\n\n";
}

// For testing (DO NOT ALTER)
void Test(bool test, string more_info) {
  static int test_number = 1;
  if (test)
    cout << "PASSSED TEST ";
  else
    cout << "FAILED  TEST ";
  cout << test_number << " " << more_info << "!\n";
  test_number++;
}
Lando242
  • 3
  • 1
  • Please enable core dumps, build with debugging symbols, and either run it and analyze the core dump, or run it under the debugger and see where it faults and why. – wallyk Apr 12 '14 at 02:26
  • The instructor has us doing all this on koding.com, which does not have a debugger in Ace to the best of my knowledge. – Lando242 Apr 12 '14 at 02:33
  • 2
    Unless access to other free tools is prevented somehow, what is stopping use of the GCC or LLVM compiler on your own equipment? – CPlusPlus OOA and D Apr 12 '14 at 03:06
  • I'm using a lab computer for this on my own time, since class ended on Thursday. The lab systems are locked down and re-imaged after every reboot, which is why we use a web based VM to code our projects. On top of that I haven't a clue how to use other compilers. I did some python work in Eclipse and was completely lost in its debugger. Its not terribly user friendly. I don't even know what to look for to be honest :( – Lando242 Apr 12 '14 at 03:24

1 Answers1

0

In your output loop, you are moving temp on to the next node before you output its value. Just swap these two lines around.

temp = temp -> next_node();  // Set head to next_node
output << temp -> contents();  // Dumping temp into output

should become

output << temp -> contents();  // Dumping temp into output
temp = temp -> next_node();  // Set head to next_node
The Dark
  • 8,453
  • 1
  • 16
  • 19
  • Ugh, I knew it would be something stupid. That fixed it right up. Thank you very much for the help. – Lando242 Apr 12 '14 at 03:38
  • 1
    You'd better add a destructor too, and delete the copy-constructor and copy-assignment op to avoid accidents. – M.M Apr 12 '14 at 06:20