您的位置:首页 > 编程语言 > C语言/C++

C++实现顺序表的基本操作

2017-06-20 18:19 495 查看
头文件:

#ifndef List_H
#define List_H

typedef int Elem;
class List{
public:
List(int size);             //初始化顺序表
~List();                    //销毁表,析构函数
bool isEmpty();             //是否为空
int listLength();           //表中元素个数
void clearList();           //清空表
bool getElem(int i,Elem *e);     //
int locateElem(Elem *e);         //返回指定元素的位置
bool priorElem(Elem *currentElem,Elem *preElem);        //找前驱
bool nextElem(Elem *currentElem,Elem *nextElem);        //找后继
void listTraverse();         //遍历顺序表
bool listInsert(int i,Elem *e);     //在指定位置插入元素
bool listDelete(int i,Elem *e);     //删除指定位置的元素
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::isEmpty(){
return m_iLength==0?true:false;
}
int List::listLength(){
return m_iLength;
}
bool List::getElem(int i,Elem *e){
if (i<0||i>=m_iSize)
{
return false;
}else{
*e=m_pList[i];
return true;
}
}
int List::locateElem(Elem *e){
for (int i=0;i<m_iLength;i++)
{
if (m_pList[i]==*e)
{
return i;
}
}
return -1;
}
bool List::priorElem(Elem *currentElem,Elem *preElem){
int curLoc=locateElem(currentElem);
if (curLoc==-1)
{
return false;
}else if(curLoc==0){
return false;
}else{
*preElem=m_pList[curLoc-1];
return true;
}
}
bool List::nextElem(Elem *currentElem,Elem *nextElem){
int curLoc=locateElem(currentElem);
if (curLoc==-1)
{
return false;
}else if(curLoc==m_iLength-1){
return false;
}else{
*nextElem=m_pList[curLoc+1];
return true;
}
}
void List::listTraverse(){
for (int i=0;i<m_iLength;i++)
{
cout<<m_pList[i]<<" ";
}
}
bool List::listInsert(int i,Elem *e){
if (i<0||i>m_iLength)
{
return false;
}else{
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,Elem *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 "List.h"

using namespace std;
int main(){
List *p=new List(7);
int e1=5;
int e2=9;
int e3=4;
int e4=6;
int e5=8;
int e6=7;
int e7=3;
if(p->isEmpty())
cout<<"Empty"<<endl;
p->listInsert(0,&e1);
p->listInsert(1,&e2);
p->listInsert(2,&e3);
p->listInsert(3,&e4);
p->listInsert(4,&e5);
p->listInsert(5,&e6);
p->listInsert(6,&e7);
p->listTraverse();
cout<<endl;
p->locateElem(&e5);
int e=0;
p->priorElem(&e2,&e);
cout<<"第二个元素的前驱"<<e<<endl;
p->nextElem(&e2,&e);
cout<<"后继"<<e<<endl;
p->listDelete(5,&e);
cout<<"删除元素:"<<e<<endl;
p->listTraverse();
cout<<endl;
p->getElem(7,&e);
cout<<"最后位置元素:"<<e<<endl;
e=24;
p->listInsert(4,&e);
p->listTraverse();
cout<<endl;
cout<<"24在:"<<p->locateElem(&e)+1<<endl;
p->clearList();
if(p->isEmpty())
cout<<"Empty"<<endl;

return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息