数据结构(殷人琨版)学习笔记之顺序表
把书上的伪代码整理了一下:
/***************************************************************************
* file name : LinearList.h
* created : 2011/11/01
* description :
* author : Gavin Dai XLX
* update :
****************************************************************************/
#ifndef __LINEARLIST_H
#define __LINEARLIST_H
/*
这是一个纯虚类,相当于就是一个接口
*/
template<class T>
class LinearList
{
public:
LinearList() { };
~LinearList() { };
virtual int size()const = 0;//求表的容量
virtual int length()const = 0;//求当前表中元素的个数
virtual int search(T& x)const = 0;//在表中查找x
virtual int locate(int i) const = 0;//在表中定位第i个元素的位置
virtual bool getData(int i, T& x)const = 0;//取表中第i个元素的值
virtual void setData(int i, T& x)const = 0;//设置表中第i个元素的值为x的值
virtual bool insert(int i, T& x) = 0;//在表的第i项后插入x
virtual bool remove(int i, T& x) = 0;//删除第i个表项,通过x返回
virtual bool isEmpty()const = 0;
virtual bool isFull()const = 0;
//virtual void sort() = 0;//排序
virtual void input() = 0;//输入
virtual void output() = 0;//输出
//virtual LinearList<T> operator=(LinearList<T>& L) = 0;
};
#endif
/***************************************************************************
* file name : SeqList.h
* created : 2011/11/01
* description :
* author : Gavin Dai XLX
* update :
****************************************************************************/
#ifndef __SEQLIST_H
#define __SEQLIST_H
#include "LinearList.h"
#include <iostream>
#include <cstdlib>
using namespace std;
const int defaultSize = 100;
template <class T>
class SeqList : public LinearList<T>
{
public:
SeqList(int sz = defaultSize);
SeqList(SeqList<T>& l);
~SeqList() { delete[] data; }
int size()const { return maxSize; }
int length()const { return last + 1; }
int search(T& x)const;
int locate(int i)const;
bool getData(int i, T& x)const
{
if (i > 0 && i <= last + 1)
{
x = data[i-1];
return true;
}else
{
return false;
}
}
void setData(int i, T& x)const
{
if (i >0 && i <= last + 1)
{
data[i-1] = x;
}
}
bool insert(int i, T& x);
bool remove(int i, T& x);
bool isEmpty()const
{
return (last == -1) ? true : false;
}
bool isFull()const
{
return (last == maxSize - 1) ? true : false;
}
void input();
void output();
SeqList<T>& operator=(SeqList<T>& l);
protected:
T* data;
int maxSize;
int last;
void reSize(int newSize);
};
#endif
/***************************************************************************
* file name : SeqList.cpp
* created : 2011/11/01
* description :
* author : Gavin Dai XLX
* update :
****************************************************************************/
#include "SeqList.h"
template <class T>
SeqList<T>::SeqList(int sz /* = defaultSize */)
{
if (sz > 0)
{
maxSize = sz;
last = -1;
data = new T[maxSize];
if (data == NULL)
{
cerr<<"存储分配错误"<<endl;
exit(1);
}
}
}
template <class T>
SeqList<T>::SeqList(SeqList<T>& l)
{
maxSize = l.size();
last = l.length() - 1;
T value;
data = new T[maxSize];
for (int i = 1; i <= last + 1; i++)
{
value = getData(i, value);
data[i-1] = value;
}
}
template <class T>
void SeqList<T>::reSize(int newSize)
{
if (newSize <= 0)
{
cerr<<"无效的数组大小"<<endl;
return;
}
if (newSize != maxSize)
{
T *newArray = new T[newSize];
if (NULL == newArray)
{
cerr<<"存储分配错误"<<endl;
exit(1);
}
int n = last + 1;
T *srcptr = data;
T *dstptr = newArray;
while(n--)
{
*dstptr++ = *srcptr++;
}
delete[] data;
data = newArray;
maxSize = newSize;
}
}
template <class T>
int SeqList<T>::search(T& x)const
{
for (int i = 0; i <= last; i++)
{
if (data[i] == x)
{
return i + 1;
}
}
return 0;
}
template <class T>
int SeqList<T>::locate(int i)const
{
if (i >= 1 && i <= last + 1)
{
return i;
}
return 0;
}
template <class T>
bool SeqList<T>::insert(int i, T& x)
{
if (last == maxSize - 1)
{
return false;
}
if (i < 0 || i > last + 1)
{
return false;
}
for (int j = last; j >= i; j--)
{
data[j+1] = data[j];
}
data[i] = x;
last++;
return true;
}
template <class T>
bool SeqList<T>::remove(int i, T &x)
{
if (last == -1)
{
return false;
}
if (i < 1 || i > last + 1)
{
return false;
}
x = data[i-1];
for (int j = i; j <= last; j++)
{
data[j-1] = data[j];
}
last--;
return true;
}
template <class T>
void SeqList<T>::input()
{
cout<<"开始建立顺序表,请输入表中元素的个数:"<<endl;
while(1)
{
cin>>last;
if (last <= maxSize - 1)
{
break;
}
cout<<"表中元素个数输入有误,范围不能超过"<<maxSize - 1<<";";
}
for (int i = 0; i <= last; i++)
{
cin>>data[i];
cout<<i + 1<<endl;
}
}
template <class T>
void SeqList<T>::output()
{
cout<<"顺序表当前元素最后位置为:"<<last<<endl;
for(int i = 0; i <= last; i++)
{
cout<<"#"<<i + 1<<":"<<data[i]<<endl;
}
}
template <class T>
SeqList<T>& SeqList<T>::operator =(SeqList<T> &l)
{
maxSize = l.size();
last = l.length() - 1;
T value;
data = new T[maxSize];
for (int i = 1; i <= last + 1; i++)
{
value = getData(i, value);
data[i-1] = value;
}
return *this;
}
/***************************************************************************
* file name : main.cpp
* created : 2011/11/01
* description :
* author : Gavin Dai XLX
* update :
****************************************************************************/
#include "SeqList.cpp"
int main()
{
SeqList<int> l(100);
SeqList<int> q;
l.input();
l.output();
cout<<l.length();
int i = 22;
l.insert(1,i);
l.output();
int iData = 89;
l.setData(1,iData);
l.output();
l.remove(3, iData);
cout<<"iData = "<<iData;
q = l;
cout<<"表q的元素:";
q.output();
system("pause");
}