数据结构与算法 c++语言描述这本书的链表类该怎么用

w98z3h24 2017-09-22 11:20:07
#ifndef lianbiao
#define lianbiao
#include<iostream>

template<typename T>
struct chainNode
{
T element;
chainNode<T> *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;
virtual int size() const=0;
virtual T & get(int theIndex) const=0;
virtual int indexOf(const T & theElement) const=0;
virtual void erase(int theIndex)=0;
virtual void insert(int theIndex,const T & theElement)=0;
virtual void output(std::ostream & out) const=0;
};

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(std::ostream & out) const;
protected:
void checkIndex(int theIndex) const; //索引无效,抛出异常
chainNode<T> *firstNode; //指向链表第一个节点的指针
int listSize; //链表的元素个数
};

#endif




#include<iostream>
#include<sstream>
#include<stdexcept>
#include"chain.h"

using namespace std;

template <typename T>
chain<T>::chain(int initialCapacity)
{
if (initialCapacity<1)
{
ostringstream s;
s<<"Initial capacity = "<<initialCapacity<<" Must be > 0";
throw illegalParameterValue(s.str());
}
firstNode=nullptr;
listSize=0;
}

template<typename T>
chain<T>::chain(const chain<T> & theList)
{
listSize=theList.listSize;
if (listSize==0)
{
firstNode=nullptr;
return;
}
chainNode<T> *sourceNode=theList.firstNode;
firstNode=new chainNode<T> (sourceNode->element);
sourceNode=sourceNode->next;
chainNode<T> *targetNode=firstNode;
while (sourceNode!=nullptr)
{
targetNode->next=new chainNode<T> (sourceNode->element);
targetNode=targetNode->next;
sourceNode=sourceNode->next;
}
targetNode->next=nullptr;
}

template<typename T>
chain<T>::~chain()
{
while (firstNode!=nullptr)
{
chainNode<T> *nextNode=firstNode->next;
delete firstNode;
firstNode=nextNode;
}
}

template<typename T>
T & chain<T>::get(int theIndex) const
{
checkIndex(theIndex);
chainNode<T> *currentNode=firstNode;
for (int i=0;i<theIndex;i++) currentNode=currentNode->next;
return currentNode->element;
}

template<typename T>
int chain<T>::indexOf(const T & theElement) const
{
chainNode<T> *currentNode=firstNode;
int index=0;
while (currentNode!=nullptr && currentNode->element!=theElement)
{
currentNode=currentNode->next;
index++;
}
if (currentNode==nullptr) return -1;
else return index;
}

template<typename T>
void chain<T>::erase(int theIndex)
{
checkIndex(theIndex);
chainNode<T> *deleteNode;
if (theIndex==0)
{
deleteNode=firstNode;
firstNode=firstNode->next;
}
else
{
chainNode<T> *p=firstNode;
for (int i=0;i<theIndex-1;i++) p=p->next;
deleteNode=p->next;
p->next=p->next->next;
}
listSize--;
delete deleteNode;
}

template<typename T>
void chain<T>::insert(int theIndex,const T & theElement)
{
if (theIndex<0 || theIndex>listSize)
{
ostringstream s;
s<<"index = "<<theIndex<<" size = "<<listSize;
throw illegalIndex(s.str());
}
if (theIndex==0) firstNode=new chainNode<T> (theElement,firstNode);
else
{
chainNode<T> *p=firstNode;
for (int i=0;i<theIndex-1;i++) p=p->next;
p->next=new chainNode<T> (theElement,p->next);
}
listSize++;
}

template<typename T>
void chain<T>::output(ostream & out) const
{
for (chainNode<T> *currentNode=firstNode;currentNode!=nullptr;currentNode=currentNode->next) out<<currentNode->element<<" ";
}

template<typename T>
ostream & operator<<(ostream & out,const chain<T> & x)
{
x.output(out);
return out;
}


不知道如何初始化,用int 进行初始化出现 未命名1.cpp:(.text+0x55): undefined reference to `chain<int>::chain(int)'
未命名1.cpp:(.text+0x61): undefined reference to `chain<int>::~chain()'
小白请大神解答
...全文
199 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

64,371

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧