数据结构-线性表

线性表的性质是数组
头文件

#define max 100
namespace seq {
 class seqlist
	{
	public:
		seqlist init(seqlist &);//创建
		seqlist watch(seqlist );//查看
		int increase(seqlist &,int,char);//增加
		void removelocate(seqlist &, int);//移除相应坐标值
		int removekey(seqlist &, char);//移除相应键值
		void removerepat(seqlist &);//去重
		void querylocate(seqlist, int);//按坐标查询
		void querykey(seqlist, char);//按值查询
		void sort(seqlist &);//冒泡排序
		void  countrepat(seqlist l);//统计重复
		seqlist combine(seqlist, seqlist);//合并

	private:
		char list[max];
		int last = 0;
	};	
}

cpp

using namespace seq;
seqlist seqlist::init(seqlist &l) {
	cout << "请输入线性表的值" << endl;
	int i = 0; 
	while (l.last<=max-1)
	{
		char a = getchar();
		if (a == '\n')
			break;
		else
			l.list[i] = a;
			i++;
			l.last++;		
	}
	l.watch(l);
	return l;
};
seqlist seqlist::watch(seqlist l) {
	cout << "查看线性表的值" << endl;
	for (int i = 0; i < l.last; i++)
	{
		cout << l.list[i];
	}
	cout << "\n" << endl;
	return l;
};
int  seqlist::increase(seqlist &l, int x, char y) {
	if (l.last == max)
	{
		cout << "表满了" << endl;
		return 0;
	}
	cout << "请输入插入线性表的位置" << endl;
	loop:cin >> x;
	if (x > l.last || x < 1)
	{	cout << "插入位置不对,请重新插入" << endl;
	goto loop;
    }
    else {
		cout << "请输入插入线性表的值" << endl;
		for (int i=l.last-1;i>=x-1;i--)
		{
			l.list[i+1] = l.list[i];
		}
		l.watch(l);
		cin >> y;
		l.list[x - 1] = y;
		l.last++;
	}
	cout << "现在的值是" << endl;
	l.watch(l);
	return 1;
}
void seqlist::removelocate(seqlist &l, int x) {
	cout << "请输入删除线性表的位置" << endl;
    loop:cin >> x;
	if (x > max || x < 1)
	{
		cout << "删除位置不对,请重新插入" << endl;
		goto loop;
	}
	else {
		cout << "要删除的值是" << l.list[x-1] << "现在将它删除" << endl;
		for (int i =x;i<= l.last - 1; i++)
		{
			l.list[i-1] = l.list[i];
		}
		l.last--;
	}
	l.watch(l);	
}
int seqlist::removekey(seqlist &l, char y) {
	cout << "请输入删除线性表的值" << endl;
	loop :cin >> y;
	int count = 0;
	for (int i = 0; i <= l.last - 1; i++)
	{
		if (l.list[i] == y)
		{
			cout << "要删除的值是" << l.list[i] << "现在将它删除" << i<<endl;
			for (int j = i; j < l.last - 1; j++)
			{
				l.list[j ] = l.list[j+1];
			}
			l.last--;
		}
		else
		{
			count++;
		}
		if (count ==l.last)
		{
			cout << "该值不存在!" << endl;
			return 0;
		}	
	}
	l.watch(l);
	return 1;
}
void seqlist::removerepat(seqlist &l) {
	cout << "开始去重" << endl;
	for (int i = 0; i < l.last; i++)
	{
		for (int j = i + 1; j < l.last; j++)
		{
			if (l.list[j] == l.list[i])
			{
				for (int k = j + 1; k < l.last-1; k++)
				{
					l.list[k - 1] = l.list[k];
				}
				l.last--;
				j--;
			}
		}
	}
	l.watch(l);
}
void seqlist::querylocate(seqlist l, int x) {
	cout << "请输入查询线性表的位置" << endl;
	loop:cin >> x;
	if (x>l.last||x<1)
	{
		cout << "输入错误,请重新输入" << endl;
		goto loop;
	}
	else
		cout << "它的位置是" << endl;
		cout << l.list[x] << endl;
		l.watch(l);
}
void seqlist::querykey(seqlist l, char y) {
	cout << "请输入查询线性表的值" << endl;
    loop:cin >> y;
	int count = 0;
	for (int i = 0; i < l.last-1; i++)
	{
		if (l.list[i] == y)
		{
			cout << "值的坐标是" <<i<< endl;
		}
		else{
			count++;
		}
		if (count == l.last)
		{
			cout << "该值不存在!" << endl;
			goto loop;
		}
	}
}
void seqlist::sort(seqlist&l) {
	cout << "现在开始排序线性表" << endl;
	for (int i = 0; i < l.last-1; i++)
	{
		for (int j = 0; j < l.last-1; j++)
		{
			if (l.list[j] > l.list[j+1])
			{
				char x;
				x = l.list[j];
				l.list[j]= l.list[j+1];
				l.list[j+1] = x;
			}
		}
	}
	l.watch(l);
}
void seqlist::countrepat(seqlist l) {
;
	int count[256] = {};//对应256个ascii
	for (int i = 0; i < l.last; i++)
		count[(int)l.list[i]]++;//把出现的每个asc统计
	for (int i = 0; i < 256; i++)
	{
		if (count[i] != 0)
			cout << (char)i << ':' << count[i] << endl;
	}
}
seqlist seqlist::combine(seqlist n, seqlist m) {
	cout << "现在开始合并线性表" << endl;
	seqlist *l=new seqlist;
	l->last = n.last + m.last;
	
	for (int i = 0; i < n.last; i++)
	{
			l->list[i] = n.list[i];
		}
	for (int i = 0; i <m.last; i++)
	{
			l->list[i+n.last] = m.list[i];
	}
	watch(*l);
	return *l;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值