Hi,
I'm fighting a battle with a linked list and I am currently losing. I am using the toolkit, pointers, nodes and a class. My code is still buggy because after building it, it just runs for a while then stops. Please help. Here is what I have, an implementation file of the linked list, everything else was given.

//Provided by Arylee McSweaney
//CLASS implemented: Sequence3(See sequence3.h for documentation)
//INVARIANT for the sequence class:
//  1. The items in the sequence are stored in a linked list.
//  2. The head pointer of the list is stored in the member variable head_ptr.
//  3. The total number of items in the list is stored in the member variable many_nodes.
//  4. The current position of the cursor is stored in the member variable cursor_ptr
//  5. The position of the previous node is stored in the member variable precursor_ptr.

#include <assert> //provides assert
#include <cstdlib>  //provides NULL, size_t
#include "node1.h"  //Provides node and the linked list functions
#include "sequence3.h"
using namespace std;

namespace main_savitch_5
{
  sequence::sequence()
  //Library facilities used: cstdlib
  {
    head_ptr = NULL;
    tail_ptr = NULL;
    cursor_ptr = NULL;
    precursor_ptr = NULL;
    many_nodes = 0;
  }
  sequence::sequence(const sequence& source)
  //Library facilitie used: node1.h
  {
    node *tail_ptr; //Needed for argument of list_copy
    list_copy(source.head_ptr, head_ptr, tail_ptr);
    many_nodes = source.many_nodes;
  }
  sequence::~sequence()
  //Library facilities used: node1.h
  {
    list_clear(head_ptr);
    many_nodes = 0;
  }
  //MODIFICATION MEMBER FUNCTIONS
  void sequence::start()
  {//Postcondition: The first item on the sequence becomes the current item
   //(but if the sequence is empty, then there is no current item).
               precursor_ptr = NULL;
               cursor_ptr=head_ptr;
               many_nodes ++;
  }
  void sequence::advance()
  {//Precondition: is_item returns true.
   //Postcondition: If the current item was already the last item on the
   //sequence, then there is no longer any current item. Otherwise, the new
   //current item is the item immediately after the original current item.
    assert(is_item());

    cursor_ptr = cursor_ptr->link();
    ++many_nodes;

     if(precursor_ptr == NULL)
      precursor_ptr = head_ptr;
    else
        precursor_ptr = precursor_ptr->link();
    }
  void sequence::insert(const value_type& entry)
  {//Postcondition: A new copy of entry has been inserted in the sequence before
  //the current item. If there was no current item, then the new entry has
  //been inserted at the front of the sequence. In either case, the newly
  //inserted item is now the current item of the sequence.

                if (is_item( ))
                list_insert(precursor_ptr, entry);
                else
                list_head_insert(head_ptr, entry);
                cursor_ptr = precursor_ptr = head_ptr;
                cursor_ptr = cursor_ptr->link();
                many_nodes++;
  }
  void sequence::attach(const value_type& entry)
  {//Postcondition: A new copy of entry has been inserted in the sequence after
  //the current item. If there was no current item, then the new entry has
  //been attached to the end of the sequence. In either case, the newly
  //inserted item is now the current item of the sequence.
    if(is_item())
    list_insert(cursor_ptr->link(), entry);
    else
    list_head_insert(head_ptr, entry);
    cursor_ptr = head_ptr;
    cursor_ptr=cursor_ptr->link();
    ++many_nodes;

  }
  void sequence::remove_current()
  {//Precondition: is_item returns true.
  //Postcondition: The current item has been removed from the sequence, and the
  //item after this (if there is one) is now the new current item.

     node *target_ptr;
     value_type target;
    target = cursor_ptr->data();    //gives target_ptr a value to point to
    target_ptr = list_search(head_ptr, target);
    //case where item to be removed is not the head pointer or the tail pointer
     if(target_ptr != NULL && head_ptr != NULL){
      target_ptr->set_data(head_ptr->data());
      list_head_remove(head_ptr);}
      --many_nodes;
  //case where item to be removed is the head pointer
      if(target_ptr = head_ptr){
      list_head_remove(head_ptr);}
      --many_nodes;
  //case where item to be removed is the tail pointer
     if(target_ptr->link() == NULL);
      target_ptr->set_data(head_ptr->data());
      list_head_remove(head_ptr);
      cursor_ptr = head_ptr;
      precursor_ptr->set_link(NULL);
      tail_ptr = precursor_ptr;
      --many_nodes;
   }
  void sequence::operator =(const sequence& source)
  {//There is a linked list available for copy.
   //Postcondition: The new linked list will be identical to the first.
   node *tail_ptr;
   if(this == &source)
   return;
   list_clear(head_ptr);
   many_nodes = 0;
   list_copy(source.head_ptr, head_ptr, tail_ptr);
   many_nodes = source.many_nodes;
  }
  //CONSTANT MEMBER FUNCTIONS
  sequence::size_type sequence::size() const {return many_nodes;}

  sequence::value_type sequence::current() const
  {//Precondition: is_item( ) returns true.
  //Postcondition: The item returned is the current item on the sequence.
  /*Current() returns the data stored in the current node, not the node itself
  and not a pointer to this node. */

   return cursor_ptr->data();
  }
  bool sequence::is_item() const
  {//Postcondition: A true return value indicates that there is a valid
  //"current" item that may be retrieved by activating the current
  //member function (listed below). A false return value indicates that
  //there is no valid current item.
   return(size()>0);

  }
}

<< moderator edit: added [co[u][/u]de][/co[u][/u]de] tags >>

would appreciate a nudge in the right direction.

When it stops, what does it tell you? (a.exe has encountered a problem and needs to close?)

When messing with linked lists, I find my biggest problems are segmentation faults, which I sort of have to guess at since WinXP decided it'd be a good idea to hide the error messages in a generic one and give you only a core dump.

Check the code library for examples of linked lists and things that work if you need a bit of help with them.

posted by accident

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.