/*........ NodeClass.h .....*/
#include<iostream>
using namespace std;
#ifndef NODE_CLASS_H
#define NODE_CLASS_H
//類宣告部分
template <class T>
class Node
{
private:
Node<T> *next; //指向后繼結點的指標
public:
T data; //資料域
Node(const T &item,Node<T> *ptr = NULL); //建構式
void insertAfter(Node<T> *p); //在本結點之后插入一個同類結點p
Node<T> * deleteAfter(); // 洗掉本類結點的后繼結點,并回傳其地址
Node<T> * nextNode()const; //獲取后繼結點的地址
};
//類實作部分
//建構式,初始化資料和指標成員
template <class T>
Node<T>::Node(const T& item,Node<T> *ptr):data(item),next(ptr)
{
}
template <class T>
Node<T>* Node<T>::nextNode()const
{
return next; //回傳私有的指標成員
}
#endif //NODE_CLASS_H
/*......LinkListClass.h ....*/
#ifndef LINK_LIST_CLASS_H
#define LINK_LIST_CLASS_H
#include <iostream>
#include <stdlib.h>
#include "NodeClass.h"
#ifndef NULL
const int NULL = 0;
#endif //NULL
template <class T>
class LinkedList
{
private:
//資料成員
Node<T> *front,*rear; //表頭和表尾指標
Node<T> *currPtr,*prevPtr; //記錄表當前遍歷位置的指標,由插入和洗掉操作更新
int size; //表中的元素個數
int position; //當前元素在表中的位置序號,由函式reset使用
public:
//清空鏈表:釋放所有結點的記憶體空間,被解構式,operator=呼叫
void clearList();
};
#endif //LINK_LIST_CLASS_H
/*下面是clearList()函式的實作 */
#include <iostream>
#include <stdlib.h>
#include "LinkListClass.h"
using namespace std;
//清空鏈表,釋放鏈表所有結點的記憶體空間,被解構式,operator=呼叫
template <class T>
void LinkedList<T>::clearList()
{
Node<T>* nextPtr;
currPtr = front;
//邊遍歷邊洗掉結點
while(currPtr != NULL)
{
//記錄下一個結點的地址,并且洗掉當前結點
nextPtr = currPtr->nextNode();
delete currPtr;
currPtr = nextPtr; //使指標指向下一個結點
}
front = currPtr = rear = NULL;
}
上述實作程序在vs2010中總是出現 ERROR:應輸入宣告,ERROR:此宣告沒有存盤類或型別說明符?
請各位大神幫忙解決下啊?偶將感激不盡
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/145998.html
標籤:基礎類
下一篇:資料結構
