C++ 将元素添加到链表并写出所有元素时,第一个元素doesen';我写不出来

C++ 将元素添加到链表并写出所有元素时,第一个元素doesen';我写不出来,c++,linked-list,C++,Linked List,在写出元素时,我的链接列表有问题: 运行main.cpp时,我得到以下输出: myList.endInsert(5); cout << myList << endl; output: nothing myList.headInsert(3); cout << myList << endl; output : 3 myList.headInsert(2); cout << myLis

在写出元素时,我的链接列表有问题: 运行main.cpp时,我得到以下输出:

myList.endInsert(5);
    cout << myList << endl;     output: nothing
    myList.headInsert(3);
    cout << myList << endl;     output : 3
    myList.headInsert(2);
    cout << myList << endl;     output : 3 2
void IntList::headInsert(int the_number)
{
    if (head == NULL) //if list is empty
    {
        head = new IntNode; //create new dynamic variable
        head -> data = the_number; //add value to new variable
        head -> link = NULL; //    }
    else
    {
        NodePtr temp = new IntNode;
        temp -> data = the_number;
        temp ->link = head; //temp pointer becomes head
        head = temp; //head becomes temp
    }

**And here is the friend function for the operator:**



 ostream& operator <<(ostream& outs, const IntList& lis)
    {

      for(NodePtr temp = lis.head; temp->getLink() != NULL; temp = temp->getLink())
      {

        if( temp->getLink() != NULL)
          {
              outs << " ";
              outs << temp->getData();
          }

      }
      return outs;
    }
myList.endInsert(5);
cout-link=头部//临时指针变为头
压头=温度//头变成了温度
}
**下面是操作员的朋友函数:**
ostream&运算符getLink()
{
如果(temp->getLink()!=NULL)
{

outs您不打印最后一项

  for(NodePtr temp = lis.head; temp != NULL; temp = temp->getLink())
  {
     outs << " ";
     outs << temp->getData();
  }
for(NodePtr temp=lis.head;temp!=NULL;temp=temp->getLink())
{

当插入设置的第一个元素时,输出

head->link == null;
以后打印支票时

if( temp->getLink() != NULL)
{
  outs << " ";
  outs << temp->getData();
}
它应该可以工作

ostream&operator getLink()!=NULL)
ostream& operator <<(ostream& outs, const IntList& lis)
{

  for(NodePtr temp = lis.head; temp != NULL; temp = temp->getLink())
  {

    //if( temp->getLink() != NULL)
      {
          outs << " ";
          outs << temp->getData();
      }

  }
  return outs;
}
{
有没有理由不使用std::list?每次你添加一个新元素时,它都会变成“头”的意思?效果很好。谢谢!对你很好!但是如果没有理由(比如这是家庭作业),还是使用std::list。否则可能会有所帮助。是的,这是家庭作业,谢谢你的帮助。我会查看链接:)
ostream& operator <<(ostream& outs, const IntList& lis)
{

  for(NodePtr temp = lis.head; temp != NULL; temp = temp->getLink())
  {

    //if( temp->getLink() != NULL)
      {
          outs << " ";
          outs << temp->getData();
      }

  }
  return outs;
}