线性链表
注意: firstnode指的就是第一个节点,而不是一个指向第一个节点的指针。
代码:
#include<iostream>
using namespace std;
template<typename T>
struct chainNode
{
T element;
chainNode* next;
chainNode() {};
chainNode(const T& element) { this->element = element; };
chainNode(const T& element, chainNode<T>* next) { this->element = element; this->next = next; };
};
template<typename T>
class linearList
{
public:
virtual ~linearList() {}; //析构函数
virtual bool empty() const = 0; //返回true,当且仅当线性表为空
virtual int size() const = 0; //返回线性表的元素个数
virtual T& get(int theIndex) const = 0; //返回索引为theIndex的元素
virtual int indexOf(const T& theElement) const = 0; //返回元素theElement第一次出现时的索引
virtual void erase(int theIndex) = 0; //删除索引为theIndex的元素
virtual void insert(int theIndex, const T& theElement) = 0; //把theElement插入线性表中索引为theIndex的位置
virtual void output() const = 0; //把线性表插入的输出流out
};
template<typename T>
class Chain : public linearList<T>
{
public:
Chain(int initialCapacity = 10);
Chain(const Chain<T>&);
~Chain();
bool empty() const { return listSize == 0; }
int size() const { return listSize; }
T& get(int theIndex) const;
int indexOf(const T& theElement) const;
void erase(int theIndex);
void insert(int theIndex, const T& theElement);
void output() const;
private:
int listSize;
chainNode<T>* firstnode;
};
template<typename T> Chain<T>::Chain(int initialCapacity)
{
if (initialCapacity < 1)
{
cout << "初始化容量小于1,不正确!" << endl;
exit(-1);
}
firstnode = NULL;
listSize = 0;
}
template<typename T> Chain<T>::Chain(const Chain<T>& theList)
{
this.listSize = theList.listSize;
if (listSize == 0)
{
firstnode = NULL;
return;
}
chainNode<T>* pt = theList->firstnode, *curr=NULL;
fisrtnode = new chainNode<T>(pt->element); //复制源节点
curr = firstnode;
while (pt != NULL)
{
curr->next = new chainNode<T>(pt->element);
pt = pt->next;
curr = curr->next;
}
curr->next = NULL;
}
template<typename T> Chain<T>::~Chain()
{
chainNode<T>* delnode = firstnode;
while (firstnode!= NULL)
{
firstnode = firstnode->next;
delete delnode;
delnode = firstnode;
}
listSize = 0;
}
template<typename T> T& Chain<T>::get(int theIndex) const
{
if (theIndex < 0 || theIndex >= listSize)
{
cout << "theIndex: " << theIndex << "不正确!" << endl;
exit(-1);
}
chainNode<T>* pt = firstnode;
int num = 0;
while (num != theIndex)
{
pt = pt->next;
++num;
}
return pt->element;
}
template<typename T> int Chain<T>::indexOf(const T& theElement) const
{
if (listSize == 0)
{
cout << "线性表为空表,故返回下标-1" << endl;
return -1;
}
chainNode<T>* pt = firstnode;
int num = 0;
while (num < listSize)
{
if (pt->element == theElement)
{
break;
}
pt = pt->next;
++num;
}
if (num >= listSize)
{
cout << "线性链表中不存在该元素,故返回下标: " << endl;
return -1;
}
return num;
}
template<typename T> void Chain<T>::erase(int theIndex)
{
if (theIndex < 0 || theIndex >= listSize)
{
cout << "下标不在线性链表长度范围内!" << endl;
exit(-1);
}
chainNode<T>* pt = firstnode, *curr = NULL;
if (theIndex == 0) //删除firstnode节点
{
curr = firstnode;
firstnode = firstnode->next;
}
else{
int num = 0;
while (num < theIndex - 1)
{
pt = pt->next;
num++;
}
curr = pt->next; //theIndex指向的删除节点
pt->next = curr->next; //删除节点的前一节点转指向删除节点的后一节点
}
--listSize;
delete curr;
}
template<typename T> void Chain<T>::insert(int theIndex, const T& theElement)
{
if (theIndex<0 || theIndex > listSize)
{
cout << "节点插入的下标不正确!" << endl;
exit(-1);
}
if (theIndex == 0)
{
firstnode = new chainNode<T>(theElement, firstnode);
}
else{
chainNode<T> * pt = firstnode, *curr = NULL;
int num = 0;
while (num < theIndex -1)
{
pt = pt->next;
num++;
}
curr = pt->next; //theIndex所在的线性链表中的节点
chainNode<T>* newchainNode = new chainNode<T>(theElement);
newchainNode->next = curr;
pt->next = newchainNode;
}
++listSize;
}
template<typename T> void Chain<T>::output() const
{
if (listSize == 0)
cout << "线性链表为空表!" << endl;
else{
cout << "线性链表输出为:";
chainNode<T>* pt = firstnode;
while (pt != NULL)
{
//pt = firstnode->next;
cout << pt->element << " ";
pt = pt->next;
}
cout << endl;
}
}
int main(int argc, char* argv[])
{
Chain<int> chain;
chain.output();
cout << "size = " << chain.size() << endl;
cout << "\n****************" << endl;
chain.insert(0, 0);
chain.output();
cout << "size = " << chain.size() << endl;
cout << "\n****************" << endl;
chain.insert(1, 1);
chain.insert(2, 2);
chain.output();
cout << "size = " << chain.size() << endl;
cout << "\n****************" << endl;
chain.insert(0, 3);
chain.insert(2, 4);
chain.output();
cout << "size = " << chain.size() << endl;
cout << "\n****************" << endl;
chain.erase(0);
chain.erase(1);
chain.output();
cout << "size = " << chain.size() << endl;
cout << "\n****************" << endl;
cout << "when elemtn = 2 , the index is :" << chain.indexOf(2) << endl;
cout << "\n****************" << endl;
chain.insert(0, 3);
chain.output();
cout << "size = " << chain.size() << endl;
cout << "when index = 2 , the elemnt is :" << chain.get(2) << endl;
chain.~Chain();
return 0;
}
运行: