rustytoaster21
rustytoaster21

Reputation: 85

Printing Linked List prints only the head node

After randomly filling a linked list with a deck of 52 cards. When I try to print it, it will print the head node 52 times. Is there something wrong with my 'traverse through linked list logic' or something wrong with my insertion logic?

These are the Node helper methods (a bagNode has a CardOfBag and a next)

card *bagNode::getCard() // retreives card in the bagnode as pointer
{
  return this->cardOfBag;
}
bagNode *bagNode::getNext() // retreives the next bagnode as pointer
{
  return this->next;
}
void bagNode::setCard(card *card) // sets card of bagnode
{
  this->cardOfBag = card;
}
void bagNode::setNext(bagNode *setNode) // sets next bagnode
{
  setNode->next = this->next;
  this->next = setNode;
}

These are the methods for the linked list (called bag): it has a header, tail, and current bagNode* pointers.

void bag::add(bagNode *node) // adds node to random position
{
  int i, place, size;
  place = randomPosition();
  size = getCurrentSize();
  if (size == 0) // for initial insertion into empty linked list
  {
    setHead(node);
    setTail(node);
    alterSize(1);
  } else {
    if ((size - 1) == place) // if the insertion is at the last node
    {
      this->tail->setNext(node);
      setTail(node);
      alterSize(1);
    } else {
      if (place == 0) // if insertion is at head node
      {
        node->setNext(this->head);
        setHead(node);
        alterSize(1);
      } else {
        setCurrent(place); // for any insertion in between first and last nodes
        node->setNext(current->getNext());
        current->setNext(node);
        alterSize(1);
      }
    }
  }
}

int bag::getCurrentSize() // returns size of bag (linked list)
{
  return this->size;
}
void bag::alterSize(int num) // changes the size int of bag by num
{
  this->size = this->size + num;
}
int bag::randomPosition() // generates random number from 0 to size exclusive
{
  int size = getCurrentSize();
  if (size != 0)
    return (rand() % size);
}
void bag::setCurrent(int desiredPosition) // this traverses the current pointer
                                          // by desiredPosition steps from the head node
{
  int i;
  this->current = this->head;
  for (i = 0; i < desiredPosition; i++) {
    this->current->setNext(this->current->getNext());
  }
}
bagNode *bag::getCurrentNode() // returns node of current pointer
{
  return this->current;
}

Upvotes: 0

Views: 126

Answers (2)

Brad Pitt
Brad Pitt

Reputation: 416

in your function

void bag::setCurrent(int desiredPosition)

You are not changing this->current at all. Basically you did nothing in that function.

Upvotes: 0

DanyAlejandro
DanyAlejandro

Reputation: 1468

bagNode::setNext() is supposed to make next point to the node provided; leave the other node alone:

void bagNode::setNext(bagNode* setNode) //sets next bagnode                                                                              
{                                                                                                                                        
  this->next = setNode;
}

Your bag::setCurrent() doesn't work. You're supposed to make this->current keep going "next" until you hit the desiredPosition, instead you're only changing its next pointer to... the same value it previously had.

Do something like this instead:

void bag::setCurrent(int desiredPosition)                                                                                             
{                                                                                                                                        
  int i;                                                                                                                                 
  this->current = this->head;                                                                                                            
  for(i = 0; i < desiredPosition; i++)                                                                                                   
    {                                                                                                                                    
      this->current = this->current->getNext();
    }                                                                                                                                    
}

That should work better now.

Upvotes: 1

Related Questions