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;
}
}