1. 程式人生 > 其它 >順序表(用模板寫,包括插入、刪除等基本操作)

順序表(用模板寫,包括插入、刪除等基本操作)

//標頭檔案sq_list.h

#define _SQ_LIST_H_
#ifndef DEFAULT_SIZE
#define DEFAULT_SIZE 1000//預設元數個數
//順序表模板
template <class ElemType>
class SqList
{
protected://資料成員
ElemType* elems;//元素儲存空間
int maxSize;//順序表最大元素個數;
int count;//元素個數
public:
SqList(int size = DEFAULT_SIZE);//建構函式模板
virtual~SqList();//解構函式模板
int Length()const;//求線性表長度
bool Empty()const;//判斷線性表是否為空
void Clear();//將線性表清空
void Traverse(void(*vist)(const ElemType&))const;//遍歷線性表
bool GetElem(int position, ElemType& e);//求指定位置的元素值
bool SetElem(int position, const ElemType& e);//設定指定位置的元素值
bool Delete(int position, ElemType& e);//刪除元素
bool Delete(int position);
bool Insert(int position, const ElemType& e);//插入元素
SqList(const SqList<ElemType>& source);//複製建構函式模板
SqList<ElemType>& operator=(const SqList<ElemType>& source);//過載運算子
};
//順序表類模板實現部分
template<class ElemType>
SqList<ElemType>::SqList(int size)//構造一個最大元素個數為size的空順序表
{
maxSize = size;//最大元素個數
elems = new ElemType[maxSize];//分配儲存空間
count = 0;//空線性表元素個數為0;
}
template<class ElemType>
SqList<ElemType>::~SqList()//銷燬線性表
{
delete[]elems;//釋放儲存空間
}
template<class ElemType>
int SqList<ElemType>::Length()const {//返回線性表元素個數
return count;
}
template<class ElemType>
bool SqList<ElemType> ::Empty()const//判斷線性表是否為空,為返回1,否為0
{
return count == 0;
}
template<class ElemType>
void SqList<ElemType> ::Clear()//清空線性表
{
count= 0;
}
template<class ElemType>
void SqList<ElemType> ::Traverse(void(*vist)(const ElemType&))const
//依次對線性表的每個元素呼叫函式(*visit)
{
for (int temPos = 1; temPos <= Length(); temPos++)//對線性表的每個元素呼叫函式(*visit)
{
(*vist)(elems[temPos - 1]);
}
}
template<class ElemType>
bool SqList<ElemType> ::GetElem(int position, ElemType& e)
//當線性表存在第position個元素是,用e返回其值,返回ture,否則返回false
{
if (position<1 || position>Length()) {
return false;//範圍錯誤
}
else {//範圍正確
e = elems[position - 1];
return true;
}
}
template<class ElemType>
bool SqList<ElemType> ::SetElem(int position, const ElemType& e)
//刪除線性表第position個元素,並用e返回其值
//position合法時返回ture,否則false
{
if (position<1 || position>Length()) {
return false;
}
else {//position合法
elems[position - 1] = e;
return true;
}
}
template<class ElemType>
bool SqList<ElemType> ::Delete(int position, ElemType& e)//刪除線性表的第position個元素
{//涉及到線性表position的每次都要判斷position是否合法
if (position<1 || position>Length()) {
return false;
}
else {//position合法
ElemType temElem;
for (int temPos = position + 1; temPos <= Length(); temPos++)
//被刪除元素之後的元素依次左移
{
GetElem(temPos, temElem);//用返回被刪除元素的值
SetElem(temPos - 1, temElem);
}
count--;
return true;//刪除成功
}
}
template<class ElemType>
bool SqList<ElemType> ::Delete(int position)//刪除線性表的第position個元素
{//涉及到線性表position的每次都要判斷position是否合法
if (position<1 || position>Length()) {
return false;
}
else {//position合法
ElemType temElem;
for (int temPos = position + 1; temPos <= Length(); temPos++)
//被刪除元素之後的元素依次左移
{
GetElem(temPos, temElem);//用返回被刪除元素的值
SetElem(temPos - 1, temElem);
}
count--;
return true;//刪除成功
}
}
template<class ElemType>
bool SqList<ElemType> ::Insert(int position, const ElemType& e){
//線上性表的第position個元素前插入元素e
if (count == maxSize)//線性表已滿返回false;
return false;
else if (position<1 || position>Length() + 1) //範圍錯誤
return false;
else {//position合法
ElemType temElem;//臨時元素
for (int temPos = Length(); temPos >= position; temPos--) {
GetElem(temPos, temElem);
SetElem(temPos+1, temElem);
}
count++;
SetElem(position, e);
return true;
}
}
template<class ElemType>
SqList<ElemType> ::SqList(const SqList<ElemType>& source) {
//線性表source構造新的線性表表--複製構造模板
maxSize = source.maxSize;
count = source.maxSize;
elems = new ElemType[maxSize];
for (int temPos = 1; temPos <= source.Length(); temPos++) {
//複製資料元素
elems[temPos - 1] = source.elems[temPos - 1];
}
}
template<class ElemType>
SqList<ElemType>& SqList<ElemType>:: operator=(const SqList<ElemType>& source)
//將線性表source賦值給當前線性表--過載賦值運算子
{
if (&source != this)//防止自賦值
{
maxSize = source.maxSize;
count = source.count;
delete[]elems;
elems = new ElemType[maxSize];
for (int temPos = 1; temPos <= source.Length(); temPos++)//
{
elems[temPos-1]= source.elems[temPos - 1];
}
}
return *this;
}
#endif

//原始檔main.cpp

#include<iostream>
using namespace std;
#include "sq_list.h"
template<class ElemType>
void Show(const ElemType& e)
{//顯示資料元素
cout << e << " ";
}
int main() {
char select = '0';
SqList<double>la, lb;
double e;
int position;
while (select != '7')
{
cout << endl << "1.生成線性表.";
cout << endl << "2.顯示線性表.";
cout << endl << "3.檢索元素.";
cout << endl << "4.設定元素值.";
cout << endl << "5.刪除元素值.";
cout << endl << "6.插入元素.";
cout << endl << "7.退出.";
cout << endl << "選擇功能(1-7):";
cin >> select;
switch (select)
{
case '1'://生成線性表
cout << endl << "輸入e(e=0時退出)";
cin >> e;
while (e != 0)
{
la.Insert(la.Length() + 1, e);
cin >> e;
}
break;
case '2'://顯示線性表
lb = la;
lb.Traverse(Show<double>);
break;
case '3'://檢索元素
cout << endl << "輸入元素位置值:";
cin >> position;
if (!la.GetElem(position, e))
cout << "沒有此位置的元素" << endl;
else
cout << "元素:" << e << endl;
break;
case'4'://設定元素值
cout << endl << "輸入要替換的位置:";
cin >> position;
cout << endl << "輸入替換為的元素:";
cin >> e;
if (!la.SetElem(position, e))
cout << "位置範圍錯誤." << endl;
else
cout << "設定成功." << endl;
break;
case '5'://刪除元素值
cout << endl << "輸入位置:";
cin >> position;
if (!la.Delete(position, e) && !la.Delete(position))
cout << "位置範圍錯." << endl;
else
cout << "被刪除的元素值:" << e << endl;
break;
case'6'://插入元素
cout << endl << "請輸入位置:";
cin >> position;
cout << endl << "請輸入元素值:";
cin >> e;
if (!la.Insert(position, e))
cout << "位置範圍錯誤." << endl;
else
cout << "成功插入" << e << endl;
break;
}
}
return 0;
}