@这篇文档是由C++代码实现的顺序线性表也就是数组
本节中将数组当成一种数据结构来看待,数组就是一个连续存储的线性表。从这些代码里面要明白数据结构和数据类型和抽象数据类型的区别。
数据结构用来反映一个数据的内部构成,即一个数据由哪些成分数据构成,以什么方式构成,呈什么结构。数据结构有逻辑上的数据结构和物理上的数据结构之分,逻辑上的数据结构反应成分数据之间的逻辑关系,物理上的数据结构反映成分数据在计算机内的存储安排。
数据类型是指具有相同数据结构的数据属于同一类,同一类数据的全体称为一个数据类型。
抽象数据类型可理解为数据类型的进一步抽象,它把数据类型和数据类型上的运算捆在一起,进行封装。
本文是以Visual Studio中新建的C++win32的控制台应用程序实现的,其中建立了头文件和源文件以及实现main()的demo文件,分别是List.h和List.cpp和demo.cpp文件。
List.h文件
#ifndef LIST_H
#define LIST_H
template <typename T>
class List
{
public:
List(int size); //构造函数,初始化List的容量空间
virtual ~List(); //析构函数
void ClearList(); //清空线性表
bool ListEmpty(); //判断线性表是否为空
int ListLength(); //获取线性表长度
bool GetElement(int i,T *e); //获取线性表中下标为i的元素,并返回存入e中
int LocateElement(T *e); //寻找第一个满足e的数据元素的位序
bool PriorElement(T *ce,T *pe); //获取指定元素ce的前驱pe
bool NextElementa(T *ce, T *ne); //获取指定元素ce的后继ne
bool ListInsert(int i ,T *e); //在第i个位置插入元素e
bool ListDelete(int i ,T *e); //删除第i个位置的元素e
void ListTraversal(); //遍历线性表
private:
T *m_pList;
int m_iSize;
int m_iLength;
};
#endif
List.cpp文件
#include "List.h"
#include <iostream>
using namespace std;
/*线性表构造函数*/
template <typename T>
List<T>::List(int size)
{
m_iSize = size;
m_pList = new T[m_iSize];
ClearList();
}
/*线性表析构函数*/
template <typename T>
List<T>::~List()
{
delete []m_pList;
m_pList = NULL;
}
/*将当前线性表清空*/
template <typename T>
void List<T>::ClearList()
{
m_iLength = 0;
}
/*判断当前线性表是否为空*/
template <typename T>
bool List<T>::ListEmpty()
{
return m_iLength==0?true:false;
}
/*返回当前线性表的长度*/
template <typename T>
int List<T>::ListLength()
{
return m_iLength;
}
/*获取位序为i的元素的值并返回给e*/
template <typename T>
bool List<T>::GetElement(int i,T *e)
{
if (i>0 || i <= m_iSize)
{
*e = m_pList[i];
return true;
}
else
{
return false;
}
}
/*在线性表内寻找值为e的元素的第一次出现的位序*/
template <typename T>
int List<T>::LocateElement(T *e)
{
for (int i=0;i < m_iLength;i++)
{
if (m_pList[i]==*e)
{
return i;
break; //返回第一个与寻找值相等的元素的下标就要推出循环,如果出现重复值,那么就会多次返回i
}
}
}
/*在线性表内寻找值为ce的元素的前驱元素并返回给pe*/
template <typename T>
bool List<T>::PriorElement(T *ce,T *pe)
{
for (int i=1;i < m_iLength;i++)
{
if (m_pList[i] == *ce)
{
*pe = m_pList[i-1];
return true;
}
}
}
/*在线性表内寻找值为ce的元素的后继元素并返回给ne*/
template <typename T>
bool List<T>::NextElementa(T *ce, T *ne)
{
for (int i=0;i <(m_iLength-1);i++)
{
if (m_pList[i] == *ce)
{
*ne = m_pList[i+1];
return true;
}
}
}
/*在线性表内下标为i的位置插入元素e*/
template <typename T>
bool List<T>::ListInsert(int i ,T *e)
{
if (i>=0 || i<= m_iLength)
{
for (int j=m_iLength;j>i;j--)
{
m_pList[j] = m_pList[j-1];
}
m_pList[i] = *e;
m_iLength++;
return true;
}
else
{
return false;
}
}
/*删除线性表内的下标为i的元素*/
template <typename T>
bool List<T>::ListDelete(int i ,T *e)
{
if (i>=0 || i<m_iLength)
{
*e = m_pList[i];
for (;i<=m_iLength;i++)
{
m_pList[i] = m_pList[i+1];
}
m_iLength--;
return true;
}
else
{
return false;
}
}
/*遍历线性表内的所有元素并且打印出来*/
template <typename T>
void List<T>::ListTraversal()
{
if (ListEmpty())
{
cout << "该数据表为空!";
}
else
{
for (int i= 0;i < m_iLength;i++)
{
cout << m_pList[i] << endl;
}
}
}
demo.cpp文件
#include "List.h"
#include "List.cpp"
#include <iostream>
using namespace std;
int main()
{
List<double> *l = new List<double>(10);
double e = 3.2;
l->ListInsert(0,&e);
double e1 = 4.3;
l->ListInsert(1,&e1);
double e2 = 5.4;
l->ListInsert(2,&e2);
double e3 = 6.5;
l->ListInsert(3,&e3);
double e11 = 7.6;
l->ListInsert(4,&e11);
cout << "顺序线性表的遍历结果如下:" << endl;
l->ListTraversal();
cout <<endl;
cout << "判断该线性表是否为空:";
if (l->ListEmpty())
{
cout << "该线性表为空!" << endl;
}
else
{
cout << "该线性表不为空!" << endl;
}
cout <<endl;
cout <<"数据表长度为:" << l->ListLength() << endl;
cout <<endl;
double e4 = 0;
l->ListDelete(2,&e4); //删除位序为2的元素,并将该元素返回给e4.
cout << "删除的元素为:" << e4 << endl;
cout <<endl;
double e5 = 1.0;
l->GetElement(2,&e5);
cout << "下标为2的元素为:" << e5 << endl;
cout <<endl;
double e6 = 4.3;
cout << "值为4.3的元素的位序为:" << l->LocateElement(&e6) << endl;
cout <<endl;
double e7 = 4.3;
double e8 = 0;
l->PriorElement(&e7,&e8);
cout << "元素4.3的前驱元素为:" << e8<< endl;
cout <<endl;
double e9 = 4.3;
double e10=0;
l->NextElementa(&e9,&e10);
cout << "元素4.3的后继元素为:" << e10<< endl;
cout <<endl;
cout << "数据表的遍历结果如下:" << endl;
l->ListTraversal();
cout <<"数据表长度为:" << l->ListLength() << endl;
cout <<endl;
l->ClearList();
l->ListTraversal();
delete []l;
l = NULL;
system("pause");
return 0;
}