问题描述
- 求教;程序中的Print_Sq(list3)为什么输不出来
-
#include
#include
#define LIST_INIT_SIZE 100//线性表储存空间的初始分配量
#define LiSTINCREMENT 10//线性表储存空间的分配增量
#define OVERFLOW -2
#define ERROR 0
#define OK 1
typedef int Status;
typedef int ElemType;
typedef struct
{
ElemType *elem; //储存空间基址
int length; //当前长度
int listsize; //但前分配储存容量
}SqList;
Status InitList_Sq(SqList &L)
{
// 构造一个空的线性表L。
L.elem = new ElemType[LIST_INIT_SIZE];
if (!L.elem) return OVERFLOW; // 存储分配失败
L.length = 0; // 长度为0
L.listsize = LIST_INIT_SIZE; // 初始存储容量
return OK;
}
Status ListInsert_Sq(SqList &L,int i,ElemType e)
{
ElemType *p,*q;
ElemType *newbase;
if(iL.length+1) return ERROR;
if(L.length>=L.listsize)
{
newbase=(ElemType *)realloc(L.elem,(L.listsize+LiSTINCREMENT)*sizeof(ElemType));
if(!newbase) return OVERFLOW;
L.elem=newbase;
L.listsize+=LiSTINCREMENT;
}
q=&(L.elem[i-1]);
for(p=&(L.elem[L.length-1]);p>=q;--p)
*(p+1)=*p;
*q=e;
++L.length;
return OK;
}
Status InsertList_Sq(SqList &L,int i,ElemType e)
{
i=L.length;
L.elem[i]=e;
++L.length;
return L.elem[i];
}int GetList_Sq(SqList L,int i,ElemType &e)
{ if(i>0 && i<=L.length)
{
return L.elem[i];
}
else
return ERROR;
}
int LocateElem_Sq(SqList L, ElemType e)
{
// 在顺序表中查询数据元素e,若存在,则返回它的位序,否则返回 0
int i = 1; // i 的初值为第 1 元素的位序
ElemType *p = L.elem; // p 的初值为第 1 元素的存储位置
while (i <= L.length && *p!=e)
{
++i;
++p;
}
if (i <= L.length)
return i;
else
return 0;
}
void Create_Sq(SqList &L)
{
cout<<"创建线性表"<
cout
int count;
cin>>count;
for(int i=0;i
{
cout
cin>>L.elem[i];
++L.length;
}
}
void Print_Sq(SqList &L)
{
cout<<"请输出线性表:"<<endl;
for(int i=0;i<L.length;i++)
cout<<L.elem[i]<<" ";
cout<<endl;
}
int ListLength(SqList L)
{
return L.length;
}
void MergeList_Sq(SqList La,SqList Lb,SqList &Lc)
{
ElemType e;
InitList_Sq(La);
InitList_Sq(Lb);
InitList_Sq(Lc);
int La_len,Lb_len,Lc_len;La_len=ListLength(La);
Lb_len=ListLength(Lb);
Lc_len=ListLength(Lc);
for(int i=0;i<=Lb_len;i++)
{
GetList_Sq(Lb,i,e);
if(GetList_Sq(Lb,i,e)==LocateElem_Sq(La,e))
InsertList_Sq(Lc,i,e);}
}
//集合的并
void main()
{
SqList list1,list2,list3;
InitList_Sq(list1);
InitList_Sq(list2);
InitList_Sq(list3);
MergeList_Sq(list1,list2,list3);
Create_Sq(list1);
Create_Sq(list2);
Print_Sq(list1);
Print_Sq(list2);
Print_Sq(list3);
}