C++模板类线性表的实现与测试

LinearList.h
#ifndef LINEARLIST_H
#define  LINEARLIST_H
#include<iostream>
#include<stdlib.h>
using namespace std;
template<class T>
class LinearList {
public:
LinearList(int MaxListSize = 10); // constructor
~LinearList() { delete[] element; } // destructor
bool IsEmpty() const { return length == 0; }
int Length() const { return length; }
bool Find(int k, T& x) const;
// return the k'th element of list in x
int Search(const T& x) const;
// return position of x
LinearList<T>& Delete(int k, T& x);
// delete k'th element and return in x
LinearList<T>& Insert(int k, const T& x);
// insert x just after k'th element
void Output(ostream& out) const;
private:
int length;
int MaxSize;
T *element; // dynamic 1D array
};


template<class T>
LinearList<T>::LinearList(int MaxListSize)
{// Constructor for formula-based linear list.
MaxSize = MaxListSize;
element = new T[MaxSize];
length = 0;
}


template<class T>// Set x to the k'th element of the list
bool LinearList<T>::Find(int k, T& x) const
{
// Return false if no k'th; true otherwise.
if (k < 1 || k > length) return false; // no k'th
x = element[k - 1];
return true;
}


template<class T>
int LinearList<T>::Search(const T& x) const
{// Locate x.  Return position of x if found.
// Return 0 if x not in list.
for (int i = 0; i < length; i++)
if (element[i] == x) return ++i;
return 0;
}


template<class T>
LinearList<T>& LinearList<T>::Delete(int k, T& x)
{// Set x to the k'th element and delete it.
// Throw OutOfBounds exception if no k'th element.
if (Find(k, x)) {// move elements k+1, ..., down
for (int i = k; i < length; i++)
element[i - 1] = element[i];
length--;
return *this;
}
else throw "Out of bounds";
return *this;  // visual needs this
}


template<class T>
LinearList<T>& LinearList<T>::Insert(int k, const T& x)
{// Insert x after the k'th element.
// Throw OutOfBounds exception if no k'th element.
// Throw NoMem exception if list is already full.
if (k < 0 || k > length) throw "Out of bounds";
if (length == MaxSize) throw "Out of bounds";
// move one up
for (int i = length - 1; i >= k; i--)
element[i + 1] = element[i];
element[k] = x;
length++;
return *this;
}


template<class T>// Put the list into the stream out.
void LinearList<T>::Output(ostream& out) const
{
for (int i = 0; i < length; i++)
out << element[i] << "  ";
}
template <class T>
ostream& operator<<(ostream& out, const LinearList<T>& x)
{
x.Output(out); 
return out;
}
#endif


LinearList.cpp
#include "stdafx.h"
#include"LinearList.h"
#include<iostream>
using namespace std;
int main()
{
try{
LinearList<int>L(5);
cout << "Length=" << L.Length() << endl;
cout << "isEmpty=" << L.IsEmpty() << endl;
L.Insert(0, 2).Insert(1, 6);
cout << "List is " << L << endl;
cout << "IsEmpty=" << L.IsEmpty() << endl;
int z;
L.Find(1, z);
cout << "First element is " << z << endl;
cout << "Length=" << L.Length() << endl;
L.Delete(1, z);
cout << "List is " << L << endl;
}
catch (...){
cerr << "An exception has occurred" << endl;
}
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值