C++数据结构——顺序表(基本代码实现与案例)

基本代码实现

#include<iostream>
using namespace std;

enum error_code{
	success, range_error, overflow, underflow
};

class list{
	public:
		list(int len);
		int length()const; // 获得顺序表长度 
		error_code get_element(const int i, int& x)const; // 按序号取元素 
		int locate(const int x)const; // 按值查找元素 
		error_code insert(const int i, const int x); // 插入元素 
		error_code delete_element(const int i); // 删除元素 
	private:
		int count, maxlen;
		int* data;
};

list::list(int len){
	count = 0;
	maxlen = len;
	data = new int[maxlen];
}

int list::length()const{
	return count;
}

error_code list::get_element(const int i, int& x)const{
	if(i <= 0 || i > count) return range_error;
	x= data[i - 1];
	return success;
}

int list::locate(const int x)const{
	for(int i = 0; i < length(); i++)
	{
		if(data[i] == x)
		    return (i + 1);
	}
	return -1;
}

error_code list::insert(const int i, const int x){
	if(count == maxlen) return overflow;
	if(i < 1 || i > length() + 1) return range_error;
	for(int j = count - 1; j >= i - 1; j--)
	    data[j + 1] = data[j];
	data[i - 1] = x;
	count++;
	return success;
}

error_code list::delete_element(const int i){
	if(length() == 0) return underflow;
	if(i < 1 || i > length() + 1) return range_error;
	for(int j = i; j < length(); j++)
	    data[j - 1] = data[j];
	count--;
	return success;
}

bool ReferenceError(error_code a){
	if(a == range_error){
		cout << "range_error!!" << endl;
		return false;
	}
	if(a == underflow){
		cout << "underflow" << endl;
		return false;
	}
	if(a == overflow){
		cout << "overflow!!" << endl;
		return false;
	}
	return true;
}

int main()
{
	list Li(11);
	int d;
	
	for(int i = 0; i < 10; i++) // 插入值 
	    ReferenceError(Li.insert(i + 1, i));
	ReferenceError(Li.delete_element(5)); // 删除值
	cout << Li.length() << endl; // 获得长度
	ReferenceError(Li.get_element(7, d)); // 按照索引获得值 
	cout << d << endl; 
	cout << Li.locate(9) << endl; // 按照值获得元素索引 
	
	return 0;
}

顺序表案例

  • 案例一:从一个有序顺序表中删除所有其值重复的元素,使表中所有元素的值均不同。
  • 案例二:将两个递增的有序顺序表合并成一个新的递增有序顺序表。
  • 案例三:已知在一维数组A[m+n]中一次存放着两个线性表(a1,a2,a3,…,am)和(b1,b2,b3,…,bn)。试编写一个函数,将数组中两个顺序表的位置互换,即将(b1,b2,b3,…,bn)放在(a1,a2,a3,…,am)的前面。
#include<iostream>
using namespace std;

enum error_code{
	success, range_error, overflow, underflow
};

class list{
	public:
		list(int len);
		int length()const;
		error_code get_element(const int i, int& x)const;
		int locate(const int x)const; 
		error_code insert(const int i, const int x);  
		error_code delete_element(const int i);  
	private:
		int count, maxlen;
		int* data;
};

list::list(int len){
	count = 0;
	maxlen = len;
	data = new int[maxlen];
}

int list::length()const{
	return count;
}

error_code list::get_element(const int i, int& x)const{
	if(i <= 0 || i > count) return range_error;
	x= data[i - 1];
	return success;
}

int list::locate(const int x)const{
	for(int i = 0; i < length(); i++)
	{
		if(data[i] == x)
		    return (i + 1);
	}
	return -1;
}

error_code list::insert(const int i, const int x){
	if(count == maxlen) return overflow;
	if(i < 1 || i > length() + 1) return range_error;
	for(int j = count - 1; j >= i - 1; j--)
	    data[j + 1] = data[j];
	data[i - 1] = x;
	count++;
	return success;
}

error_code list::delete_element(const int i){
	if(length() == 0) return underflow;
	if(i < 1 || i > length() + 1) return range_error;
	for(int j = i; j < length(); j++)
	    data[j - 1] = data[j];
	count--;
	return success;
}

bool ReferenceError(error_code a){
	if(a == range_error){
		cout << "range_error!!" << endl;
		return false;
	}
	if(a == underflow){
		cout << "underflow" << endl;
		return false;
	}
	if(a == overflow){
		cout << "overflow!!" << endl;
		return false;
	}
	return true;
}

// 去除重复值
void repeat()
{
	cout << "去重问题!!" << endl;
	// 初始化题目条件 
	list Li(30);
	int d1, d2;
	for(int i = 1; i <= 10; i++)
	    ReferenceError(Li.insert(i, i));
    for(int i = 10; i < 15; i++)
	    ReferenceError(Li.insert(i + 1, i));
	cout << "去重前个数:" << Li.length() << endl;
	
	// 开始操作
	for(int i = 1; i < Li.length(); i++)
	{
		l:
		ReferenceError(Li.get_element(i, d1));
		ReferenceError(Li.get_element(i + 1, d2));
		if(d1 == d2)
		{
			ReferenceError(Li.delete_element(i + 1));
			goto l;
		}
	}
	cout << "去重后个数:" << Li.length() << endl;
}
	
// 两表合并
void merge()
{
	cout << "两表合并问题!!" << endl;
	// 初始化题目条件
	list La(20), Lb(20), Lc(50);
	int x, y;
	int ia = 1, ib = 1, ic = 1;
	for(int i = 1; i <= 10; i++)
	{
	    ReferenceError(La.insert(i, i));
		ReferenceError(Lb.insert(i, i + 5));	
	}
	
	// 开始操作
	while(ia < La.length() && ib < Lb.length())
	{
		ReferenceError(La.get_element(ia, x));
		ReferenceError(Lb.get_element(ib, y));
		if(x == y)
		{
			Lc.insert(ic, x);
			ic++;
			ia++;
			Lc.insert(ic, y);
			ic++;
			ib++;	
		}
		if(x > y)
		{
			Lc.insert(ic, y);
			ic++;
			ib++;
		}
		if(x < y)
		{
			Lc.insert(ic, x);
			ic++;
			ia++;
		}
	}
	while(ia < La.length())
	{
		La.get_element(ia, x);
		Lc.insert(ic, x);
		ic++;
		ia++;
	}
	while(ib < Lb.length())
	{
		Lb.get_element(ib, x);
		Lc.insert(ic, x);
		ic++;
		ib++;
	}
	
	// 展示结果
	for(int i = 1; i <= Lc.length(); i++)
	{
		ReferenceError(Lc.get_element(i, x));
		cout << x << "  ";
	} 
}
	
// 两表互换 
void Reverse(list& a, int end, int begin = 1) // 逆置算法 
{
	int x, y;
	for(int i = 0; i < (end - begin) * 1.0 / 2; i++)
	{
	    ReferenceError(a.get_element(begin + i, x));
	    ReferenceError(a.get_element(end - i, y));
		ReferenceError(a.delete_element(begin + i));
		ReferenceError(a.insert(begin + i, y));
		ReferenceError(a.delete_element(end - i));
		ReferenceError(a.insert(end - i, x));		
	}
}
void exchange()
{
	cout << "两表互换问题!!" << endl;
	// 初始化题目条件
	int m, n, d;
	cin >> m >> n;
	list A(m + n + 10);
	for(int i = 1; i <= m + n; i++)
	    ReferenceError(A.insert(i, i));

	// 调用逆置算法 
	Reverse(A, m + n); // 整体逆置 
	Reverse(A, m + n, n + 1); // m 逆置 
	Reverse(A, n); // n 逆置
	
	// 结果展示
	for(int i = 1; i <= A.length(); i++)
	{
	    ReferenceError(A.get_element(i, d));
		cout << d << "  "; 	
	}  
}

int main()
{
	// 去除重复值
	repeat();
	
	// 两表合并
	merge();
	
	// 两表互换 
	exchange();
	
	return 0;
}

更多内容大家可以前往我的个人博客浏览:eyes++的个人空间

  • 8
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值