数据结构—线性表(2)顺序表 完整源码
#include <iostream>
#include <stdlib.h>
#include <windows.h>
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;
}