linkedlist.h
来自「一个进行整数计算的C语言词法分析器」· C头文件 代码 · 共 306 行
H
306 行
#include "Node.h"
#include "stdlib.h"
template<class T>
class LinkedList
{
Node<T>*front,*rear; //一个头指针和一个尾指针
Node<T>*prevPtr,*currPtr; //前指针和当前指针
int size,position; //链表长度 和 当前位置
Node<T>*GetNode(const T&item,Node<T>*ptrNext=NULL); //得到一个节点
void FreeNode(Node<T>*p); //释放节点
void CopyList(const LinkedList<T>&L); //复制链表
public:
LinkedList(void); //无参构造函数
LinkedList(const LinkedList <T>&L); //有参构造函数
~LinkedList(void); //析构函数
LinkedList<T>& operator=(const LinkedList<T>&L); //重载赋值符号
int ListSize(void)const; //取链表长度
int ListEmpty(void)const; //判断链表是不是空
void Reset(int pos=0); //把当前指针重置在链表的头
void Next(void); //把当前指针移到下一个
int EndOfList(void)const; //判断是否是链表的尾
int CurrentPosition(void)const; //求当前指针的位置
void InsertFront(const T&item); //插入头
void InsertRear(const T&item); //插入尾
void InsertAt(const T&item); //插在当前位置
void InsertAfter(const T&item); //插在当前节点后面
T DeleteFront(void); //去掉头指针
void DeleteAt(void); //去掉当前指针
T& Data(void); //取this指针中的数据
void ClearList(void); //清空指针
};
template <class T>
LinkedList<T>::LinkedList(void):
front(NULL), rear(NULL),prevPtr(NULL),
currPtr(NULL),size(0),position(-1){}
template <class T>
Node<T> * LinkedList<T>::GetNode(const T& item,Node<T>* ptrNext)
{
Node<T> *p;
p = new Node<T>(item, ptrNext);
if (p == NULL)
{
cout << "Memory allocation failure!\n";
exit(1);
}
return p;
}
template <class T>
LinkedList<T>::LinkedList(const LinkedList<T>& L)
{
front = rear = NULL;
prevPtr = currPtr = NULL;
size = 0;
position = -1;
CopyList(L);
}
template <class T>
void LinkedList<T>::FreeNode(Node<T> *p)
{
delete p;
}
template <class T>
void LinkedList<T>::CopyList(const LinkedList<T>& L)
{
// use p to chain through L
Node<T> *p = L.front; int pos;
// insert each element in L at the rear of current object
while (p != NULL)
{
InsertRear(p->data);
p = p->NextNode( );
}
if (position == -1) return; // if list is empty return
// reset prevPtr and currPtr in the new list
prevPtr = NULL; currPtr = front;
for (pos = 0; pos != position; pos++)
{
prevPtr = currPtr;
currPtr = currPtr->NextNode( );
}
}
template <class T>
void LinkedList<T>::ClearList(void)
{
Node<T> *currPosition, *nextPosition;
currPosition = front;
while(currPosition != NULL)
{
// get address of next node and delete current node
nextPosition = currPosition->NextNode( );
FreeNode(currPosition);
currPosition = nextPosition; // Move to next node
}
front = rear = NULL;
prevPtr = currPtr = NULL;
size = 0;
position = -1;
}
template <class T>
LinkedList<T>::~LinkedList(void)
{
ClearList( );
}
template <class T>
LinkedList<T>& LinkedList<T>::operator=
(const LinkedList<T>& L)
{
if (this == &L) // Can't assign list to itself
return *this;
ClearList( );
CopyList(L);
return *this;
}
template <class T>
int LinkedList<T>::ListSize(void) const
{
return size;
}
template <class T>
int LinkedList<T>::ListEmpty(void) const
{
return size == 0;
}
template <class T>
void LinkedList<T>::Next(void)
{ // if traversal has reached the end of the list or
// the list is empty, just return
if (currPtr != NULL)
{ // advance the two pointers one node forward
prevPtr = currPtr;
currPtr = currPtr->NextNode( );
position++;
}
}
template <class T>
int LinkedList<T>::EndOfList(void) const
{
return currPtr == NULL;
}
template <class T>
int LinkedList<T>::CurrentPosition(void) const
{
return position;
}
template <class T>
void LinkedList<T>::Reset(int pos)
{
int startPos;
if (front == NULL) return;
if (pos < 0 || pos > size-1)
{
cerr << "Reset: Invalid list position: " <<pos << endl;
return;
}
if(pos == 0)
{
prevPtr = NULL;
currPtr = front;
position = 0;
}
else
{
currPtr = front->NextNode( );
prevPtr = front;
startPos = 1;
for(position=startPos; position != pos; position++)
{
prevPtr = currPtr;
currPtr = currPtr->NextNode( );
}
}
}
template <class T>
T& LinkedList<T>::Data(void)
{
// error if list is empty or traversal completed
if (size == 0 || currPtr == NULL)
{
cerr << "Data: invalid reference!" << endl;
exit(1);
}
return currPtr->data;
}
template <class T>
void LinkedList<T>::InsertAt(const T& item)
{
Node<T> *newNode;
if (prevPtr == NULL)
{
newNode = GetNode(item, front);
front = newNode;
}
else
{
newNode = GetNode(item);
prevPtr->InsertAfter(newNode);
}
if (prevPtr == rear)
{
rear = newNode;
position = size;
}
currPtr = newNode;
size++;
}
template <class T>
void LinkedList<T>::InsertFront(const T& item)
{
// call Reset if the list is not empty
if (front != NULL)
Reset( );
InsertAt(item); // inserts at front
}
template <class T>
void LinkedList<T>::InsertAfter(const T& item)
{
Node<T> *p;
p = GetNode(item);
if (front == NULL) // inserting into an empty list
{
front = currPtr = rear = p;
position = 0;
}
else
{
if (currPtr == NULL) currPtr = prevPtr;
currPtr->InsertAfter(p);
if (currPtr == rear)
{
rear = p;
position = size;
}
else position++;
prevPtr = currPtr;
currPtr = p;
}
size++; // increment list size
}
template <class T>
void LinkedList<T>::InsertRear(const T& item)
{
Node<T> *newNode;
prevPtr = rear;
newNode = GetNode(item); // create the new node
if (rear == NULL) // if list empty, insert at front
front=rear = newNode;
else
{
rear->InsertAfter(newNode);
rear = newNode;
}
currPtr = rear;
position = size;
size++;
}
template <class T>
void LinkedList<T>::DeleteAt(void)
{
Node<T> *p;
if (currPtr == NULL)
{
cerr << "Invalid deletion!" << endl;
exit(1);
}
if (prevPtr == NULL)
{
p = front;
front = front->NextNode( );
}
else p = prevPtr->DeleteAfter( );
if (p == rear)
{
rear = prevPtr;
position--;
}
currPtr = p->NextNode( );
FreeNode(p);
size--;
}
template <class T>
T LinkedList<T>::DeleteFront(void)
{
T item;
Reset( );
if (front == NULL)
{
cerr << "Invalid deletion!" << endl;
exit(1);
}
item = currPtr->data;
DeleteAt( );
return item;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?