Class 链表参数化构造函数

Class 链表参数化构造函数,class,c++11,linked-list,queue,Class,C++11,Linked List,Queue,我试图为链表做一个参数化构造函数。我的程序将要通过使用一个类似列表来实现一个队列,所以我想做一个类似队列(int值,int大小)的参数化构造函数,但它不运行或做列表 这是我解决这个问题的代码 Queue(int value,int _size) { for(int i = 0; i < _size; ++i) { Node* temp = new Node; temp->data = value;

我试图为链表做一个参数化构造函数。我的程序将要通过使用一个类似列表来实现一个队列,所以我想做一个类似队列(int值,int大小)的参数化构造函数,但它不运行或做列表 这是我解决这个问题的代码

Queue(int value,int _size)
    {
        for(int i = 0; i < _size; ++i)
        {
            Node* temp = new Node;
            temp->data = value;
            temp->next = nullptr;
            if(head == nullptr)
            {
                head = tail = temp;
            }
            else
            {
                tail->next = temp;
                tail = temp;

            }
        }
    }
队列(int值,int大小)
{
用于(int i=0;i<_size;++i)
{
Node*temp=新节点;
温度->数据=值;
temp->next=nullptr;
if(head==nullptr)
{
头部=尾部=温度;
}
其他的
{
尾部->下一步=温度;
尾=温度;
}
}
}
我希望结果是以值乘以大小填充lest,就像我运行这个函数队列x(20,3)时,链表应该是
20

在创建节点后遵循此代码。我希望这能奏效。并且一定要使用i++而不是++i,因为后者会使循环的大小增加1倍

 if(head == NULL)
  head = temp;
else{
  Node *x;
  x= head;
  while(x->next != NULL)
     x = x->next;
  x->next = temp;

}

创建节点后,请遵循此代码。我希望这能奏效。并且一定要使用i++而不是++i,因为后者会使循环的大小增加1倍

 if(head == NULL)
  head = temp;
else{
  Node *x;
  x= head;
  while(x->next != NULL)
     x = x->next;
  x->next = temp;

}由于这是一个构造函数,
头和
尾未正确初始化以使用它们。我建议在循环之前添加
head=tail=nullptr
,看看会发生什么。

因为这是一个构造函数,所以没有正确初始化
head
tail
。我建议在循环之前添加
head=tail=nullptr
,看看会发生什么。

如果添加标记,您将获得更多答案如果添加标记,您将获得更多答案