链表函数调用
Linked List Function Calls
我几乎完成了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的调用没有匹配的函数,但它几乎匹配。
您的insert
和remove
方法采用Node<T> *
参数,但这些行:
A.insert(42,99);
A.remove(100);
正在传递整数。您可能希望传递tmp
而不是99
,但您必须进行另一次搜索才能找到100
的等效项。
相关文章:
- C++析构函数被意外调用双链表
- 使用析构函数释放链表
- 为数据缺少默认构造函数的链表创建节点
- 我是C++新手。我在此代码中遇到错误,无法使用 SwapNodes 函数C++创建链表。该函数不交换节点
- 如何使用函数在链表中插入元素
- 类中的链表,并且应该通过该类中的函数调用
- 链表函数调用
- 析构函数在链表中是必需的
- 在C++中调用链表函数
- C++ int& operator[](字符串键)函数使用链表
- 继承链是否创建构造函数调用链?
- Pop函数和链表的实现
- 每次在链表中调用insert_node()函数时,Head都被赋值为Null
- 函数返回链表中的总数据
- 如何在C++中调用递归链表遍历函数
- 键函数的链表插入被某些参数卡住了
- 如何使用析构函数清除链表内存,而不会出现valgrind错误?[更新:操作员过载帮助]
- 使用函数创建链表时出现分段错误
- 复制构造函数后链表中的访问冲突
- 使用递归函数删除链表