我当前正在学习链接列表如何在C++中工作,并且我已经编写了这段代码,它给了我编译错误,并且我没有得到我在早期版本的代码中获得的resalts。这是我以前丢失的代码的早期版本。链接列表和节点
所以我需要帮助:
应该如何add函数(在列表中最后一个把一个元素)是什么样子?
我需要向解构器添加什么?
而remove_if(T & t)表示应删除所有具有值t的元素,remove_if(谓词& pred)表示应删除所有元素pred返回true?
我需要在T>之间添加什么?
我的代码,我已经编辑:
#include <iostream>
using namespace std;
template <class T>
class List;
template <class T>
class Node {
public:
Node (T *t) : data(t), next(0) {}
~Node();
private:
T *data;
Node* next;
friend class List<T>;
};
template <class T>
class Predicate {
public:
Predicate() {}
virtual bool operator()(const T& v) = 0;
};
template <class T>
class List {
public:
List() : first(new Node<T>(T())) {} //"dummy"-node
void add(T *t);
void remove_if(T t);
void remove_if(Predicate<T> &pred);
void print();
private:
Node<T> *first;
};
主营:
int main()
{
List<int> intlista;
intlista.add(new int(1));
intlista.add(new int(2));
intlista.add(new int(3));
intlista.add(new int(2));
intlista.add(new int(4));
intlista.add(new int(5));
intlista.add(new int(6));
intlista.print();
intlista.remove_if(2);
intlista.print();
Between<int> one_to_four(1,4);
intlista.remove_if(one_to_four);
intlista.print();
}
写道:
{ 1 2 3 2 4 5 6 }
{ 1 3 4 5 6 }
{ 5 6 }
错误,那你什么 – juanchopanza
您需要出示'名单:: add'的定义页眉??? –
@juanchopanza “之间未在此区域中声明” “期望初级表达式在”int“” “之前预期;在”i nt“” “one_to_four没有在这个区域声明” –