链接列表函数调用

时间:2015-03-19 03:47:34

标签: c++ compiler-errors linked-list

我几乎完成了一个LinkedList程序,但无法弄清楚这个关于我的插入和删除功能的编译器错误。除最后一个测试位外,测试器文件的其他部分似乎都称它为正常。

我会发布整件事只是为了安全:

部首:

#ifndef LINKEDLIST_H
#define LINKEDLIST_H

#include <iostream>

template <class T>
class Node
{
 public:
 T m_data;                  // Data to be stored
 Node<T>* m_next;     // Pointer to the next element in the list


 // Purpose: Default constructor
 // Postconditions: next pointer set to NULL
 // ---INLINE---
 Node() : m_next(NULL) {}

 // Purpose: Auxiliaty constructor, construct from parameters
 // Postconditions: data and next pointer set to parameters
 // ---INLINE---
 Node(const T& x, Node<T>* p)
         : m_data(x), m_next(p) {}

};

template <class T>
class LinkedList
{

 public:
 Node<T>* head;     // Pointer to the head of the list

// Purpose: Default constructor
// Postconditions: head pointer set to NULL
// ---INLINE---
 LinkedList() : head(NULL) {}


// Purpose: puts the data value x at the position pointed by pos
// Parameters: x is data value to inserted
//     pos pointer to the position to insert x at.
// Preconditions: pos is a pointer to a node in this list.
// Postconditions: x is inserted at the position pointed by pos
void insert(const T& x, Node<T>* pos);

// Purpose: removed the element in the position pointed by pos
// Parameters: pos pointer to the position to remove.
// Preconditions: pos is a pointer to a node in this list.
// Postconditions: position pointed by pos is removed from the list
void remove(Node<T>* pos);

插入和删除

的实现
template <class T>
void LinkedList<T>::remove(Node<T>* pos)
{
 Node<T>* tmp;
 tmp = pos->m_next;
 pos->m_data = tmp->m_data;
 pos->m_next = tmp->m_next;
 delete tmp;
}

template <class T>
void LinkedList<T>::insert(const T& x, Node<T>* pos)
{
 Node<T>* tmp;
 tmp = new Node<T>;
 tmp->m_data = pos->m_data;
 tmp->m_next = pos->m_next;
 pos->m_data = x;
 pos->m_next = tmp;
}

主程序

void test05() {

LinkedList<int> A;
Node<int>* tmp;

cout << endl << endl;
cout << " ***************** " << endl;
cout << " *  TEST SET #5  * " << endl;
cout << " ***************** " << endl;


//TEST : Panics on an empty list
cout << endl << "TEST : Panics on an empty list" << endl;
cout << A << endl;
cout << "Size of A = " << A.size() << endl;
tmp = A.getFirstPtr();
cout << "First = " << tmp << endl;
tmp = A.getLastPtr();
cout << "Last = " << tmp << endl;

//TEST : Inserting 10 elements to a
cout << endl << "TEST : Inserting 10 elements into A" << endl;
for (int k=0; k<10; k++){
  A.insert_front(k*11);
}
cout << A << endl;
cout << "Size of A = " << A.size() << endl;


//TEST : Panics on Invalid Index
cout << endl << "TEST : Panic on Invalid Index" << endl;
tmp = A.getAtPtr(99);
cout << tmp << endl;
A.insert(42,99);
A.remove(100);


 //TEST : Clearing A
 cout << endl << "TEST : Clearing A" << endl;
 A.clear();
 cout << A << endl;
 cout << "Size of A = " << A.size() << endl << endl;

 cout << "Test 05 - Done!" << endl;
}

int main () {

cout << "Hello World!!, This is the LinkedList LARGE Tester" << endl;

test01();
test02();
test03();
test04();
test05();


cout << "LARGE Done!" << endl;
return 0;
}

我编辑了其他测试调用因为它们正常工作。当我在TEST 5中注释掉删除和插入时,程序工作正常,所以我把它缩小到那个。编译器说没有用于调用remove和insert的匹配函数,但它是近似匹配。

1 个答案:

答案 0 :(得分:0)

您的insertremove方法采用Node<T> *参数,但这些行:

A.insert(42,99);
A.remove(100);

传递整数。您可能希望传递tmp而不是99,但是您必须进行另一次搜索才能找到100的等效内容。