顺序表基于数组,优点是简单方便、寻址和遍历非常快;缺点是删除元素和添加元素时元素的后面所有元素位置都要发生移动。
程序如下:
List.h
#ifndef LIST_H
#define LIST_H
template<typename Elem>//自定义的类Elem对象(如coordinate类)需要重载<<和==
class List
{
public:
List(int size);
~List();
void clearList();//清空表
bool listEmpty();//判断是否为空表
int listLength();//表元素长度
bool getElem(int i,Elem *e);//获得第i(0开始)个元素赋值给e
int locateElem(Elem *e);//找出与e相等的元素并返回元素下标
bool priorElem(Elem* currentElem,Elem* preElem);//用指针类型可以节省空间,找到和当前元素一样的值的元素的前一个元素赋给preElem
bool nextElem(Elem* currentElem,Elem* nextElem);
bool listInsert(int i,Elem*e);//在i位置插入元素
bool listDelete(int i,Elem *e);//删除i位置元素
void listTraverse();//遍历
private:
Elem *m_pList;
int m_iSize;
int m_iLength;
};
template<typename Elem>
List<Elem>::List(int size)
{
m_iSize = size;
m_pList = new Elem[m_iSize];
m_iLength = 0;
cout << "List<Elem>::List(int size)" << endl;
}
template<typename Elem>
List<Elem>::~List()
{
delete []m_pList;
m_pList = NULL;
}
template<typename Elem>
void List<Elem>::clearList()
{
m_iLength = 0;
}
template<typename Elem>
bool List<Elem>::listEmpty()
{
if(m_iLength == 0)return true;
return false;
}
template<typename Elem>
int List<Elem>::listLength()
{
return m_iLength;
}
template<typename Elem>
bool List<Elem>::getElem(int i,Elem *e)
{
if((i >= 0)&&(m_iLength > i)){
*e = m_pList[i];
return true;
}else{
return false;
}
}
template<typename Elem>
int List<Elem>::locateElem(Elem *e)
{
int i;
for(i = 0;i < m_iLength;i ++){
if(*e == m_pList[i]){
return i;
}
}
cout << "没有相同的元素" << endl;
return -1;
}
template<typename Elem>
bool List<Elem>::priorElem(Elem* currentElem,Elem* preElem)
{
int temp = locateElem(currentElem);//取名temp因为它是一个临时的返回值
if(temp > 0){
*preElem = m_pList[temp - 1];
return true;
}else{
return false;
}
}
template<typename Elem>
bool List<Elem>::nextElem(Elem* currentElem,Elem* nextElem)
{
int temp = locateElem(currentElem);//取名temp因为它是一个临时的返回值
if((temp >= 0)&&(temp < (m_iLength - 1))){
*nextElem = m_pList[temp + 1];
return true;
}else{
return false;
}
}
template<typename Elem>
void List<Elem>::listTraverse()
{
if(!listEmpty()){
for(int i = 0;i < m_iLength;i ++){
cout << m_pList[i] << ",";
}
cout << endl;
}else{
cout << "空表!" << endl;
}
}
template<typename Elem>
bool List<Elem>::listInsert(int i,Elem*e)
{
if((i >= 0)&&(i <= m_iLength)&&(m_iLength < m_iSize)){
for(int temp = m_iLength-1;temp >= i;temp--){
m_pList[temp+1] = m_pList[temp];
}
m_pList[i] = *e;
m_iLength ++;//别忘了!
return true;
}else{
return false;
}
}
template<typename Elem>
bool List<Elem>::listDelete(int i,Elem *e)
{
if((i >= 0)&&(i < m_iLength)&&(!listEmpty())){
*e = m_pList[i];
for(int temp = i;temp < m_iLength-1;temp++){
m_pList[temp] = m_pList[temp+1];
}
m_iLength --;
return true;
}else{
return false;
}
}
#endif
类模板没有List.cpp
demo.cpp
#include<iostream>
#include<stdlib.h>
#include"List.h"
using namespace std;
//线性表(包括顺序表和链表)——顺序表(数组)c++实现
//遍历和寻址非常快、方便
//缺点是插入和删除元素时,元素后面的所有元素都要移动(链表即可改善)
int main(){
List<int> l1(5);
int a,e = 10;
int e2 = 11;
l1.listInsert(0,&e);
l1.listInsert(0,&e);
l1.listInsert(0,&e);
l1.listInsert(0,&e2);
l1.listInsert(0,&e);
l1.listTraverse();
l1.listDelete(3,&a);
cout << a << endl;
l1.listTraverse();
l1.listInsert(0,&e2);
l1.listTraverse();
l1.getElem(0,&a);
cout << a << endl;
cout << l1.listLength() << endl;
cout << l1.listEmpty() << endl;
l1.nextElem(&a,&e);
cout << e << endl;
l1.clearList();
cout << l1.listEmpty() << endl;
l1.locateElem(&a);
l1.listTraverse();
l1.priorElem(&a,&e);
system("pause");
return 0;
}
运行结果如图: