# 这是哪错了？数据结构的？

yonggangsun 2011-11-08 02:17:16

#include <stdio.h>
#include <malloc.h>
#define MaxSize 50
typedef char ElemType;
typedef struct
{ ElemType data[MaxSize]; //存放顺序表元素
int length; //存放顺序表的长度
} SqList; //顺序表的类型定义
void CreateList(SqList *&L,ElemType a[],int n)
//建立顺序表
{
int i;
L=(SqList *)malloc(sizeof(SqList));
for (i=0;i<n;i++)
L->data[i]=a[i];
L->length=n;
}
void InitList(SqList *&L)
{
L=(SqList *)malloc(sizeof(SqList)); //分配存放线性表的空间
L->length=0;
}
void DestroyList(SqList *&L)
{
free(L);
}
int ListEmpty(SqList *L)
{
return(L->length==0);
}
int ListLength(SqList *L)
{
return(L->length);
}
void DispList(SqList *L)
{
int i;
if (ListEmpty(L)) return;
for (i=0;i<L->length;i++)
printf("%c ",L->data[i]);
printf("\n");
}
int GetElem(SqList *L,int i,ElemType &e)
{
if (i<1 || i>L->length)
return 0;
e=L->data[i-1];
return 1;
}
int LocateElem(SqList *L, ElemType e)
{
int i=0;
while (i<L->length && L->data[i]!=e) i++;
if (i>=L->length)
return 0;
else
return i+1;
}
int ListInsert(SqList *&L,int i,ElemType e)
{
int j;
if (i<1 || i>L->length+1)
return 0;
i--; //将顺序表位序转化为elem下标
for (j=L->length;j>i;j--) //将data[i]及后面元素后移一个位置
L->data[j]=L->data[j-1];
L->data[i]=e;
L->length++; //顺序表长度增1
return 1;
}
int ListDelete(SqList *&L,int i,ElemType &e)
{
int j;
if (i<1 || i>L->length)
return 0;
i--; //将顺序表位序转化为elem下标
e=L->data[i];
for (j=i;j<L->length-1;j++) //将data[i]之后的元素前移一个位置
L->data[j]=L->data[j+1];
L->length--; //顺序表长度减1
return 1;
}
int compare(SqList *&a,SqList *&b)
{
char i,j;
ElemType e,f;
while(a->data[0]==b->data[0])
{
i=ListDelete(a,0,e);
j=ListDelete(b,0,f);
}
if((a->length==b->length)&&a->data[0]==NULL)
return 0;
else if((a->data[0]>b->data[0])||(a->length!=0&&b->length==0))
return 1;
else
return -1;
}

void main()
{
SqList *a,*b;
//ElemType e;
//int c;
InitList(a);
ListInsert(a,1,'a');
ListInsert(a,2,'c');
ListInsert(a,3,'a');
ListInsert(a,4,'d');
ListInsert(a,5,'b');
DispList(a);
InitList(b);
ListInsert(b,1,'a');
ListInsert(b,2,'c');
ListInsert(b,3,'e');
ListInsert(b,4,'d');
ListInsert(b,5,'b');
DispList(b);

printf("%d\n",compare(a,b));

// printf("ListLength(L)=%d\n",ListLength(L));
// printf("ListEmpty(L)=%d\n",ListEmpty(L));
// GetElem(L,3,e);
// printf("e=%c\n",e);
// printf("LocateElem(L,'a')=%d\n",LocateElem(L,'a'));
// ListInsert(L,4,'e');

// ListDelete(L,3,e);
// DispList(L);
DestroyList(a);
DestroyList(b);
}
...全文
112 4 打赏 收藏 举报

4 条回复

yonggangsun 2011-11-08
• 打赏
• 举报

int compare(SqList *&a,SqList *&b)
{
char i,j;
ElemType e,f;
while(a->data[0]==b->data[0]&&a->length!=0)
{
i=ListDelete(a,1,e);
j=ListDelete(b,1,f);
}
if((a->data[0]<b->data[0])||(a->length==0&&b->length!=0))
return -1;
else if((a->data[0]>b->data[0])||(a->length!=0&&b->length==0))
return 1;
else
return 0;
}

• 打赏
• 举报

void CreateList(SqList *&L,ElemType a[],int n)

SqList * CreateList(ElemType a[],int n)
//建立顺序表
{
int i;
SqList *L=(SqList *)malloc(sizeof(SqList));
for (i=0;i<n;i++)
L->data[i]=a[i];
L->length=n;
reurn L;
}

• 打赏
• 举报

``````
int compare(SqList *a,SqList *b)
{
int i = 0,j = 0;
while( i < a->length && j < a->length )
{
if(a->data[i] == b->data[j])
{
i++;
j++;
}
else if( a->data[i] > b->data[j] )
{
return 1;
}
else
return -1;
}
if( a->length > b->length )
return 1;
if( a->length < b->length )
return -1;
return 0;
}
``````

• 打赏
• 举报

i=ListDelete(a,1,e);//-----error
j=ListDelete(b,1,f);//-----error是从第一个元素开始 不是0

6.7w+

C语言相关问题讨论

2011-11-08 02:17