title: Using the C++ list standard library to help in linked-list program
body: <p>I'm working on a C++ program that is supposed to utilize a linked list to create a hypercard stack (whatever that is). The problem is, I have no idea what I'm doing. I've written some code, but it crashes, and honestly I have no idea if it even meets the specifications for what I need it to do if it did work as written. Here's the code I've come up with. </p>
  
  <p><strong>Update</strong>
  Also, as some of you have mentioned, I'd like to use the <code>std::list</code> library if that would make this easier, but I have no idea how to do so, any help would be greatly appreciated.</p>
  
  <p>This is my .h file:</p>
  
  <pre><code>//program6.h
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;list&gt;
  using namespace std;
  
  class Node {
  public:
      Node();
      Node(char code, int num, string data);
      Node(Node &amp; node);
      ~Node();
  
      bool readFile();
      void setNext(Node* next);
      void print();
  
  private:
      char Code;
      int Num;
      string Data;
      Node *Next;
  };
  </code></pre>
  
  <p>My Implementation File:</p>
  
  <pre><code>//program6.cpp
  #include "program6.h"
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;list&gt;
  using namespace std;
  
  class Node {
  public:
      Node();
      Node(char code, int num, string data);
      Node(Node &amp; node);
      ~Node();
  
      bool readFile();
      void setNext(Node* next);
      void print();
  
  private:
      char Code;
      int Num;
      string Data;
      Node *Next;
  };
  
  Node::Node() {
      Code = '\0';
      Num = 0;
      Data = "";
      Next = NULL;
  }
  
  Node::Node(char code, int num, string data) {
      char Code = code;
      int Num = num;
      string Data = data;
      Next = NULL;
  }
  
  Node::Node(Node &amp; node) {
      Code = node.Code;
      Num = node.Num;
      Data = node.Data;
      Next = NULL;
  }
  
  Node::~Node() {
  }
  
  bool Node::readFile() {
      char code = '\0';
      int num = 0;
      string data = "";
  
      ifstream inputFile;
      inputFile.open("prog6.dat");
  
      if(!inputFile) {
          cerr &lt;&lt; "Open Faiulre" &lt;&lt; endl;
          exit(1);
          return false;
      }
  
      Node *head = NULL;
      while(!inputFile.eof()) {
          inputFile &gt;&gt; code &gt;&gt; num &gt;&gt; data;
  
          Node *temp = new Node(code, num, data);
          temp-&gt;setNext(head);
          head = temp;
      }
  
      inputFile.close();
      head-&gt;print();
      return true;
  }
  
  void Node::setNext(Node* next) {
      Next = next;
  }
  
  void Node::print() {
      cout &lt;&lt; Code &lt;&lt; " " &lt;&lt; Num &lt;&lt; " " &lt;&lt; Data;
      if(Next != NULL) 
          Next-&gt;print();
  }
  </code></pre>
  
  <p>And my main/test file:</p>
  
  <pre><code>//program6test.cpp
  #include "program6.h"
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;list&gt;
  using namespace std;
  
  int main() {
      Node list;
      if(list.readFile()) 
          cout &lt;&lt; "Success" &lt;&lt; endl;
      else
          cout &lt;&lt; "Failure" &lt;&lt; endl;
  
      return 0;
  }
  </code></pre>
  
  <p>And here is the output I get when I compile and run this program:</p>
  
  <pre><code>[cs331129@cs ~]$ g++ -o prog6 program6test.cpp
  [cs331129@cs ~]$ prog6
  terminate called after throwing an instance of 'St9bad_alloc'
    what():  St9bad_alloc
  Aborted
  </code></pre>
  
  <p>And here are the "directions" I was given:</p>
  
  <p>The hypercard “stack” concept is more general than a stack since “cards”
  (elements) can be inserted anywhere and the entire “stack” can be traversed back
  to the “home card”. Using circular lists, implement a hypercard stack.</p>
  
  <p>Suppose we have a class or record structure (below) that contains a key code
  (integer), a string of information (type Entry) 25 characters or fewer and a pointer</p>
  
  <pre><code>typedef struct elem_tag {
     Key key;
     Entry fact;
     struct elem_tag * link;
  } Node_type;
  </code></pre>
  
  <p>We can create a “hypercard stack”, read in information from a file, build a linked list of cards, and perform operations (<code>insert(i)</code>, <code>delete(d)</code>, <code>traverse(t)</code>, <code>home(h)</code>, <code>forward(f)</code>, <code>print(p)</code>) on the list. For example, given a data file containing:</p>
  
  <pre><code>i 27 Mary had a little lamb
  i 15 Today is a good day
  i 35 Now is the time!
  i 9 This lab is easy and fun
  p
  </code></pre>
  
  <p>we produce a “hypercard stack” below.</p>
  
  <pre><code>                                                         -------Tail/Current
                                                           v
  ---&gt;   27       ---&gt;   15        ---&gt;   35       ---&gt;   9       ---
  |      Mary...         Today...         Now...          This...    |
  ----------------------------------------------------------------------------
  </code></pre>
  
  <p>Note that 27 is the “home card” (the home card is the next one after the one tail points to) and the card printed will be associate with “Current” (i.e., “This lab ...”). If we now process the following data file items:</p>
  
  <pre><code>d 35
  t
  i 37 Better now.
  </code></pre>
  
  <p>we will have the following list (note that traverse (t) should output each “fact” encountered for the entire list from the current pointer).</p>
  
  <pre><code>                         --- Current                                 ---- Tail
                           v                                          v
  ---&gt;   27       ---&gt;     15         ---&gt;     37          ---&gt;     9       ---
  |      Mary...           Today...            Better...            This...    |
  -------------------------------------------------------------------------------
  </code></pre>
  
  <p>If we process the data item</p>
  
  <pre><code>h
  </code></pre>
  
  <p>the list will be</p>
  
  <pre><code>  --- Current                                                --- Tail
           v                                                          v
  ---&gt;     27       ---&gt;     15        ---&gt;     37          ---&gt;     9       ---
  }        Mary...           Today...           Better...            This...    |
  -------------------------------------------------------------------------------
  </code></pre>
  
  <p>To delete 9 (i.e., d 9 appears in the data file) the card before must be found (“Current points to it”) and “Tail” adjusted.</p>
  
  <p>Write a C++ program to do the following:</p>
  
  <ul>
  <li>Read in the information and build a “hypercard” linked list</li>
  <li>Process all codes and data. Check the code first before attempting to read
  any additional data. Remember to check for special cases (i.e., list is empty, list has one element, etc.). </li>
  </ul>
  
  <p>Input:
  Each input data line in the file will consist of a character code and the appropriate data (no errors will be introduced) and will be in prog6.dat. Read and process each line separately to the end of the file. </p>
  
  <p>Output:
  Output will consist of facts (strings) from print (p) or traverse (t) commands and an active response for each command.</p>
  
  <p>Hints:
  Declare a list type, either as a class or a record (struct is a public class) - for example:</p>
  
  <pre><code>typedef struct list_tag{
      Node_type * current;
      Node_type * tail;
  } List_type;
  </code></pre>
  
  <p>Build the initial linked list by inserting each data item in turn at the end of the list. Maintain “Tail” and “Current” pointers. You must check to see if the “
  Tail” pointer is NULL before insertion, otherwise insertion is AFTER the “Current” position. Deletion will cause the “Current” pointer to be set to point at the card before the one being deleted (unless it is the last card in which case “Tail” and “Current” must become NULL). Forward (f) should move the “Current” pointer to the next card.</p>
  
  <p>Write a separate function for each ADT list operation (i.e., init/create, insert, delete, print, traverse, home), developed first as stubs, then as full functions. Use the standard technique of placing the main, functions, and headers in separate files</p>
  
  <p>Here is the file I'll be using for the input:</p>
  
  <pre><code>i 27 Mary had a little lamb
  i 15 Today is a good day
  i 35 Now is the time!
  i 9 This lab is easy and fun
  p
  d 35
  t
  i 37 Better Now.
  f
  p
  h
  p
  d 27
  d 15
  d 37
  d 9
  i 44 This should be it!
  t
  p
  </code></pre>
  
  <p>I have no idea where to go from here or how to do anything else. I've been reading tutorials all night, but it's just not clicking as to how to apply anything I'm reading it this program. I'm just trying to make it through this class with a C seeing as I've already changed my major for next semester since I suck at all of this. Can anyone help?</p>