链表函数调用

Linked List Function Calls

本文关键字:函数调用 链表      更新时间:2023-10-16

我几乎完成了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;
}

我删掉了其他测试调用,因为它们工作正常。当我在TEST5中注释掉删除和插入时,这个程序运行得很好,所以我把它缩小到了这个范围。编译器表示,对remove和insert的调用没有匹配的函数,但它几乎匹配。

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

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

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