2015-03-19 97 views
0

我幾乎完成了LinkedList程序,但無法找出關於我的插入和刪除函數的這一個compliler錯誤。除了最後一個測試位以外,測試儀文件的其他部分似乎只是簡單的稱呼它。鏈接列表函數調用

我會後整個事情只是爲了安全起見:

頁眉:插入

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

我敬佩由於它們正常工作,請將其他測試呼叫轉出。該程序工作正常時,我註釋掉刪除和插入測試5,所以我已經縮小到這一點。編譯器說,沒有用於調用remove和insert的匹配函數,但是它幾乎匹配。

+0

如果要刪除列表中的最後一個節點,該怎麼辦?如果你走過,我希望你有一個例外。 – 2015-03-19 03:51:00

回答

0

insertremove方法採取Node<T> *參數,但這些行:

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

逝去的整數。您可能想要通過tmp而不是99,但您必須執行另一次搜索才能找到100的等效項。