线性表(顺序表)相关操作的c++代码

       分享一段线性表相关操作的程序,线性表的实现非常简单。这里设置了一个栅栏以便一些操作,可自建类型作为元素进行测试。实现的功能主要有:

①生成和清除线性表

②查找

③插入、添加

④删除

⑤当前节点找后继和前驱节点

⑥表长度增减

头文件:

// Alist.h

#define DefaultListSize 10  //const int DefaultListSize = 10;

/************************************************************
基于数组的线性表实现
功能:
    生成和清除线性表
    当前节点找后继和前驱元素
    获取改变值
    长度增减、任何位置插入删除
***************************************************************/

template <class Elem> 
class AList  
{
private:
	int maxSize;        // 线性表最大存放元素数
	int listSize;       // 线性表实际存放元素数
	int fence;          // 栅栏的位置
	Elem* listArray;    // 存放元素的数组(地址)

public:
    AList(int size=DefaultListSize) // 该参数可选
	{ 
		maxSize = size;
		listSize = fence = 0;
		listArray = new Elem[maxSize];
	}
	~AList() { delete [] listArray; } 
	//清除线性表
	void clear() 
	{
		delete [] listArray;
		listSize = fence = 0;
		listArray = new Elem[maxSize];
	}

	//插入:插值在右边(栅栏分界)第一个元素
	bool insert(const Elem&);
	//添加:线性表末尾添加元素
	bool append(const Elem&);
	//删除:删除并得到栅栏右边第一个元素
	bool remove(Elem&);

	void setStart() { fence = 0; }
	void setEnd()   { fence = listSize; }

	//后继和前驱元素
	void prev()     { if (fence != 0) fence--; }
	void next()     { if (fence <= listSize) fence++; }

	//实际线性表中栅栏左侧和右侧元素数
	int leftLength() const  { return fence; }
	int rightLength() const { return listSize - fence; }

	//判断并设置栅栏位置
	bool setPos(int pos) 
	{
		if ((pos >= 0) && (pos <= listSize)) fence = pos;
		return (pos >= 0) && (pos <= listSize);
	}

	//判断并获取栅栏处的值
	bool getValue(Elem& it) const 
	{
		if (rightLength() == 0) 
			return false;
		else 
		{ 
			it = listArray[fence]; 
			return true; 
		}
	}

	void print() const 
	{
		int temp = 0;
		cout << "< ";
		while (temp < fence) cout << listArray[temp++] << " ";
		cout << "| ";
		while (temp<listSize) cout << listArray[temp++] << " ";
		cout << ">\n";
	}
};

template <class Elem> 
bool AList<Elem>::insert(const Elem& item) 
{
	if (listSize == maxSize) return false; //线性表满
	for(int i=listSize; i>fence; i--)      // 栅栏右边的元素向后移,腾出插入的空间
		listArray[i] = listArray[i-1];      
	listArray[fence] = item;
	listSize++;                           // 线性表大小+1
	return true;
}

template <class Elem> 
bool AList<Elem>::append(const Elem& item) 
{
	if (listSize == maxSize) return false;  //线性表满
	listArray[listSize++] = item;
	return true;
}


template <class Elem> bool AList<Elem>::remove(Elem& it) 
{
	if (rightLength() == 0) return false; // 删除元素不存在
	it = listArray[fence];                // 暂存删除元素
	for(int i=fence; i<listSize-1; i++)   // 栅栏右边的元素向前移
		listArray[i] = listArray[i+1];
	listSize--;                           // 线性表大小-1
	return true;
}

实现部分:

// Alist.cpp : 定义控制台应用程序的入口点。

#include "stdafx.h"
#include "Alist.h"
#include "time.h"
#include <iostream>
using namespace std;

//自定义断言功能,val假输出错误信息
void Assert(bool val, char* string) {
	if (!val) 
	{ 
		cout << "Assertion Failed: " << string << endl;
		exit(-1);//不成功的终止
	}
}

//判断奇偶
inline bool EVEN(int x) { return (x % 2) == 0; }
inline bool ODD(int x) { return (x & 1) != 0; }

//产生随机数
inline void Randomize() 
{ srand(1); }
inline int Random(int n)
{ return rand() % (n); }

//交换两个元素
//数组
template<class Elem>
inline void swap(Elem A[], int i, int j) {
	Elem temp = A[i];
	A[i] = A[j];
	A[j] = temp;
}
//引用
template<class Elem>
inline void swap(Elem &e1, Elem &e2) {
	Elem temp = e1;
	e1 = e2;
	e2 = temp;
}

//计时
clock_t tstart = 0;  

void Settime()//系统当前时间
{ 
	tstart = clock(); 
}

double Gettime()//转换为以秒为单位的时间段
{ 
	return (double)((double)clock() - (double)tstart)/(double)CLOCKS_PER_SEC; 
}

//查找整型元素
bool find(AList<int>& L, int k) 
{
	int it;
	for (L.setStart(); L.getValue(it); L.next())
		if (k == it) 
			return true; 
	return false;                
}

int _tmain(int argc, _TCHAR* argv[])
{
	AList<int> L1(15);
	int temp=0;

	L1.setStart();
	L1.append(int(39));
	L1.next();

	L1.append(int(9));
	L1.append(int(5));
	L1.append(int(4));
	L1.append(int(2));
	L1.append(int(1));
	cout << "建线性表 ";
	L1.print();

	L1.insert(int(3));
	cout << "插入元素 ";
	L1.print();

	L1.remove(temp);
	cout << "删除元素 ";
    L1.print();

	L1.setStart();
	if (! find(L1, 3))
		cout << "查找元素:Value 3 not found.\n";
	else cout << "查找元素:Found 3" << endl;

	system("pause");
	return 0;
}

运行结果:


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值