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

C++实现线性表之顺序表

2017-02-06 23:03 471 查看

C++实现线性表之顺序表

List.h

#ifndef LIST_H
#define LIST_H
#include <iostream>

using namespace std;

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 ¤tElem, int &preElem);     //获取指定元素的前驱
bool NextElem(int ¤tElem, int &nextElem);     //获取指定元素的后继
bool ListInsert(int n, int &e);                     //在第n个位置上插入元素
bool ListDelete(int n, int &e);                     //删除第n个位置上的元素
void ListTraverse();                                //便利线性表

private:
int *m_pList;
int m_iSize;
int m_iLength;
};

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 true;
}
else
{
return false;
}

//return m_iLength == 0 ? true : false;
}

int List::ListLength()
{
return m_iLength;
}

bool List::GetElem(int i, int &e)
{
if (i < 0 || i >= m_iSize)
{
return false;
}
else
{
e = m_pList[i];
return true;
}
}

int List::LocateElem(int &e)
{
for (size_t i = 0; i < m_iLength; i++)
{
if (e == m_pList[i])
{
return i;
}

}
return -1;
}

bool List::PriorElem(int ¤tElem, int &preElem)
{
int temp = LocateElem(currentElem);
if (temp != -1)
{
if (temp != 0)
{
preElem = m_pList[temp - 1];
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}

bool List::NextElem(int ¤tElem, int &nextElem)
{
int temp = LocateElem(currentElem);
if (temp != -1)
{
if (temp != m_iLength - 1)
{
nextElem = m_pList[temp + 1];
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}

void List::ListTraverse()
{
for (size_t i = 0; i < m_iLength; i++)
{
cout << m_pList[i] << endl;
}
}

bool List::ListInsert(int n, int &e)
{
if (n < 0 || n > m_iLength)
{
return false;
}
else
{
for (int i = m_iLength - 1; i >= n ; i--)
{
m_pList[i + 1] = m_pList[i];
}
m_pList
= e;
m_iLength++;
return true;
}

}

bool List::ListDelete(int n, int &e)
{
if (n < 0 || n >= m_iLength)
{
return false;
}
else
{
e = m_pList
;
for (size_t i = n + 1; i < m_iLength; i++)
{
m_pList[i - 1] = m_pList[i];
}

m_iLength--;
return true;
}
}

#endif // !LIST_H


main.cpp

#include "List.h"

int main()
{
List *list1 = new List(10);

int e = 1;
int a = 2;
int b = 3;
int c = 4;
int d = 5;
int f = 6;
int g = 7;
int h = 0;

list1->ListInsert(0, e);
list1->ListInsert(1, a);
list1->ListInsert(2, b);
list1->ListInsert(3, c);
list1->ListInsert(4, d);
list1->ListInsert(5, f);
list1->ListInsert(3, g);

//list1->ClearList();

list1->ListTraverse();

list1->GetElem(2, h);

list1->NextElem(d, h);

cout << "$" << h << " "<<list1->ListLength() << endl;
delete list1;
list1 = NULL;

system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据结构 线性表 C++