数据结构之顺序链表

       顺序表存储位置是相邻连续的,可以随即访问的一种数据结构,一个顺序表在使用前必须指定起长度,一旦分配内存,则在使用中不可以动态的更改。他的优点是访问数据是比较方便,可以随即的访问表中的任何一个数据。

       顺序表和数组类似,只不过多了几个自己编写的插入删除之类的接口。




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

template <class T>
class SeqList
{
public:
SeqList(int initialval1,int initialval2,T* initialval3=NULL):pElem(initialval3),nLength(initialval1),maxLength(initialval2)
{
if (0 == initialval2)
{
pElem = NULL;
}
else
 pElem = new T[initialval2]();

}
SeqList(const SeqList& rh)
{
pElem = rh.pElem;
nLength = rh.nLength;
maxLength = rh.maxLength;
}
SeqList operator=(const SeqList& rh);


void Seq_Insert(const T& rh, int i);
void Seq_Delete(int i);


public:
T* pElem;
int nLength;
int maxLength;
};


template <class T>
SeqList<T> SeqList<T>::operator=(const SeqList& rh)
{
SeqList<T> seq;
seq.nLength = rh.nLength;
seq.maxLength = rh.maxLength;
seq.pElem = new T[maxLength]();
memcpy(seq.pElem, rh.pElem, maxLenght * sizeof(T));

return seq;
}


template <class T>
void SeqList<T>::Seq_Insert(const T& rh, int i)
{
if (i<=nLength)
{
pElem[i-1] = rh;
nLength++;
for (int j = i ; j < nLength + 1 && j < maxLength; j++)
{
pElem[j] = pElem[1 + i++];
}
}
else
{
cout << "Insert Error!" << endl;
}



}


template <class T>
void SeqList<T>::Seq_Delete(int i)
{
if (i<=nLength)
{
for (int j=i-1;i<nLength-1;)
{
pElem[j] = pElem[i++];
}
}
else
{
cout << "Delete Error!" << endl;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值