嗨,在我的c++分配中,我必须实现一组函数,其中一个函数要求我从前向列表中复制值,并将它们存储在链接列表中。我们的大学使用一个名为Edstem的网站运行并标记代码。它在一个名为tests.hpp的文件中通过了一系列测试。
我在另一个IDE中为这个特定的函数编写了我的代码,它工作得很好,我让它打印出链接列表中的所有节点。
下面是我在repl.it中所做的代码,以了解如何将其应用于我的作业
这就是我在edstem网站上运行测试后得到的结果
这是来自tests.hpp的

#include <iostream>
#include <map>
#include<forward_list>
using namespace std;
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next = nullptr;
};
int main() {
forward_list<int> flist1;
flist1 = {1,2,3,4,5,6,5,3,7,3,5,2,6,3,6,34,3,65,6};
Node * head = nullptr;
Node * temp = head;
for (auto it = flist1.begin() ; it != flist1.end(); it++){
if (head == nullptr){
head = new Node();
head->data = *it;
temp = head;
}else{
if(temp != nullptr){
temp->next = new Node();
temp->next->data = *it;
temp = temp->next;
}
}
//temp->next = new Node();
//temp->next->data = *it;
// temp = temp->next;
}
temp = head;
while (temp != nullptr){
cout << temp->data<< endl;
temp = temp->next;
}
}下面是我必须实现的函数
// Constructor from initializer list
// ***For you to implement
// This implements the functionality you see with, for example,
// std::forward_list<int> my_list = {1,2,3}
// which creates a new linked list where the first node holds 1, second 2, and
// third 3.
// The {1,2,3} here is of type std::initializer_list<int> and you
// see this is the argument to this constructor (with data of type T
// rather than just int).
// You can access the elements of a std::initializer_list via an iterator
// for example you can cycle through all the elements with
// for(auto it = input.begin(); it!= input.end(); ++it){Do something with *it}
template <typename T>
Forward_list<T>::Forward_list(std::initializer_list<T> input)
{
head_ = nullptr;
Node * temp = head_;
for (auto it = input.begin(); it != input.end(); it++){
if (head_ == nullptr){
head_ = new Node(*it);
temp = head_;
}else{
if(temp != nullptr){
temp->next = new Node(*it);
temp = temp->next;
}
}
temp = head_;
}
}
#endif下面是链接列表的类,相关部分
template <typename T>
class Forward_list
{
public:
class Node
{
public:
// A node will hold data of type T
T data{};
// next will point to the next node in the list
// we initialise next to nullptr
Node* next = nullptr;
// Because we have already intialised the variables
// the default constructor doesn't need to do anything
Node(){}
// To make life easier we also provide a constructor
// that takes the T data to be copied into the data member variable
// There is an optional second argument which is
// used to update the next pointer. This defaults to nullptr
// if the constructor is called with just one argument.
Node(T input_data, Node* next_node= nullptr)
{
data = input_data;
next = next_node;
}
// Destructor
~Node(){}
};
private:
// private member variables for Forward_list
// the trailing underscore is a stylistic choice to
// distinguish these as private member variables
unsigned size_ = 0;
Node* head_ = nullptr;以及所使用的测试函数
void test_initializer_list(void)
{
std::initializer_list<int> inputs = {5,23,1,105,-2,7,88,0};
Forward_list<int> my_list {inputs};
std::forward_list<int> real_list {inputs};
for(unsigned i=8; i>0; --i)
{
assert(my_list.front() == real_list.front());
assert(my_list.size() == i);
real_list.pop_front();
my_list.pop_front();
}
}发布于 2022-04-09 07:47:00
只要把线移开
temp = head_;在循环体的末端。否则,您可以在每次迭代中将要处理的节点重置为head节点,而不管您是否位于第一个元素。
Imho是一种不需要考虑两种状态的循环。
此外,由于某些原因,构造函数中从未设置size_。
下面是您的实现版本,上面提到的bug已经修复,还有一个版本是从头开始实现的。
使用fix实现您的版本
template <typename T>
Forward_list<T>::Forward_list(std::initializer_list<T> input)
: size_(input.size())
{
head_ = nullptr;
Node* temp = head_;
for (auto it = input.begin(); it != input.end(); it++) {
if (head_ == nullptr) {
head_ = new Node(*it);
temp = head_;
}
else {
if (temp != nullptr) {
temp->next = new Node(*it);
temp = temp->next;
}
}
}
}我自己的版本
template <typename T>
Forward_list<T>::Forward_list(std::initializer_list<T> input)
: size_(input.size()),
head_(nullptr)
{
auto it = input.begin();
auto const end = input.end();
if (it != end)
{
head_ = new Node(*it);
++it;
for (auto temp = head_; it != end; ++it)
{
auto next = new Node(*it);
temp->next = next;
temp = next;
}
}
}https://stackoverflow.com/questions/71805816
复制相似问题