代码示例:
#ifndef LIST_H
#define LIST_H
class List
{
public:
List(int size);//创建线性表
~List();//销毁线性表
void ClearList();//清空线性表
bool ListEmpty();//判断线性表是否为空
int ListLength();//获取线性表的长度
bool GetElem(int i,int*e);//获取指定元素
int LocateElem(int*e);//寻找第一个满足e的数据元素的位序
bool PriorElem(int*currentElem,int*preElem);//获取指定元素的前驱
bool NextElem(int*currentElem,int*NextElem);//获取指定元素的后驱
bool ListInsert(int i,int*e);//在第i个位置插入元素
bool ListDelete(int i,int*e);//在第i个位置删除元素
void ListTraverse();//遍历线性表
private:
int*m_pList;
int m_iSize;
int m_iLength;
};
#endif
#include"List.h"
#include<iostream>
using namespace std;
List::List (int size)
{
m_iSize=size;
m_pList=new int[m_iSize];
m_iLength=0;
}
List::~List()
{
delete[]m_pList;
m_pList=NULL;
}
void List::ClearList()
{
m_iLength=0;
}
bool List::ListEmpty()
{
if(m_iLength==0)//return m_iLength==0?true:false;
{
return true;
}
return false;
}
int List::ListLength()
{
return m_iLength;
}
bool List::GetElem(int i,int*e)
{
if(i<0||i>=m_iSize)
{
return false;
}
*e=m_pList[i];
return true;
}
int List::LocateElem(int *e)
{
for(int i=0;i<m_iLength;i++)
{
if(m_pList[i]==*e)
{
return i;
}
}
return -1;
}
bool List::PriorElem(int*currentElem,int*preElem)
{
int temp=LocateElem(currentElem);
if(temp==-1)
{
return false;
}
else
{
if(temp==0)
{
return false;
}
else
{
*preElem= m_pList[temp-1];
return true;
}
}
}
bool List::NextElem(int*currentElem,int*NextElem)
{
int temp=LocateElem(currentElem);
if(temp==-1)
{
return false;
}
else
{
if(temp==m_iLength-1)
{
return false;
}
else
{
*NextElem= m_pList[temp+1];
return true;
}
}
}
void List::ListTraverse()
{
for(int i=0;i<m_iLength;i++)
{
cout<<m_pList[i]<<endl;
}
}
bool List::ListInsert(int i,int*e)
{
if(i<0||i>m_iLength)
{
return false;
}
for(int k=m_iLength-1;k>=i;k--)
{
m_pList[k+1]=m_pList[k];
}
m_pList[i]=*e;
m_iLength++;
return true;
}
bool List::ListDelete(int i,int*e)
{
if(i<0||i>=m_iLength)
{
return false;
}
*e= m_pList[i];
for(int k=i+1;k<m_iLength;k++)
{
m_pList[k-1]=m_pList[k];
}
m_iLength--;
return true;
}
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include"List.h"
using namespace std;
/*
线性表——顺序表
3 5 7 2 9 1 8
前驱 后继
BOOL InitList(List**list);//创建线性表
void DestroyList(List*list);//销毁线性表
void ClearList(List*list);//清空线性表
BOOL ListEmpty(List*list);//判断线性表是否为空
int ListLength(List*list);//获取线性表的长度
BOOL GetElem(List*list,int i,Elem*e);//获取指定元素
int LocateElem(List*list,Elem*e);//寻找第一个满足e的数据元素的位序
BOOL PriorElem(List*list,Elem*currentElem,Elem*preElem);//获取指定元素的前驱
BOOL NextElem(List*list,Elem*currentElem,Elem*NextElem);//获取指定元素的后驱
BOOL ListInsert(List*list,int i,Elem*e);//在第i个位置插入元素
BOOL LIstDelete(LIst*list,int i,Elem*e);//在第i个位置删除元素
void ListTraverse(LIst*list);//遍历线性表
*/
int main(void)
{
int e1=3;
int e2=5;
int e3=7;
int e4=2;
int e5=9;
int e6=1;
int e7=8;
int temp=0;
List*list1=new List(10);
cout<<"len="<<list1->ListLength()<<endl;
cout<<endl;
list1->ListInsert(0,&e1);
list1->ListInsert(1,&e2);
list1->ListInsert(2,&e3);
list1->ListInsert(3,&e4);
list1->ListInsert(4,&e5);
list1->ListInsert(5,&e6);
list1->ListInsert(6,&e7);
list1->ListTraverse();
cout<<endl;
cout<<e3<<endl;
list1->PriorElem(&e3,&temp);
cout<<"Ftemp="<<temp<<endl;
cout<<e5<<endl;
list1->PriorElem(&e5,&temp);
cout<<"Ftemp="<<temp<<endl;
cout<<endl;
cout<<e3<<endl;
list1->NextElem(&e3,&temp);
cout<<"Btemp="<<temp<<endl;
cout<<e5<<endl;
list1->NextElem(&e5,&temp);
cout<<"Btemp="<<temp<<endl;
cout<<endl;
list1->GetElem(0,&temp);
cout<<temp<<endl;
cout<<endl;
cout<<list1->LocateElem(&e5)<<endl;
cout<<endl;
cout<<"len="<<list1->ListLength()<<endl;
cout<<endl;
list1->ListInsert(1,&e7);
list1->ListTraverse();
cout<<endl;
list1->ListDelete(0,&temp);
if(list1->ListEmpty())
{
cout<<"empty"<<endl;
}
else
{
cout<<"not empty"<<endl;
}
list1->ClearList();
list1->ListTraverse();
cout<<temp<<endl;
system("pause");
return 0;
}
打印结果: