C++实现顺序表

本篇讲述c++如何实现线性表中的顺序表,即用数组的方式实现。
头文件中的内容如下:

class List
{
public:
	List(int capacity);
	~List();
	int Length();
	void ClearList();                       //清空链表
	bool IsEmpty();
	bool IsFull();
	bool GetElem(int i, int *e);            //获取第i个位置上的元素
	int LocateElem(int ele);
	bool GetPredecessor(int ele, int *e);   //获取目标元素的前驱
	bool GetSuccessor(int ele, int *e);     //获取目标元素的后继
	bool TraverseList();
	bool ListInsert(int loc, int ele);      //当前位置插入元素
	bool ListDelete(int loc, int *e);       //当前位置删除元素
private:
	int *m_pList;
	int m_iCapacity;
	int m_iLen;
};

源文件中的内容如下:

#include <iostream>
#include "List.h"
using namespace std;


List::List(int capacity)
{
	m_iCapacity = capacity;
	m_pList = new int[m_iCapacity];
	m_iLen=0;

}
List::~List()
{
	delete []m_pList;
	m_pList = NULL;
}
int List::Length()
{
	return m_iLen;
}

void List::ClearList()
{
	m_iLen = 0;
}
bool List::IsEmpty()
{
	return m_iLen == 0 ? true : false;
}
bool List::IsFull()
{
	return m_iLen == m_iCapacity ? true : false;
}
bool List::GetElem(int i, int *e)
{
	if (i < 0 || i >= m_iCapacity){
		return false;
	}
	*e = m_pList[i];
	return true;
}
int List::LocateElem(int ele)
{
	for (int i = 0; i < m_iLen; i++){
		if (m_pList[i] == ele){
			return i;
		}
	}
	return -1; //not find
}

bool List::GetPredecessor(int ele, int *e)
{
	int loc = LocateElem(ele);
	if (loc == -1 || loc == 0){
		return false;
	}
	else{
		*e = m_pList[loc-1];
		return true;
	}
}
bool List::GetSuccessor(int ele, int *e)
{
	int loc = LocateElem(ele);
	if (loc == -1 || loc == m_iLen-1){
		return false;
	}else{
		*e = m_pList[loc+1];
		return true;
	}
}
bool List::TraverseList()
{
	if (m_iLen == 0){
		return false;
	}else{
		for (int i = 0; i < m_iLen; i++){
			cout << m_pList[i] << endl;
		}
		return true;
	}
}

bool List::ListInsert(int loc, int ele)
{
	if (IsFull()){
		return false;
	}else{
		if (loc >= 0 && loc <= m_iLen){
			for (int i = m_iLen - 1; i >= loc; i--){
				m_pList[i + 1] = m_pList[i];
			}
			m_pList[loc] = ele;
			m_iLen++;
			return true;
		}else{
			return false;
		}
	}
}

bool List::ListDelete(int loc, int *e)
{
	if (IsEmpty()){
		return false;
	}else{
		if (loc >= 0 && loc <= m_iLen-1){
			*e = m_pList[loc];
			for (int i = loc; i < m_iLen - 1; i++){
				m_pList[i] = m_pList[i + 1];
			}
			m_iLen--;
			return true;
		}else{
			return false;
		}
	}
}

测试程序如下:

#include <iostream>
#include "List.h"
using namespace std;

int main(void)
{
	List *p = new List(5);
	p->ListInsert(0, 3);
	p->ListInsert(1, 5);
	p->ListInsert(2, 7);
	p->ListInsert(3, 9);
	p->ListInsert(4, 11);
	p->ListInsert(5, 13);
	p->TraverseList();
	cout << endl;

	int temp = 0;
	p->ListDelete(2, &temp);
	p->ListDelete(2, &temp);
	p->TraverseList();
	cout << endl;

	delete p;
	p = NULL;
	system("pause");
	return 0;
}
使用c++实现顺序表:多文件编程,层次清晰,函数有注释 SeqList();//构造函数,存储的元素个数设为0 bool setLength(size_t length);//设置已经存储的元素个数 bool addElement(ElemType element);//把某个元素添加到顺序表末尾 bool addElement(ElemType element , size_t n);//插入一个元素,使其成为第n个元素,其余元素后移 bool delElement();//删除所有的元素 bool delElement(size_t n);//删除第n个元素 bool delElement(string elementDetailType,string elementDetail);//通过某个元素细节找到元素,把这个元素删除 bool replaceElement(ElemType element , size_t n);//使用一个元素,替换掉第n个元素 bool swapElement(size_t n1 , size_t n2);//把第n1个元素和第n2个元素交换 ElemType* getElement();//得到数组头的指针 ElemType* getElement(size_t n);//得到第n个元素的指针 size_t getLength();//得到存储的元素个数 size_t getMaxSize();//得到顺序表容量 bool showElementDetail();//输出所有的元素细节 bool showElementDetail(size_t n);//输出第n个元素的细节 bool showElementDetail(string elementDetailType,string elementDetail);//通过某个元素细节找到元素,输出元素所有细节 size_t findElement(string elementDetailType,string elementDetail);//通过某个元素细节找到元素位置 static int inputAInt(int min = 0,int max = 9,int defaultValue = -1);//从键盘读取,限制为一个min到max间的整数,非法情况返回defaultValue void startControlLoop();//打开控制界面 ~SeqList();//析构函数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值