线性表-顺序表

顺序表:把线性表中的所有表项按照其逻辑顺序依次存储到从计算机存储中指定存储位置开始的一块连续的存储空间
SeqList.h

#ifndef SEQLIST_H
#define SEQLIST_H
#include"linearList.h"

const int defaultSize = 100;

template<typename T>
class SeqList:public LinearList<T>{
protected:
T* data;
int maxSize;
int last;
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;
void setData(int i, T &x);
//插入删除都从1开始去移动
bool Insert(int i, T &x);
bool Remove(int i, T &x);
bool IsEmpty()const;
bool IsFull()const;
void Sort();
void input();
void output();
void reSize(int newSize);
SeqList<T>operator =(SeqList<T>& L);
};

#endif



SeqList.cpp

#include<iostream>
#include<stdlib.h>
#include"SeqList.h"
using namespace std;

template<typename T>
SeqList<T>::SeqList(int sz)
{
if(sz>0){
maxSize = sz-1;
last = -1;
data = new T[maxSize];
if(NULL==data){
cerr << "Memory allocation error" << endl;
}
}
}

template<typename T>
SeqList<T>::SeqList(SeqList<T> &L)
{
maxSize = L.Size();
last = L.Length()-1;
T value;
data = new T[maxSize];
if(NULL==data){
cerr << "Memory allocation error" << endl;
}else{
for(int i=1;i<=last+1;++i){
L.getData(i,value);
data[i-1]=value;
}
}
}

template<typename T>
void SeqList<T>::reSize(int newSize)
{
if(newSize<=0){
cerr << "invalid size" << endl;
}else if(newSize!=maxSize){
T* newArray = new T[newSize];
if(NULL==newArray){
cerr << "Memory allocation error" << endl;
}else{
int num = newSize>=(last+1)?(last+1):newSize;//如果newSize比length还小
T* srcptr = data;
T* destptr = newArray;
for(int i=0;i<num;++i){
destptr[i]=srcptr[i];
}
delete []data;//删除老数组
data = newArray;//复制新数组
maxSize = newSize;
last = num-1;
}
}
}

template<typename T>
int SeqList<T>::Search(T &x) const
{
for(int i=0;i<=last;++i){
if(data[i]==x){
return i+1;
}
}
cerr << "not found " << endl;
return -1;
}

template<typename T>
int SeqList<T>::Locate(int i) const
{
if(i>=1&&i<=last+1)
return i;
return 0;
}

template<typename T>
bool SeqList<T>::getData(int i, T &x) const
{
if(i>0&&i<=last+1){
x = data[i-1];
return true;
}else{
return false;
}
}

template<typename T>
void SeqList<T>::setData(int i, T &x)
{
if(i>0&&i<=last+1){
data[i-1]=x;
}
}

template<typename 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;
}

/*
x返回删除元素的值
*/
template<typename T>
bool SeqList<T>::Remove(int i, T &x)
{
if(i<=0||i>last+1)//无效位置
return false;
x = data[i-1];
for(int j=i-1;j<last;j++){
data[j]=data[j+1];
}
last--;
return true;
}

template<typename T>
bool SeqList<T>::IsEmpty() const
{
return last==-1?true:false;//-1表示空
}

template<typename T>
bool SeqList<T>::IsFull() const
{
return (last+1)==maxSize?true:false;
}

template<typename T>
void SeqList<T>::Sort()
{
int temp;
for(int i=0;i<=last;++i){
for(int j=i+1;j<=last+1;++j){
if(data[j]>data[i]){
temp = data[j];
data[j] = data[i];
data[i] = temp;
}
}
}
}

/*
从键盘输入建立顺序表
*/
template<typename T>
void SeqList<T>::input()
{
cout << "please input the size of table:";
int i;
while(true){
cin >> i;
last = i-1;
if(last<maxSize)
break;
cout << "invalid size,need less than" << maxSize << endl;
}
for(int i=0;i<=last;++i){
cout <<i+1<<":";
cin >> data[i];
}
}

template<typename T>
void SeqList<T>::output()
{
for(int i=0;i<=last;++i){
cout << data[i] << " ";
}
cout << endl;
}

template<typename T>
SeqList<T> SeqList<T>::operator=(SeqList<T>& L)
{
maxSize = L.Size();
last = L.Length()-1;
T *value;
data = new T[maxSize];
if(NULL==data){
cerr << "Memory allocation error" << endl;
}
for(int i=0;i<=last;++i){
L.getData(i+1,value);
data[i]=value;
}
}

int main()
{
//测试1,2,3,4,5,6,7,8,9,10
SeqList<int> seq;
seq.input();
seq.Sort();
int num = 100;
seq.Insert(3,num);
seq.getData(5,num);
cout << num << endl;
seq.output();
SeqList<int> seq2 = seq;
seq2.output();
seq2.reSize(8);
seq2.output();
}

7
10 9 8 100 7 6 5 4 3 2 1
10 9 8 100 7 6 5 4 3 2 1
10 9 8 100 7 6 5 4

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值