数据结构之顺序链表
2016-04-24 19:36
525 查看
顺序表存储位置是相邻连续的,可以随即访问的一种数据结构,一个顺序表在使用前必须指定起长度,一旦分配内存,则在使用中不可以动态的更改。他的优点是访问数据是比较方便,可以随即的访问表中的任何一个数据。
顺序表和数组类似,只不过多了几个自己编写的插入删除之类的接口。
#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;
}
}
顺序表和数组类似,只不过多了几个自己编写的插入删除之类的接口。
#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;
}
}
相关文章推荐
- STL栈之表达式求值
- 循环队列实现杨辉三角及数据结构
- 数据结构实验二 栈、队列
- 双向链表
- 数据结构部分整理
- 数据结构算法3_2 迷宫求解
- 数据结构二叉树
- 数据结构(一)
- 数据结构算法——顺序表应用
- 栈的应用实践——括号匹配的检验
- 234. Palindrome Linked List
- 第六章-数据结构入门
- Android JNI 使用的数据结构JNINativeMethod详解
- 树的子结构[数据结构]
- 数据结构(c)——队列
- 数据结构_写程序计算给定多项式在给定点处的值
- 【数据结构】堆的实现以及简单的函数
- 数据结构-栈的实现之运算式求值
- MySQL索引背后的数据结构及算法原理
- 数据结构_分别使用for循环和递归打印1-N的所有整数,比较运行时间