数据结构》严奶奶版本---线性表(2)顺序表 完整源码

数据结构—线性表(2)顺序表 完整源码

//顺序表
#include 
#include 
#include 

using namespace std;

//初始化储存的元素个数
#define INIT_SIZE 10
//增长因子 每次增长为原来的两倍
#define ADD_TWICE 2  

//存储的元素类型
typedef int ElemType;

//顺序表的存储结构
typedef struct sqlist{

	ElemType* data;
	int max_size;//表中最多存储元素个数
	int now_size;//当前表中存储元素个数

}Sqlist;


bool init_sqlist(Sqlist* &sq);//初始化顺序表
bool delete_sqlist(Sqlist* &sq);//销毁顺序表
bool judge_fulled(Sqlist* &sq);//判断顺序表是否达到储存上限
bool judge_empty(Sqlist* &sq);//判断顺序表是否为空
bool sq_growth(Sqlist* &sq);//增大储存
bool add_data(Sqlist* &sq,ElemType data);//尾插法插入数据
bool add_datas(Sqlist* &sq);//尾插多个数据
bool sq_show(Sqlist* &sq);//遍历顺序表
int  sq_length(Sqlist* &sq);//得到当前表长
bool get_data(Sqlist* &sq,int addr);//输出特定位置上的元素
bool get_addr(Sqlist* &sq,ElemType data);//输出特定元素的位置
bool insert_data(Sqlist* &sq,ElemType data,int addr);//在特定的位置上插入数据
bool delete_data(Sqlist* &sq,int addr);//删除特定位置上的元素
bool union_sq(Sqlist* &sq1,Sqlist* &sq2);//实现两个集合的并集
bool merge_sq(Sqlist* &sq1,Sqlist* &sq2);//归并两个非递减有序的顺序表
bool sort_sq(Sqlist* &sq);//非递减排序

void show();
void switch_channel(int channel);

Sqlist s;
Sqlist* sq = &s; 

int main()
{
	int channel;

	do{
		show();
		cin>>channel;
		system("cls");
		switch_channel(channel);
		system("pause");
		
	}while(1);

	return 0;
}

void show()
{
	cout<<endl<<endl<<"\t顺序表基础操作\n";
	cout<<"\t1--新建顺序表\n";
	cout<<"\t2--表尾插入数据\n";
	cout<<"\t3--遍历显示顺序表\n";
	cout<<"\t4--当前表长\n";
	cout<<"\t5--输出特定位置上的元素\n";
	cout<<"\t6--输出特定元素的位置\n";
	cout<<"\t7--在特定的位置上插入数据\n";
	cout<<"\t8--删除特定位置上的元素\n";
	cout<<"\t9--实现两个集合的并集\n";
	cout<<"\t10--归并两个非递减有序的顺序表\n";
	cout<<"\t11--非递减排序\n";
	cout<<"\t12--销毁顺序表\n";

}

void switch_channel(int channel)
{
	switch(channel)
	{
	case 1:
		{
			init_sqlist(sq);
		}break;
	case 2:
		{
			add_datas(sq);
		}break;
	case 3:
		{
			sq_show(sq);
		}break;
	case 4:
		{
			cout<<"当前表长:"<<sq_length(sq)<<endl;			
		}break;	
	case 5:
		{
			int addr;
			cout<<"请输入查询的元素位置:";
			cin>>addr;
			get_data(sq,addr);	
		}break;
	case 6:
		{
			int data;
			cout<<"请输入查询的元素:";
			cin>>data;
			get_addr(sq,data);	
		}break;
	case 7:
		{
			int addr;
			int data;
			cout<<"请输入插入的元素:";
			cin>>data;
			cout<<"请输入插入元素的位置:";
			cin>>addr;
			insert_data(sq,data,addr);	
		}break;
	case 8:
		{
			int addr;
			cout<<"请输入删除的元素位置:";
			cin>>addr;
			delete_data(sq,addr);	
		}break;
	case 9:
		{	
			Sqlist s1,s2;
			Sqlist* sq1 = &s1;
			Sqlist* sq2 = &s2;
			init_sqlist(sq1);
			init_sqlist(sq2);
			add_datas(sq1);
			add_datas(sq1);
			union_sq(sq1,sq2);

		}break;
	case 10:
		{
			Sqlist s1,s2;
			Sqlist* sq1 = &s1;
			Sqlist* sq2 = &s2;
			init_sqlist(sq1);
			init_sqlist(sq2);
			add_datas(sq1);
			add_datas(sq2);	
			sort_sq(sq1);
			sort_sq(sq2);

			merge_sq(sq1,sq2);
		}break;
	case 11:
		{
			sort_sq(sq);
			sq_show(sq);
		}break;
	case 12:
		{
			delete_sqlist(sq);	
		}break;
	
	}


}

//初始化顺序表
bool init_sqlist(Sqlist* &sq)
{
	sq->data = (ElemType *)malloc(sizeof(ElemType)*INIT_SIZE);
	if(sq->data == NULL)
	{
		cout<<"开辟顺序表数据存储空间失败\n";
		exit(-1);	
	}

	sq->max_size = INIT_SIZE;
	sq->now_size = 0;
	
	return true;
}

bool delete_sqlist(Sqlist* &sq)
{
	if(sq == NULL)
	{
		cout<<"当前表不存在\n";
		return false;		
	}

	if(judge_empty(sq) == true)
	{
		cout<<"当前表内为空\n";
		return false;
	}

	free(sq->data);

	if(sq->data == NULL)
	{
		cout<<"已销毁"<<endl;
		return true;
	}
	
	return true;

}


//判断顺序表是否达到储存上限
bool judge_fulled(Sqlist* &sq)
{
	if(sq->max_size == sq->now_size)
		return true;
	
	return false;
}

//判断顺序表是否为空
bool judge_empty(Sqlist* &sq)
{
	if(sq->now_size == 0)
		return true;

	return false;

}

//增大储存
bool sq_growth(Sqlist* &sq)
{
	sq->data = (ElemType *)realloc(sq->data,sizeof(ElemType)*sq->max_size*ADD_TWICE);
	if(sq->data == NULL)
	{
		cout<<"开辟顺序表数据存储空间失败\n";
		exit(-1);	
	}
	
	sq->max_size = sq->max_size * ADD_TWICE;
	
	return true;
}

//尾插法插入数据
bool add_data(Sqlist* &sq,ElemType data)
{
	//存储已满,扩大存储空间
	if(judge_fulled(sq) == true)
		sq_growth(sq);

	sq->data[sq->now_size] = data;
	sq->now_size += 1;

	return true;
}

//尾插多个数据
bool add_datas(Sqlist* &sq)
{
	int count = 0;
	int i;
	ElemType temp;

	cout<<"请输入要插入元素的个数:";
	scanf("%d",&count);
	printf("\n");

	for(i = 0; i < count; i++)
	{
		cout<<"请输入要插入的第"<<i+1<<"个元素:";
		cin>>temp;
		add_data(sq,temp);	
	}
	
	cout<<"\n插入完成\n";
	return true;

}

//遍历顺序表
bool sq_show(Sqlist* &sq)
{
	if(sq == NULL)
	{
		cout<<"当前表不存在\n";
		return false;		
	}

	if(judge_empty(sq) == true)
	{
		cout<<"当前表内为空\n";
		return false;
	}
	
	int i;
	cout<<"表内元素为:";
	for(i = 0; i< sq->now_size; i++)
		cout<<sq->data[i]<<" ";
	printf("\n");

	return true;

}
//得到当前表长
int sq_length(Sqlist* &sq)
{
	if(sq == NULL)
	{
		cout<<"当前表不存在\n";
		return false;		
	}
	
	return sq->now_size;

}
//输出特定位置上的元素
bool get_data(Sqlist* &sq,int addr)
{
	addr -= 1;
	
	if(addr < 0 || addr >= sq->now_size)
	{
		cout<<"访问位置不合法\n";
		return false;
	}

	cout<<"此位置上的元素为:"<<sq->data[addr]<<endl;
	return true;
}

//输出特定元素的位置
bool get_addr(Sqlist* &sq,ElemType data)
{
	int i = 0;
	for(i = 0; i < sq->now_size && sq->data[i] != data; i++);
	
	if(i >= sq->now_size) 
	{
		cout<<"此元素不存在于表中\n";
		return false;	
	}

	cout<<"此元素在表中处于第"<<i+1<<"个元素"<<endl;
	return true;


}
//在特定的位置上插入数据
bool insert_data(Sqlist* &sq,ElemType data,int addr)
{
	addr -= 1;
	
	if(addr < 0 || addr >= sq->now_size)
	{
		cout<<"此位置不合法\n";
		return false;
	}
	
	sq->now_size += 1;

	//存储已满,扩大存储空间
	if(judge_fulled(sq) == true)
		sq_growth(sq);

	int i;
	for(i = sq->now_size - 1; i > addr; i--)
		sq->data[i] = sq->data[i-1];
	
	sq->data[addr] = data;

	return true;
	
}
//删除特定位置上的元素
bool delete_data(Sqlist* &sq,int addr)
{
	addr -= 1;
	
	if(addr < 0 || addr >= sq->now_size)
	{
		cout<<"此位置不合法\n";
		return false;
	}
	
	int i;

	for(i = addr; i < sq->now_size; i++)
		sq->data[i] = sq->data[i+1];

	sq->now_size -= 1;

	return true;
}

//实现两个集合的并集
bool union_sq(Sqlist* &sq1,Sqlist* &sq2)
{
	int len1 = sq1->now_size;
	int len2 = sq2->now_size;
	int i,j;
	
	cout<<"集合1:\n";
	sq_show(sq1);
	cout<<"集合2:\n";
	sq_show(sq2);

	for(i = 0; i < len2; i++)
	{
		for(j = 0; j < len1; j++)
		{
			if(sq1->data[j] == sq2->data[i])
				break;
		}

		if(j == len1)
		{
			add_data(sq1,sq2->data[i]);
		}
	}
	
	cout<<"并集:\n";
	sq_show(sq1);
	
	return true;
}

//非递减排序  冒泡排序
bool sort_sq(Sqlist* &sq)
{
	int i,j;
	int temp;

	for(i = 0; i < sq->now_size; i++)
	{
		for(j = 0; j < sq->now_size - i - 1; j++)
		{
			if(sq->data[j] > sq->data[j+1])
			{
				temp = sq->data[j];
				sq->data[j] = sq->data[j+1];
				sq->data[j+1] = temp;
			}
		}
	
	}
	return true;
}

//归并两个非递减有序的顺序表
bool merge_sq(Sqlist* &sq1,Sqlist* &sq2)
{
	sort_sq(sq1);
	sort_sq(sq2);
	
	Sqlist s3;
	Sqlist* sq3 = &s3;
	init_sqlist(sq3);
	
	int len1 = sq_length(sq1);
	int len2 = sq_length(sq2);
	int i=0,j=0,k=0;
	
	cout<<"集合1:\n";
	sq_show(sq1);
	cout<<"集合2:\n";
	sq_show(sq2);


	while(i < len1 && j < len2)
	{
		if(sq1->data[i] < sq2->data[j])
		{
			add_data(sq3,sq1->data[i]);
			i++;
		}
		
		else
		{
			add_data(sq3,sq2->data[j]);
			j++;
		}
	}

	while(i < len1)
	{
		add_data(sq3,sq1->data[i]);
		i++;
	}

	
	while(j < len2)
	{
		add_data(sq3,sq2->data[j]);
		j++;
	}
	
	cout<<"归并:\n";
	sq_show(sq3);

	return true;
}

你可能感兴趣的:(数据结构源码实现#严奶奶版本)