用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c++代码库

顺序表

2012-10-18 作者: LeiZhen举报

[c++]代码库

# include <iostream>
using namespace std;
const int MaxSize = 10;  // 线性表的最大容量
typedef char ElemType;   // 自定义类型

class SeqList
{
private:
	ElemType* data;
	int length;
public:
	bool InitList()	// 初始化顺序表空间
	{
		data = new ElemType[MaxSize];
		if ( NULL == data )
		{
			cout << "Not Have Enough Memory!\n";
			return false;
		}
		length = 0;
		return true;
	}
	bool CreatList( int site, ElemType e )	// 创建顺序表
	{
		if ( site<1 || site>MaxSize )
		{	// 越界检查
			cout << "Overflow, ";
			return false;
		}
		else
		{
			data[site-1] = e;
			length ++;
			return true;
		}
	}
	void DispList()	// 输出顺序表
	{
		for ( int i=0;i < length;++i )
			cout << data[i] << ' ';
		cout << endl;
	}
	int ListLength() { return length; }		// 求表长
	bool ListEmpty() { return 0 == length; }	// 判断是否为空表
	bool GetElem( int site, ElemType &e )	// 求某位置上的元素
	{
		if ( site<1 || site>length )
			return false;
		else
			e = data[site-1];
		return true;
	}
	bool LocateElem( int& site, ElemType e )	// 求元素的位置
	{
		for ( int i=0;i < length;++i )
		{
			if ( e == data[i] )
			{
				site = i+1;
				return true;
			}
		}
		return false;
	}
	bool ListInsert( int site, ElemType e )		// 插入元素
	{
		if ( site<1 || site>length )
		{
			cout << "Overflow, ";
			return false;
		}
		else
		{
			site --;
			for ( int i=length;i > site;--i )
				data[i] = data[i-1];
			data[site] = e;
			length ++;
			return true; 
		}
	}
	bool ListDelete( int site, ElemType &e )  	// 删除元素
	{
		if ( site<1 || site>length )
		{
			cout << "Overflow, ";
			return false;
		}
		else
		{
			e = data[site--];
			for ( int i=site;i < length;++i )
				data[i] = data[i+1];
			length --;
			return true;
		}
	}
	void DestoryList()	{ delete[] data; }	// 清空顺序表
};

int main (void)
{
	SeqList L;	// 定义顺序表

	cout << ">>>>>>>>> InitList Operation <<<<<<<<\n";
	L.InitList();	// 初始化顺序表
	int temp;
	ElemType e;
	cout << "Input The SeqList Length You Want To Creat : ";
	cin >> temp;
	cout << "Input Element : \n";
	cout << ">>>>>>>> CreatList Operation <<<<<<<\n";
	for ( int i=1;i <= temp;++i )
	{
		printf("No.%d : ", i);
		cin >> e;
		if ( L.CreatList( i, e ) ) 		// 创建顺序表
			printf("Creat No.%d element success!\n", i);
		else
			printf("Creat No.%d element failed!\n", i);
		cin.sync();		// 清除缓冲区
	}
	cout << "Now SeqList is : \n";
	cout << ">>>>>>>>> DispList Operation <<<<<<<<\n";
	L.DispList();	// 输出顺序表

	cout << ">>>>>>>> ListLength Operation <<<<<<<\n";
	printf("SeqList Length Is %d.\n", L.ListLength());	// 输出表长

	cout << ">>>>>>>> ListEmpty Operation <<<<<<<<\n";
	if ( L.ListEmpty() )	// 判断是否是空表
		cout << "SeqList Is Empty!\n";
	else
		cout << "Not a Empty SeqList!\n";

	cout << ">>>>>>>>> GetElem Operation <<<<<<<<<\n";
	cout << "Input Site To Check Element : ";
	cin >> temp;
	if ( L.GetElem( temp, e ) )		// 用 e 返回顺序表中第 temp 个元素
		printf("No.%d Element Is %c\n.", temp, e);
	else
		cout << "Overflow The SeqList!\n";

	cout << ">>>>>>>> LocateElem Operation <<<<<<<<\n";	
	cout << "Input Element To Check Site : ";
	cin >> e;
	if ( L.LocateElem( temp, e ) )		// 输入元素查看元素所在的位置
		printf("%c Exist In No.%d.\n", e, temp);
	else
		printf("Can't Find %c!\n", e);

	cout << ">>>>>>>> ListInsert Operation <<<<<<<<\n";
	cout << "Input Site : ";
	cin >> temp;
	cout << "Input Element : ";
	cin >> e;
	if ( L.ListInsert( temp, e ) )	// 在 temp 位置插入 e
	{
		cout << "Insert Success!\nNow SeqList Is : ";
		L.DispList();
	}
	else
		cout << "Insert Failed!\n";

	cout << ">>>>>>>> ListDelete Operation <<<<<<<<\n";
	cout << "Input Delete Site : ";
	cin >> temp;
	if ( L.ListDelete( temp, e ) )	// 删除 temp 位置的元素
	{
		printf("Delete %c Success!\nNow SeqList Is : ", e);
		L.DispList();
	}
	else
		cout << "Delete Failed!\n";

	L.DestoryList(); 	// 销毁线性表

	return 0;
}

[代码运行效果截图]


顺序表


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...