(1)编程实现顺序表的基本操作:建立顺序表,修改顺序表,插入顺序表,删除顺序表;
(2)采用顺序表结构编程实现:两个集合的运算:交集/并集/差集。
/*
(1)
*/
#include <iostream>
#define MAXSIZE 100
typedef struct{
int *elem;
int length;
}Sqlist;
void InitList(Sqlist &L);
void DestroyList(Sqlist &L);
void PrintList(Sqlist L);
void IsEmpty(Sqlist L);
void ListInsert(Sqlist &L,int i,int e);
void ListDelet(Sqlist &L,int i);
void ChangeList(Sqlist &L,int i,int e);
void GetLength(Sqlist L);
using namespace std;
int main(){
Sqlist L;
InitList(L);
for(int i=0;i<10;i++){
L.elem[i]=i;
L.length++;
}
PrintList(L);
IsEmpty(L);
ListInsert(L,20,20);
ListInsert(L,4,44);
PrintList(L);
GetLength(L);
ListDelet(L,9);
PrintList(L);
GetLength(L);
ChangeList(L,8,88);
PrintList(L);
DestroyList(L);
IsEmpty(L);
GetLength(L);
return 0;
}
//初始化顺序表L
void InitList(Sqlist &L){
L.elem=new int[MAXSIZE];
L.length=0;
}
//销毁顺序表L
void DestroyList(Sqlist &L){
if(L.elem) delete[]L.elem;
L.length=0;
L.elem=NULL;
cout<<"---已销毁顺序表---"<<endl;
}
//打印顺序表元素
void PrintList(Sqlist L){
cout<<"顺序表为:";
for(int i=0;i<L.length;i++){
cout<<L.elem[i]<<" ";
}
cout<<endl;
}
//判断顺序表L是否为空
void IsEmpty(Sqlist L){
if(0==L.length)
cout<<"顺序表为空"<<endl;
else
cout<<"顺序表不为空"<<endl;
}
//插入第i个元素(i是位置不是下标)
void ListInsert(Sqlist &L,int i,int e){
if(i<1 || i>L.length+1){
cout<<"位置不对,不存在第"<<i<<"个位置"<<endl;
}
else{
for(int j=L.length-1;j>=i-1;j--){
L.elem[j+1]=L.elem[j];
}
cout<<"---插入第"<<i<<"个位置的值为"<<e<<"---"<<endl;
L.elem[i-1]=e;
++L.length;
}
}
//删除第i个元素(i是位置不是下标)
void ListDelet(Sqlist &L,int i){
if(i<1 || i>L.length)
cout<<"位置不对,不存在第"<<i<<"个位置"<<endl;
else{
cout<<"---删除第"<<i<<"个位置的值为"<<L.elem[i-1]<<"---"<<endl;
for(int j=i;j<=L.length-1;j++)
L.elem[j-1]=L.elem[j];
--L.length;
}
}
//修改第i个元素的数据为e
void ChangeList(Sqlist &L,int i,int e){
L.elem[i-1]=e;
cout<<"修改第"<<i<<"个元素为"<<e<<endl;
}
//获取顺序表长度
void GetLength(Sqlist L){
cout<<"现在顺序表长度为"<<L.length<<endl;
}
/*
(2)
*/
#include <iostream>
#define MAXSIZE 100
typedef struct{
int *elem;
int length;
}jbclist;
void InitList(jbclist &L);
void PrintList(jbclist L);
void jiao(jbclist L1,jbclist L2,jbclist &intersectList);
void bing(jbclist L1,jbclist L2,jbclist &unionList);
void cha(jbclist L1,jbclist L2,jbclist intersectList,jbclist &subtrractList);
using namespace std;
int main(){
jbclist L1;
InitList(L1);
int l1;
cout<<"请输入L1的长度:";
cin>>l1;
for(int i=0;i<l1;i++){
cin>>L1.elem[i];
L1.length++;
}
PrintList(L1);
jbclist L2;
InitList(L2);
int l2;
cout<<"请输入L2的长度:";
cin>>l2;
for(int i=0;i<l2;i++){
cin>>L2.elem[i];
L2.length++;
}
PrintList(L2);
//求交集
jbclist intersectList;
InitList(intersectList);
jiao(L1,L2,intersectList);
//求并集
jbclist unionList;
InitList(unionList);
bing(L2,L1,unionList);
//差集L1-L2
jbclist subtrractList;
InitList(subtrractList);
cha(L1,L2,intersectList,subtrractList);
return 0;
}
//初始化顺序表L
void InitList(jbclist &L){
L.elem=new int[MAXSIZE];
L.length=0;
}
//打印顺序表元素
void PrintList(jbclist L){
cout<<"顺序表为:";
for(int i=0;i<L.length;i++){
cout<<L.elem[i]<<" ";
}
cout<<endl;
}
//求交集
void jiao(jbclist L1,jbclist L2,jbclist &intersectList){
for(int i=0;i<L1.length;i++){
for(int j=0;j<L2.length;j++){
if(L1.elem[i]==L2.elem[j]){
intersectList.elem[intersectList.length]=L1.elem[i];
intersectList.length++;
}
}
}
cout<<endl;
cout<<"交集为:";
for(int i=0;i<intersectList.length;i++){
cout<<intersectList.elem[i]<<" ";
}
cout<<endl;
}
//求并集
void bing(jbclist L1,jbclist L2,jbclist &unionList){
for(int i=0;i<L2.length;i++){
unionList.elem[unionList.length]=L2.elem[i];
unionList.length++;
}
for(int i=0;i<L1.length;i++){
for(int j=0;j<unionList.length;j++){
if(L1.elem[i]==unionList.elem[j]){
break;
}else if(L1.elem[i]!=unionList.elem[j] && j==unionList.length-1){
unionList.elem[unionList.length]=L1.elem[i];
unionList.length++;
}
}
}
cout<<endl;
cout<<"并集为:";
for(int i=0;i<unionList.length;i++){
cout<<unionList.elem[i]<<" ";
}
cout<<endl;
}
//求差集
void cha(jbclist L1,jbclist L2,jbclist intersectList,jbclist &subtrractList){
for(int i=0;i<L1.length;i++){
subtrractList.elem[subtrractList.length]=L1.elem[i];
subtrractList.length++;
}
for(int i=0;i<subtrractList.length;i++){
for(int j=0;j<intersectList.length;j++){
if(subtrractList.elem[i]==intersectList.elem[j]){
for(int k=i;k<=subtrractList.length-2;k++)
subtrractList.elem[k]=subtrractList.elem[k+1];
subtrractList.length--;
}
}
}
cout<<endl;
cout<<"差集为:";
for(int i=0;i<subtrractList.length;i++){
cout<<subtrractList.elem[i]<<" ";
}
cout<<endl;
}