点击查看代码
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <malloc.h>
#define TURE 1
#define false 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define LIST_INIT_SIZE 100
#define LISTINCREAMENT 10
typedef int Status;
typedef int ElemType;
typedef struct
{
ElemType *elem;
int length;
int listsize;
}SqList;
Status InitList(SqList *L);
Status ListInsert(SqList *L,int i,ElemType e);
Status ListDelete(SqList *L,int i,ElemType *e);
void ListOutput(SqList L);
//void User_Choice(int Choice);
Status LocataElem(SqList,ElemType e);
//C = A∩B
SqList Intersection(SqList *LA,SqList *LB);
int main()
{
SqList L;
Status flag;
int n,i,Choice;
ElemType e;
//初始化顺序表
flag = InitList(&L);
if(flag = OK)
{
printf("List init sucess!
");
}
else
printf("
Please input %d elements:
",n);
printf("Choice:1,2,3,4,5,6....");
scanf("%d",&Choice);
switch(Choice)
{
case 1://插入
{
printf("input n:");
scanf("%d",n);
printf("
Please intput %d elements:
");
for(i = 1;i<=n;i++)
{
scanf("%d",&e);
flag = ListInsert(&L,i,e);
}
};break;
case 2://输出
{
printf("
The elements of list are:
");
ListOutput(L);
} ;break;
case 3://查找
{
printf("
Iput the element to be queried:");
scanf("%d",&e);
i=0;
i=LocataElem(L,e);
printf("Its index is %d.",i);
};break;
case 4://删除
{
printf("
Input the index of element to be deleted:");
scanf("%d",&i);
flag = ListDelete(&L,i,&e);
printf("the deleted element is %d:
",e);
};break;
case 5: ;break;
case 6: ;break;
}
return 0;
}
//初始化为空的顺序表
Status InitList(SqList *L)
{
L->elem = (ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L->elem)
exit(OVERFLOW);
L->length = 0;
L->listsize = LIST_INIT_SIZE;
}
Status ListInsert(SqList *L,int i,ElemType e)
{
ElemType *newbase,*q,*p;
if(i<1 || i>L->length+1)
return ERROR;
if(L->length>=L->listsize)
{
newbase = (ElemType*)realloc(L->elem,(L->listsize+LISTINCREAMENT)*sizeof(ElemType));
if(newbase == NULL) exit(OVERFLOW);
L->elem = newbase;
L->listsize = L->listsize +LISTINCREAMENT;
}
q = &(L->elem[i-1]);
for(p = &L->elem[L->length-1];p>=q;--p)
*(p+1) = *p;
*q = e;
++L->length;
return OK;
}
void ListOutput(SqList L)
{
int i;
if(L.length == 0)
printf("List is null!
");
else
{
for(i=0;i<L.length;i++)
printf("%d",L.elem[i]);
printf("
");
}
}
Status LocataElem(SqList L,ElemType e)
{
int i = 0;
while(i<=L.length-1 &&L.elem[i]!=e)
i++;
if(i<=L.length - 1)
return i;
else
return -1;
}
Status ListDelete(SqList *L,int i,ElemType *e)
{
ElemType *p,*q;
if(i<1 || i>L->length) exit(ERROR);
p = &L->elem[i-1];
*e = *p;
q = L->elem+L->length-1;
for(++p;p<=q;p++)
{
*(p-1) = *p;
}
--L->length;
return OK;
}
SqList Intersection(SqList *LA,SqList *LB)
{
SqList LC;
InitList(&LC);
int i,e;
for(i=0;i<LA->length;i++)
{
LC.elem[i] =LA.elem[i];
LC->length++;
}
for()
}