float get(int x) {
if(x == 0) {return head->x;}
else {
Node *current = head;
for(int a=0; a<x; a++) {
current = current->next;
}
return current->x;
}
}
这是我的代码,但是当我使用测试用例运行它时,它显示“Lab3.exe 已停止工作”。我对 C++ 还是很陌生,我们的老师丝毫没有帮助,有人可以就我的实现给我建议吗?
编辑:这是测试用例......
#include <iostream>
#include <string>
using namespace std;
// including _my copy_ of SLList
#include "./SLList.h"
using namespace ods;
template <class FloatList>
bool listTest(){
FloatList L;
L.add(0, 3.14);
L.remove(0);
L.add(0, 1.62);
L.add(1, 2.23);
L.set(1, 2.72);
/*float x = L.get(1);*/
return ( 2.72f == L.get(1) and 2 == L.size() );
}
int main() {
if ( listTest<SLList<float> >() )
cout << "SLList passes basic list interface test" << endl;
else
cout << "SLList FAILS basic list interface test" << endl;
}
以下是 SLL 的设置方式:
template<class T>
class SLList {
T null;
protected:
class Node {
public:
T x;
Node *next;
Node(T x0) {
x = 0;
next = NULL;
}
};
Node *head;
Node *tail;
int n;
public:
SLList() {
null = (T)NULL;
n = 0;
head = tail = NULL;
}
virtual ~SLList() {
Node *u = head;
while (u != NULL) {
Node *w = u;
u = u->next;
delete w;
}
}