views:

149

answers:

5

Hello,

I'm having some trouble printing out the contents of a linked list. I'm using an example code that I found somewhere. I did edit it a bit, but I don't think that's why it's crashing.

class stringlist 
{
  struct node 
  {
    std::string data;
    node* next;
  };
  node* head;
  node* tail;
public:
  BOOLEAN append(std::string newdata)
  {
      if (head)
      {
          tail->next = new node;
          if (tail->next != NULL)
          {
              tail=tail->next;
              tail->data = newdata;
              return TRUE;
          }
          else
              return FALSE;
      }
      else
      {
          head = new node;
          if (head != NULL)
          {
              tail = head;
              head->data = newdata;
              return TRUE;
          }
          else
              return FALSE;
      }
  }
  BOOLEAN clear(std::string deldata)
  {
      node* temp1 = head;
      node* temp2 = NULL;
      BOOLEAN result = FALSE;
      while (temp1 != NULL)
      {
          if (temp1->data == deldata)
          {
              if (temp1 == head)
                  head=temp1->next;
              if (temp1==tail)
                  tail = temp2;
              if (temp2 != NULL)
                  temp2->next = temp1->next;
              delete temp1;
              if (temp2 == NULL)
                  temp1 = head;
              else
                  temp1 = temp2->next;
              result = TRUE;
          }
          else // temp1->data != deldata
          {
              temp2 = temp1;
              temp1 = temp1->next;
          }
      }
      return result;
  }
  BOOLEAN exists(std::string finddata)
  {
      node* temp = head;
      BOOLEAN found = FALSE;
      while (temp != NULL && !found)
      {
          if (temp->data == finddata)
              found=true;
          else
              temp = temp->next;
      }
      return found;
  }
  void print()
  {
      node* tmp = head;
      while (tmp)
      {
          printf("%s", tmp->data.c_str());
          tmp = tmp->next;
      }
  }
  stringlist()
  {
      head=NULL;
      tail=NULL;      
  }
};

My main() function is really simple:

int main()
{
stringlist mylist;
  if (mylist.append("something"))
      count++;
  if (mylist.append("else"))
      count++;
  if (mylist.append("yet"))
      count++;
  cout<<"Added "<<count<<" items\n";
  mylist.print();
return 0;
}

For some reason in Print() tmp is never NULL

+3  A: 

in node, add a constructor to initialize next to null

rmn
A: 

You aren't initializing head->tail appropriately in append when head==NULL initially.

MSN
he is, after checking that head = new isn't null
rmn
Um.. he never initializes (any node)->tail anywhere.
MSN
A: 

Correct. That's because tail is only NULL in your code when the linked list is initially created. After you add a node, you set tail = head, and from that point in time, every time you add an element, you set tail->next = new node, and then tail = tail->next... so that tail->next always = tail.

Nick
A: 

As @rmn pointed out, you're not initializing the value of node->next.

BOOLEAN append(std::string newdata) 
{ 
  if (head) 
  { 
    tail->next = new node; 
    if (tail->next != NULL) 
    { 
      tail=tail->next; 
      tail->data = newdata; 
      tail->next = NULL; // <- this is the part that is missing
      return TRUE; 
    }
    else 
      return FALSE; 
  }
  else 
  { 
    head = new node; 
    if (head != NULL) 
    { 
      tail = head; 
      head->data = newdata;
      head->next = NULL; // <- it's also missing here.
      return TRUE; 
    } 
    else 
      return FALSE; 
  } 
} 

You could solve this by having a default constructor for node:

struct node   
{  
  std::string data;  
  node* next;

  node() : next(NULL) { }
};

With the default constructor you won't need to add tail->next = NULL;.

Bill
A: 

Thanks a lot guys, works like a charm now.

zukeh
For future reference, it's better to comment on your original post, or edit your original post. That way everyone can see your update easily. Glad to hear it works!
Bill
Sorry. First time here
zukeh