永发信息网

如何在C++中建立一个顺序表

答案:1  悬赏:70  手机版
解决时间 2021-03-17 12:26
如何在C++中建立一个顺序表
最佳答案

#include <iostream>
using namespace std;
//ADT抽象数据类型
template <class 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 indeOf(const T& theElement)const = 0;  
virtual void erase(int theIndex) = 0;
virtual void insert(int theIndex, const T& theElement) = 0;
virtual void output(ostream& out)const = 0;
};

template <class 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 <class T>
class chain :public linearList<T>
{
public:
//构造函数
chain(int initialCapacity = 10);
chain(const chain<T>&);
~chain();

//抽象数据类型ADT的方法
bool empty()const{ return listSize == 0; }
int size()const{ return listSize; }
T& get(int theIndex)const;
int indeOf(const T& theElement)const;
void erase(int theIndex);
void insert(int theIndex, const T& theElement);
void output(ostream& out)const;




protected:
//检查索引,无效抛出异常
void checkIndex(int theIndex)const;
chainNode<T>* firstNode;
int listSize;
};
template <class T>
chain<T>::chain(int initialCapacity = 10)
{
//if (initialCapacity < 1)
//{
// throw 1;
//}
firstNode = NULL;
listSize = 0;
}
template <class T >
chain<T>::chain(const chain<T>& theList)
{
listSize = theList.listSize;
if (listSize == 0)
{
firstNode = NULL;
return;
}
chainNode <T> sourceNode = theList.firstNode;
firstNode = new chainNode<T>(sourceNode->element);
chainNode<T>* targetNode = firstNode;
while (sourceNode != NULL)
{
targetNode->next = new chainNode<T>(sourceNode->element);
targetNode = targetNode->next;
sourceNode = sourceNode->next;
}
targetNode->next = NULL;
}
template <class T>
void chain<T>::checkIndex(int theIndex)const
{
if (theIndex < 0 || theIndex >= listSize)
{
throw 1;
}
}


template<class T>
chain<T>::~chain()
{
while (firstNode != NULL)
{
chainNode<T>* nextNode = firstNode->next;
delete firstNode;
firstNode = nextNode;
}
}
template <class T>
T& chain<T>::get(int theIndex)const
{
try
{
checkIndex(theIndex);
}
catch (int)
{
cout << "索引无效!!" << endl;
}
chainNode<T>* currentNode = firstNode;
for (int i = 0; i < theIndex; i++)
currentNode = currentNode->next;
return currentNode->element;
}
template <class T>
int chain<T>::indeOf(const T& theElement)const
{
chainNode<T>* currentNode = firstNode;
int index = 0;
while (currentNode != NULL&&currentNode->element != theElement)
{
currentNode = currentNode->next;
index++;
}
if (currentNode == NULL)
return -1;
else
return index;
}


template <class T>
void chain<T>::erase(int theIndex)
{
try
{
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;
}
catch (int)
{
cout << "索引无效" << endl;
}

}


template <class T>
void chain<T>::insert(int theIndex, const T& theElement)
{
if (theIndex < 0 || theIndex>listSize)
cout << "索引不正确";
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 <class T>
void chain<T>::output(ostream& out)const
{
for (chainNode<T>* currentNode = firstNode; currentNode != NULL; currentNode = currentNode->next)
{
out << currentNode->element << " ";
}
}
template <class T>
ostream& operator<<(ostream& out, const chain<T>& x)
{
x.output(out);
return out;
}
//点击我头像有惊喜
int main()
{
chain<int> a(3);
a.insert(0, 1);
a.insert(1, 2);
a.insert(2, 3);
a.insert(3, 4);
a.insert(0, 5);
chain<char> b;
b.insert(0, 'A');
b.insert(1, 'B');
b.insert(2, 'C');
//专业解答.
cout << a<<endl;
cout << b << endl;
cout << a.get(0);
cout << a.get(1);
return 0;
}嗯大致就是这些内容,使用了很多C++的知识
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
随心菜馆这个地址在什么地方,我要处理点事
石头作文
麻阳隶属怀化市还是隶属湘西州
你好,160T冲床怎么刹不住车
一台电脑上有两个UG软件一个是8.0一个是9.0打
形容时机到了,事情就自然成功了的成语
教育和就业细节表怎么填啊
5/14乘于21/25乘以五分之七这道题的
以下关于酶和ATP相互关系的叙述,正确的是A.A
小汽车行驶一定要配备三角警示标置吗?
草菇肉片的做法步骤图,草菇肉片怎么做好吃
键子的组词有哪些
我是南通启东人 档案被调到了南通人才市场 要
水果批发中心我想知道这个在什么地方
我的事业在中国我的成就在中国我的归宿在中国
推荐资讯
win7 开始菜单 任务栏 没有最近项目记录
我是长头发,为什么感觉头发洗的越勤油的越快
打工被辞退,有工资么??
宝马b58最内侧的火花塞如何换
一个古人去看望朋友,到了朋友家发现朋友不在
大学英语四级每年什么时候开考?
最早测出大气压数值的科学家是意大利的______
为什么会梦见去世的人过得不好?
元方是一个怎样的孩子从文中找出依据加以说明
创维40x3是智能机吗
皖北可以种植四角豆吗?
100打8万 还是要进攻 这的确是神话
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?