打印链接列表仅打印头节点

时间:2019-07-09 21:16:00

标签: c++ linked-list

用52张牌随机填充一个链表后。当我尝试打印它时,它将打印头节点52次。我的“遍历链表逻辑”有问题还是插入逻辑有问题?

这些是Node帮助器方法(一个bagNode有一个CardOfBag和下一个)

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

这些是链接列表(称为bag)的方法: 它具有标题,尾部和当前的bagNode*指针。

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

2 个答案:

答案 0 :(得分:1)

bagNode :: setNext()应该指向提供的节点的下一个点。不理会另一个节点:

select min(callid), conf, grouping, min([start]), max([end]), count(*)
from (select c.*,
             sum(case when prev_end < [start] then 1 else 0 end) over (order by start) as grouping
      from (select c.*,
                   max([end]) over (partition by conf order by [start] rows between unbounded preceding and 1 preceding) as prev_end
            from calls c
           ) c
     ) c
group by conf, grouping;

您的bag :: setCurrent()不起作用。您应该使此“->当前”继续前进“下一个”,直到达到所需的位置为止,相反,您只需将其下一个指针更改为... 与以前相同的值即可。

改为执行以下操作:

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

现在应该会更好。

答案 1 :(得分:0)

在您的功能中

void bag::setCurrent(int desiredPosition)

您根本没有更改 this-> current 。基本上,您在该功能中什么也没做。