zukeh
zukeh

Reputation: 13

Crash, while printing contents of linked-list

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

Upvotes: 1

Views: 637

Answers (4)

Bill
Bill

Reputation: 14685

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;.

Upvotes: 0

Nick
Nick

Reputation: 5955

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.

Upvotes: 0

MSN
MSN

Reputation: 54584

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

Upvotes: 0

rmn
rmn

Reputation: 2426

in node, add a constructor to initialize next to null

Upvotes: 3

Related Questions