函数用c语言实现顺序表 用c语言编写顺序表( 三 )


{
int i;
if(ListEmpty(L)) return;
for(i=0;iL-length;i++)
printf("%c",L-elem[i]);
printf("\n");
}
int ListLength(SqList *L)/* 求顺序表的长度 */
{
return(L-length);
}
int ListEmpty(SqList *L)/* 求顺序表是否为空 */
{
return(L-length==0);
}
int GetElem(SqList *L,int i,ElemType e)/*求顺序表中某个数据的元素值 */
{
if(i1||iL-length)
return 0;
else
e=L-elem[i-1];
return 1;
}
int LocateElem(SqList *L,ElemType e)/*按值查找元素*/
{
int i=0;
while(iL-lengthL-elem[i]!=e) i++;
if(i=L-length)
return 0;
else
return i+1;
}
int ListInsert(SqList *L,int i,ElemType e)/*按i位置插入元素*/
{
int j;
if(i1||iL-length+1)
return 0;
i--;
for (j=L-length;ji;j--)
L-elem[j]=L-elem[j-1];
L-elem[i]=e;
L-length++;
return 1;
}
int ListDelete(SqList *L,int i,ElemType e)/*删除某个位置的元素*/
{
int j;
if (i1||iL-length)
return 0;
i--;
e=L-elem[i];/*e中的放elem[i]有何用..以后没用到*/
for(j=i;jL-length-1;j++)
L-elem[j]=L-elem[j+1];
L-length--;
return 1;
}
void DestroyList(SqList *L)/*销毁链表*/
{
free(L);
}
void main()
{
SqList L;
ElemType a[]={'a','b','c','d'};
int c;
int e;
while(1)
{
printf("Please Choose the step you want ?\n\n");
scanf("%d",c);
if(c==0) break;
switch(c)
{
case 1: InitList(L);break;
case 2: CreateListR(L,a,4);break;
case 3: DispList(L);break;
case 4: printf("long %d", ListLength(L));break;
case 5: printf("%d",ListEmpty(L));break;
case 6: GetElem(L,3,e);break;
case 7: LocateElem(L,'a');break;
case 8: ListInsert(L,4,'f');break;
case 9: DispList(L);break;
case 10: ListDelete(L,3,e);break;
case 11: DispList(L);break;
case 12: DestroyList(L);break;
default: printf("error data");break;
}
}
}
C语言顺序表的基本操作#include stdio.h
#include stdlib.h
typedef int DataType; // 定义数据数据类型
typedef struct {
DataType *data;// data指向数据区的首个数据
int length;// 数据长度
}SqList;
void Sort(SqList *L) {
int i,j,k;
DataType tmp;
for(i = 0; iL-length - 1; ++i) {
k = i;
for(j = i + 1; jL-length; ++j)
if(L-data[k]L-data[j])
k = j;
if(k != i) {
tmp = L-data[k];
L-data[k] = L-data[i];
L-data[i] = tmp;
}
}
}
SqList *CreateList(DataType a[],int n) {
int i;
SqList *L;
L = (SqList *)malloc(sizeof(SqList));
L-data = https://www.04ip.com/post/(DataType *)malloc(n * sizeof(DataType));
L-length = n;
for(i = 0; in; ++i) L-data[i] = a[i];
Sort(L);
return L;
}
int InsertList(SqList *L,DataType x) {
int i,j;
for (i = 0;iL-length;i++) {
if(x = L-data[i]) {
for(j = L-length;j = i;j--)
L-data[j + 1] = L-data[j]; // 结点后移
L-data[i] = x;
L-length++;
return 1;
}
}
L-data[L-length++] = x;
return 1;
}
int RemoveListElem(SqList *L,DataType d) {
int i,j;
for(i = 0; iL-length; ++i) {
if(L-data[i] == d) {
for(j = i; jL-length - 1; ++j)
L-data[j] = L-data[j + 1];
L-length--;
return 1;
}
}
return 0;
}
SqList *AndList(SqList *A, SqList *B) {/* A∩B */
int i,j,k = 0;
int len = (A-lengthB-length) ? B-length : A-length;
SqList *C = (SqList *)malloc(sizeof(SqList));
C-length = len;
C-data = https://www.04ip.com/post/(DataType *)malloc(len * sizeof(DataType));

推荐阅读