程序除了case 1,其他都运行不出来
试了case 2,能输入能输出元素,但是求交集就显示以下错误
头文件是给的现成的
#include
using namespace std;
#include"SqList.h"
char pause;
void dispmenu()
{
cout << endl;
cout << "1-创建集合\n";
cout << "2-集合A、B交\n";
cout << "3-集合A、B并\n";
cout << "4-集合A、B差\n";
cout << "0-退出\n";
}
//交
template<class DT>
void InterSet(SqList La, SqList Lb, SqList & Lc) //两个集合的交
{
int j = 0;
char e = ' ';
for (int i = 1; i <= La.length; i++)
{
if (GetElem_i(La, i, e))
if (LocateElem_e(Lb, e))
InsertElem_i(Lc, i, e);
}
Lc.length = j;
}
//并
template<class DT>
void Union(SqList & La, SqList Lb) //两个集合的并
{
for (int i = 1; i < Lb.length; i++)
{
char e = ' ';
GetElem_i(Lb, i, e);
if (!LocateElem_e(La, e))
{
int k = La.length + 1;
InsertElem_i(La, k, e);
}
}
}
//cha
template<class DT>
void SubSet(SqList La, SqList Lb, SqList & Lc) //两个集合的差
{
int j = 0;
char e = ' ';
for (int i = 1; i <= La.length; i++)
{
if (GetElem_i(La, i, e))
if (!LocateElem_e(Lb, e))
InsertElem_i(Lc, j++, e);
}
Lc.length = j;
}
int main()
{
int na, nb;
int i = 0, j = 0;
char e = ' ';
SqList<char> La; //建立容量为20、元素类型为字符型的空顺序表
SqList<char> Lb;
SqList<char> Lc;
system("cls");
int choice;
do
{
dispmenu(); //显示主菜单
cout << "Enter choice(1~4,0 退出):";
cin >> choice;
switch (choice)
{
case 1: //初始化线性表 La,Lb
cout << "请输入要创建的顺序表La的长度:";
cin >> na;
InitList(La, na);
cout << endl << "创建成功!" << endl;
cout << "请输入要创建的元素个数:";
cin >> na;
CreateList(La, na);
cout << "创建的顺序表La元素为:\n"; //显示表元素La
DispList(La);
cout << endl;
cout << "请输入要创建的顺序表Lb的长度:";
cin >> nb;
InitList(Lb, nb);
cout << endl << "创建成功!" << endl;
cout << "请输入要创建的元素个数:";
cin >> nb;
CreateList(Lb, nb);
cout << "创建的顺序表Lb元素为:\n"; //显示表元素Lb
DispList(Lb);
cout << endl;
break;
case 2: //两个集合的交
cout << "请输入要创建的顺序表La的长度:";
cin >> na;
InitList(La, na);
cout << endl << "创建成功!" << endl;
cout << "请输入要创建的元素个数:";
cin >> na;
CreateList(La, na);
cout << "创建的顺序表La元素为:\n"; //显示表元素La
DispList(La);
cout << endl;
cout << "请输入要创建的顺序表Lb的长度:";
cin >> nb;
InitList(Lb, nb);
cout << endl << "创建成功!" << endl;
cout << "请输入要创建的元素个数:";
cin >> nb;
CreateList(Lb, nb);
cout << "创建的顺序表Lb元素为:\n"; //显示表元素Lb
DispList(Lb);
cout << endl;
InterSet(La, Lb, Lc);
cout << "A与B的并集为:" << endl;
DispList(Lc);
cout << endl;
DestroyList(La);
DestroyList(Lb);
DestroyList(Lc);
break;
case 3:
cout << "请输入要创建的顺序表La的长度:";
cin >> na;
InitList(La, na);
cout << endl << "创建成功!" << endl;
cout << "请输入要创建的元素个数:";
cin >> na;
CreateList(La, na);
cout << "创建的顺序表La元素为:\n"; //显示表元素La
DispList(La);
cout << endl;
cout << "请输入要创建的顺序表Lb的长度:";
cin >> nb;
InitList(Lb, nb);
cout << endl << "创建成功!" << endl;
cout << "请输入要创建的元素个数:";
cin >> nb;
CreateList(Lb, nb);
cout << "创建的顺序表Lb元素为:\n"; //显示表元素Lb
DispList(Lb);
cout << endl;
Union(La, Lb);
cout << "A与B的并集为:" << endl;
DispList(La);
cout << endl;
DestroyList(La);
DestroyList(Lb);
break;
case 4:cout << "请输入要创建的顺序表La的长度:";
cin >> na;
InitList(La, na);
cout << endl << "创建成功!" << endl;
cout << "请输入要创建的元素个数:";
cin >> na;
CreateList(La, na);
cout << "创建的顺序表La元素为:\n"; //显示表元素La
DispList(La);
cout << endl;
cout << "请输入要创建的顺序表Lb的长度:";
cin >> nb;
InitList(Lb, nb);
cout << endl << "创建成功!" << endl;
cout << "请输入要创建的元素个数:";
cin >> nb;
CreateList(Lb, nb);
cout << "创建的顺序表Lb元素为:\n"; //显示表元素Lb
DispList(Lb);
cout << endl;
SubSet(La, Lb, Lc);
cout << "A与B的差为:" << endl;
DispList(Lc);
cout << endl;
DestroyList(La);
DestroyList(Lb);
DestroyList(Lc);
break;
case 0: //退出
cout << "结束运行bye-bye!" << endl;
break;
default: //无效选择
cout << "无效选择!\n";
break;
}
} while (choice != 0);
return 1;
};
```c++
template <class DT>
struct SqList // 顺序表
{
DT *elem; // 表首址
int length; // 表长
int size; // 表容量
};
//算法2.1
template <class DT>
bool PriorElem_e(SqList L, DT e, DT &pre_e) // 求值为e的元素前驱
{
int k;
k=LocateElem_e(L,e); //
if(k>1)
{
GetElem_i(L,k-1,pre_e);
return true;
}
else
return false;
}
//【算法2.2】 初始化
template <class DT>
bool InitList(SqList &L, int m)
{
L.elem=new DT[m]; // 申请表空间
if(L.elem==NULL)
{
cout<<"未创建成功!"; // 申请不成功,退出
exit (1);
}
L.length=0; // 申请成功,属性赋值。空表,表长为0
L.size=m; // 表容量为m
return true;
}
//【算法2.3】 创建表元素
template <class DT>
bool CreateList(SqList &L,int n)
{
int i;
if(n>L.size) // 1.元素个数大于表容量,不能创建。
{
cout<<"元素个数大于表长,不能创建!"<true;
}
cout<<"请依次输入"<"个元素值:"<// 2.依位序输入各元素值
for(i=1;i<=n;i++)
cin>>L.elem[i-1];
L.length=n; // 3.表长为创建的元素个数
return false;
}
//【算法2.4】 销毁顺序表
template <class DT>
void DestroyList(SqList &L)
{
delete [] L.elem; // 1.释放表空间
L.length=0; // 2.属性赋值
L.size=0;
}
//【算法2.5】 获取第i个元素值
template<class DT>
bool GetElem_i(SqList L,int i, DT &e)
{
if(i<1 || i>L.length) // 1.位序不合理,返回false
{
cout<<"该元素不存在!"<false;
}
e=L.elem[i-1]; // 2. 否则,获取第i个元素值
return true; // 返回true
}
//【算法2.6】 按值查找
template<class DT>
int LocateElem_e(SqList L, DT e)
{
for(int i=0; i<L.length; i++) // 顺序查找
if(L.elem[i]==e) // 1.找到
return i+1; // 返回元素位序
return 0; // 2.未找到,返回0
}
//【算法2.7】
template<class DT>
bool InsertElem_i(SqList &L,int i, DT e)
{
if(L.length>=L.size) // 1.表满,不能插入
return false;
if(i<1 || i>L.length+1) // 2.插入位置不合理,不能插入
return false;
for (int j=L.length; j>=i; j--) // 3. an~ai依次后移
L.elem[j]=L.elem[j-1];
L.elem[i-1]=e;
L.length++;
return true; // 插入成功,返回true
}
//【算法2.8】 删除第i个元素
template<class DT>
bool DeleElem_i(SqList &L,int i)
{
if(L.length==0) // 1.表空,不能删除
return false;
if(i<1 || i>L.length) // 2.删除位置不合理,不能插入
return false;
for (int j=i; j<L.length; j++) // 3. ai+1~an依次前移
L.elem[j-1]=L.elem[j];
L.length--;
return true; // 删除成功,返回true
}
//【算法2.9】
template<class DT>
bool PutElem(SqList &L,int i, DT e) // 修改第i个元素的值
{
if(i<1 || i>L.length) // 1.位序不合理,不能修改,
return false; // 返回false
L.elem[i-1]=e; // 2.重置第i个元素值
return true; // 3.修改成功,返回true
}
// 清空顺序表
template<class DT>
void ClearList(SqList &L)
{
L.length=0; // 空表,表长为0
}
// 测表长
template<class DT>
int ListLength(SqList L)
{
return L.length;
}
template<class DT>
bool ListEmpty(SqList L) // 测表空
{
if(L.length==0) // 空表,返回true
return true;
else
return false; // 非空表,返回false
}
template<class DT>
bool ListFull(SqList L)
{
if(L.length==L.size) // 表满,返回true
return true;
else
return false; // 表不满,返回false
}
//【算法2.10】 遍历输出
template <class DT>
void DispList(SqList L)
{
int i;
for(i=0;i<L.length;i++) // 依位序输出元素值
{
cout<<L.elem[i]<<"\t";
}
cout<
```
昨天的问题后来解决了么?
这次你怎么输入的
其中 L.elem是不是已经释放了,造成重复释放
不知道你这个问题是否已经解决, 如果还没有解决的话:要搞清楚什么是引用,首先我们要清楚变编程语言的变量名或者叫标识符究竟是什么,比如int a; int *p; 这里面的a或者p,究竟是个什么呢? ———— 这些标识符本质上就是用来代指一个内存单元的.在汇编语言中,比如[0x1101]这就是一个内存单元,里面的0x1101是这个内存单元的地址或者说编号,外面的方括号用来表明直接寻址方式,即表示这个地址所代表的的内存单元。
而用一长串地址加一个方括号表示内存单元太过麻烦,我们就给这个[0x1101]这个内存单元起了一个好听的名字a,这个符号a经过编译器翻译和分配内存单元之后,所形成的的汇编指令,就被翻译成[0x1101]了,我们可以叫这个内存单元a,当然我们也可以叫它b或者dog随便什么名字都可以,一个内存单元当然可以拥有多个名字,至于叫什么其实无所谓,因为指的都是这个内存单元。这个小名b就是引用啦。
int a = 0;
int &b = a;
这个b就是给a这个内存起了个小名b而已,并没有什么特别的。
&在C++中有两种用法,一种是引用,一种是取地址。
同样,指针int *p = &a; 中的p,所代表的也不过是一个普通的内存单元,我们就叫它[0x1102],
1、 我们&p ,也就是取这个内存单元的地址,也就是0x1102这个地址值。
2、我们输出p ,得到的就是这个内存单元里的内容,也就是a的地址呗,就是0x1101
3、 我们输出*p,这是解引用操作,得到的就是a所代表的内存单元存的的内容,或者说是指针p指向的内存单元的内容,就是0
那么我们既然知道了变量a引用变量b指的都是同一个内存单元[0x1101],那呢我们无论或者a或者b中的任何一个进行操作,[0x1101]这个内存单元都会发生相应的改变。
2.引用的注意事项:
1、引用必须初始化: int &b; b = a;是错误的,必须是int &b = a,引用在初始化后,不可以改变:
2、一个别名不允许更改成另一个变量的别名,起别名后,别名只能代表一块内存,不能改变成另一块内存
示例代码
//1. 引用的基本语法
int a = 10;
int &b = a; //给a对应的内存起个别名b,用b也可以操作这款块内存
b = 20;
cout <<"a= "<< a << endl; // 20
cout << "b= "<<b << endl; // 20
b = 100;
cout << "a= " << a << endl; //100
cout << "b= " << b << endl; //100
//2 引用的注意事项:必须初始化,不可改变内存的指向
int c = 10;
int &d = c;
int g = 20;
d = g; //赋值从操作,而不是更改引用
// int& d = g; //这也不是更改引用,这是多次初始化了,也是错的,别名就不能更改
cout << "c= " << c << endl; //c和d的内容一样,因为这就是一块相同的内存区域
cout << "d= " << d << endl;
cout << "g= " << g << endl;