//hanshushixian.cpp
#include "yudingyi.h"
#include "shunxujiegou.h"
//----线性表的动态分配顺序存储结构函数实现---------
Status InitList(SqList *L)//算法2.3:构造一个空的线性表
{
(*L).elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!(*L).elem) exit(OVERFLOW);
(*L).length=0;
(*L).listsize=LIST_INIT_SIZE;
return OK;
}
Status DestroyList(SqList *L)//销毁线性表
{
free((*L).elem);
(*L).elem=NULL;
(*L).length=0;
(*L).listsize=0;
return OK;
}
Status ClearList(SqList *L)//重置线性表为空表
{
(*L).length=0;
(*L).listsize=LIST_INIT_SIZE;
return OK;
}
Status ListEmpty(SqList L)//若为空表,返回TRUE,否则返回FALSE
{
if(L.length==0)
return TRUE;
else
return FALSE;
}
Status ListLength(SqList L)//返回表中数据个数
{
return L.length;
}
Status GetElem(SqList L,int i, int *e)//用e返回表中第i个数据元素的值
{
if(i>L.listsize) return FALSE;
ElemType *p=L.elem;
*e=p[i-1];
return OK;
}
Status LocateElem(SqList L,ElemType e,Status (*compare)(ElemType *p,ElemType e))//算法2.6:返回L中第一个与e满足compare关系数据元素的位序
{
int i,n,j;
ElemType *p=L.elem;
n=ListLength(L);
for(i=1;i<=n;i++)
{
j=compare(p++,e);
if(j==1) return i;
}
return FALSE;
}
Status PriorElem(SqList L,ElemType cur_e,ElemType *e)//若cur_e是L的数据元素,且不是第一个,则用e返回它的前驱,否则操作失败
{
ElemType *p=L.elem;
ElemType i;
for(i=0;i=L.length-1) return FALSE;
else *e=p[i-1];
return OK;
}
Status NextElem(SqList L,ElemType cur_e,ElemType *e)//若cur_e是L的数据元素,且不是最后一个,则用e返回它的后继,否则操作失败
{
ElemType *p=L.elem;
ElemType i;
for(i=0;i=L.length-1) return FALSE;
else *e=p[i+1];
return OK;
}
Status ListInsert(SqList *L,ElemType i,ElemType e)//算法2.4:在L中第i个位置之前插入新的数据元素,L的长度加1
{
ElemType *newbase,j;
if(!(1<=i<=(*L).length+1)) return FALSE;
if((*L).length==(*L).listsize)
{
newbase=(ElemType *)realloc((*L).elem,((*L).listsize+LISTINCREMENT)*sizeof(ElemType));
if(!newbase) exit(OVERFLOW);
(*L).elem=newbase;
(*L).listsize +=LISTINCREMENT;
}
for(j=0;j<=(*L).length-i;j++)
{
((*L).elem)[(*L).length-j]=((*L).elem)[(*L).length-j-1];
}
((*L).elem)[i-1]=e;
(*L).length++;
return OK;
}
Status ListDelete(SqList *L,ElemType i,ElemType *e)//算法2.5:删除L的第i个元素,并用e返回其值,L的长度减1
{
if(!(1<=i<=(*L).length)) return FALSE;
*e=((*L).elem)[i-1];
ElemType j;
for(j=0;j<=(*L).length-i-1;j++)
{
((*L).elem)[i-1+j]=((*L).elem)[i+j];
}
(*L).length--;
return OK;
}
Status ListTravarse(SqList L,Status (*com)(ElemType *p))//依次对L中的每个元素调用com
{
ElemType *p,i;
Status n,m;
p=L.elem;
n=ListLength(L);
for(i=0;i
#include "yudingyi.h"
#include "shunxujiegou.h"
Status visit (ElemType *p)
{
printf("%d \n",*p);
return OK;
}
Status compare(ElemType *p,ElemType e)
{
if(*p==e) return OK;
else return FALSE;
}
int main()
{
SqList T,F;
int i,j,m,n,e,f;
n=InitList(&T);
printf("初始化表a:%d (1:成功 0:失败)\n",n);
n=InitList(&F);
printf("初始化表b:%d (1:成功 0:失败)\n",n);
printf("请输入准备往表a输入的个数:");
scanf("%d",&n);
for(j=1;j<=n;j++)
{
printf("请输入表a第%d个数:",j);
scanf("%d",&m);
i=ListInsert(&T,j,m);
printf("输入成功?:%d (1:成功 0:失败)\n",i);
}
printf("输出表a内数据:");
for(j=1;j<=n;j++)
{
printf("%d ",T.elem[j-1]);
} //表a初始化完毕
printf("\n请输入准备往表b输入的个数:");
scanf("%d",&n);
for(j=1;j<=n;j++)
{
printf("请输入表b第%d个数:",j);
scanf("%d",&m);
i=ListInsert(&F,j,m);
printf("输入成功?:%d (1:成功 0:失败)\n",i);
}
printf("输出表b内数据:");
for(j=1;j<=n;j++)
{
printf("%d ",F.elem[j-1]);
}//表b初始化完毕
Union(&T,F);
printf("\n输出表a内数据:");
e=ListLength(T);
for(j=1;j<=e;j++)
{
printf("%d ",T.elem[j-1]);
}
printf("\n");
i=ListEmpty(T);
printf("表a是否为空表:%d (1:是 0:否)\n",i);
i=ClearList(&T);
printf("置空表a:%d (1:成功 0:失败)\n",i);
i=ListEmpty(T);
printf("表a是否为空表:%d (1:是 0:否)\n",i);
i=ListLength(F);
printf("表b的长度:%d\n",i);
i=GetElem(F,3,&f);
printf("操作成功:%d (1:成功 0:失败)\n",i);
printf("表b第3个数:%d\n",f);
i=PriorElem(F,2,&f);
printf("操作成功:%d (1:成功 0:失败)\n",i);
printf("表b元素2的前驱:%d\n",f);
i=NextElem(F,2,&f);
printf("操作成功:%d (1:成功 0:失败)\n",i);
printf("表b元素2的后继:%d\n",f);
i=ListDelete(&F,2,&f);
printf("删除成功:%d (1:成功 0:失败)\n",i);
printf("输出表b内数据:");
e=ListLength(F);
for(j=1;j<=e;j++)
{
printf("%d ",F.elem[j-1]);
}
printf("\n输出表b内数据:");
i=ListTravarse(F,visit);
}
void Union(SqList *La,SqList Lb)//---算法2.1:将所有在Lb中但不在La中的数据元素插入到La中---
{
ElemType La_length,Lb_length;
Lb_length=ListLength(Lb);
int i;
ElemType m,n;
for(i=0;i
#include "stdio.h"
#include "stdlib.h"
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
//-----线性表的动态分配顺序存储结构-------
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef struct {
ElemType *elem;
int length;
int listsize;
}SqList;
//---------------函数声明------------------
Status InitList(SqList *L);
Status DestroyList(SqList *L);
Status ClearList(SqList *L);
Status ListEmpty(SqList L);
Status ListLength(SqList L);
Status GetElem(SqList L,int i, int *e);
Status LocateElem(SqList L,ElemType e,Status (*compare)(ElemType *p,ElemType e));
Status PriorElem(SqList L,ElemType cur_e,ElemType *e);
Status NextElem(SqList L,ElemType cur_e,ElemType *e);
Status ListInsert(SqList *L,ElemType i,ElemType e);
Status ListDelete(SqList *L,ElemType i,ElemType *e);
void Union(SqList *La,SqList Lb);
Status ListTravarse(SqList L,Status (*com)(ElemType *p));
Status visit (ElemType *p);
Status compare(ElemType *p,ElemType e);