C++线性表的基本操作

C++线性表的基本操作:
代码如下:

 #include
#include
using namespace std;
typedef int Elem;
#define  LIST_INST_SIZE 8
class List 
{
public:
	List(int size);//构造函数
	~List();//析构函数,销毁
	void ClearList();//清空
	bool ListEmpty();//判空
	int ListLength();//返回线性表的长度
	bool GetElem(int i, Elem *e);//得到指定位置的元素
	int LocateElem(Elem *e);//寻找第一个满足e的数据元素的位序
	bool PriorElem(Elem *currentElem, Elem *preElem);//求一个元素的前驱
	bool NextElem(Elem *currentElem, Elem *nextElem);//求一个元素的后继
	void ListTraverse();//线性表遍历
	bool ListInsert(int i, Elem *e);//在第i个位置插入元素
	bool ListDelete(int i, Elem *e);//删除第i个位置的元素
	void ListScan();//输入线性表元素
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, Elem *e)
{
	if (i < 0 || i >= m_iSize)
	{
		return false;
	}
	*e = m_pList[i];
	return true;
}
int List::LocateElem(Elem *e)
{
	int i;
	for (i = 0; i < m_iLength; i++)
	{
		if (m_pList[i] == *e)
		{
			return i;
		}     
	}
	return -1;
}
bool List::PriorElem(Elem *currentElem, Elem *preElem)
{
	int temp=LocateElem(currentElem);
	if (temp > 0 && temp < m_iLength)
	{
		*preElem = m_pList[temp - 1];
		return true;
	}
	else
		return false;
}
bool List::NextElem(Elem *currentElem, Elem *nextElem)
{
	int temp = LocateElem(currentElem);
	if (temp >= 0 && temp < m_iLength - 1)
	{
		*nextElem = m_pList[temp + 1];
		return true;
	}
	else
		return false;
}
void List::ListTraverse()
{
	for (int i = 0; i < m_iLength; i++)
	{
		cout << m_pList[i] << " ";
	}
	cout << endl;
}
bool List::ListInsert(int i, Elem *e)
{
	if (i < 0 || i >m_iLength)//i等于m_iLength时,相当于在线性表结尾处插入元素,不会走下面的For循环
	{
		return false;
	}
	for (int k = m_iLength-1; k >=i ; k--)
	{
		m_pList[k+1] = m_pList[k];//从线性表的最后一个元素往后移,若从i的位置往后移,会覆盖掉i+1位置的元素,所以一定要从最后一个元素往后移
	}
	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; k < m_iLength-1; k++)
	{
		m_pList[k] = m_pList[k + 1];
	}
	m_iLength--;
	return true;
}
void List::ListScan()
{
	cout << "please enter numbers:" << endl;
	for (int i = 0; i < LIST_INST_SIZE; i++)
	{
		cin >> m_pList[i];
		m_iLength++;
	}
}
 
int main()
{
	int e1=7;
	int e2,e3,e4,e5;
	List *list1 = new List(10);//调用构造函数,用堆实例化对象,分配内存为10
	list1->ListScan();//调用输入函数
	cout << "得到下标为3位置的元素:" << endl;
	list1->GetElem(3, &e3);
	cout << e3 << endl;
	cout << "得到e3元素的前驱:" << endl;
	list1->PriorElem(&e3, &e4);
	cout << e4 << endl;
	cout << "得到e3元素的后继:" << endl;
	list1->NextElem(&e3, &e5);
	cout << e5 << endl;
	cout << "在下标为2处插入e1:" << endl;
	list1->ListInsert(2,&e1);//调用插入函数
	list1->ListTraverse();
	cout << "删除下标为6的元素:" << endl;
	list1->ListDelete(6, &e2);//调用删除函数
	list1->ListTraverse();
	cout << e2 << endl;
	delete list1;//调用析构函数
	system("pause");
	return 0;
}

结果截图:
C++线性表的基本操作_第1张图片

你可能感兴趣的:(C++数据结构)