69,368
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
#define MaxSize 10
typedef char ElemType;
typedef struct
{
ElemType elem[MaxSize];
int length;
} SqList;
void Insert(SqList *&A, ElemType x) //error1:excepted ';',','or ')'before '&'token.
{
int i = 0, j;
while(i < A.length && A.elem[i] < x)
i++;
for(j = A.length - 1; j >= i; j--)
A.elem[j+1] = A.elem[j];
A.elem[i] = x;
A.length++;
}
int main()
{
SqList *p;
int i;
p = (SqList*)malloc(sizeof(SqList));
for(i = 0; i < p->length; i++)
scanf("%c",p->elem[i]);
Insert(p,8);
for(i = 0; i < p->length; i++)
printf("%c", p->elem[i]);
return 0;
}
void Insert(SqList *&A, ElemType x)=>void Insert(SqList A, ElemType x)
Insert(p,8);=>Insert(*p,(ElemType)'8');
scanf("%c",p->elem[i]);=>scanf("%c",&p->elem[i]);getchar();单字符行输入
仔细看看,,,#include <stdio.h>
#include <stdlib.h>
#define MaxSize 10
typedef char ElemType;
typedef struct
{
ElemType elem[MaxSize];
int length;
} SqList;
void Insert(SqList *A, ElemType x) //error1:excepted ';',','or ')'before '&'token.
{
int i = 0, j;
while(i < A->length && A->elem[i] < x)//如果你写的是C语言下的数据结构,那么用.的话应该会报错的,C语言下没有A.length
i++;
for(j = A->length - 1; j >= i; j--)
A->elem[j+1] = A->elem[j];
A->elem[i] = x;
A->length++;
}
int main()
{
SqList *p;
int i;
p = (SqList*)malloc(sizeof(SqList));
printf("请输入顺序表内元素的个数:");
scanf("%d",&(p->length));
if((p->length)>MaxSize||(p->length)<0){
printf("输入元素出错!");
exit(-1);
}
for(i = 0; i < p->length; i++){//这里p->length没有初值
getchar();//对于scanf用于字符的读入,因为会读入回车,通常加上一个getchar()
scanf("%c",&(p->elem[i]));//这里少了&取地址运算符
}
Insert(p,'8');//你的ElemType是char类型的,加上''吧
for(i = 0; i < p->length; i++){
printf("%c", p->elem[i]);
}
return 0;
}
LZ试试看行不
void Insert(SqList *&A, ElemType x) //error1:excepted ';',','or ')'before '&'token.
//把&去掉即可
#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
typedef struct
{
ElemType *elem;
int length;
int maxSize;
} SqList;
void reMalloc(SqList *A)
{
ElemType *p=(ElemType*)realloc(A->elem,2*A->maxSize*sizeof(ElemType));
if(p==NULL){
printf("realloc error!");
exit(1);
}
A->elem=p;
A->maxSize=2*A->maxSize;
}
void Insert(SqList *A, ElemType x)
{
int i = 0, j;
while(i < A->length && A->elem[i] < x)
i++;
if(A->length==A->maxSize)
reMalloc(A);
for(j = A->length-1; j >= i; j--)
A->elem[j+1] = A->elem[j];
A->elem[i] = x;
A->length++;
}
void initList(SqList *A,int maxsize)
{
if(maxsize<0)
{
printf("max size error");
exit(1);
}
A->length=0;
A->maxSize=maxsize;
A->elem=(ElemType *)malloc(maxsize*sizeof(ElemType));
if(A->elem==NULL)
{
printf("malloc error!");
exit(1);
}
for (int i=0;i<A->maxSize;i++)
{
printf("input element %d = ",i);
scanf("%d",&(A->elem[i]));
A->length++;
}
}
void show(SqList A)
{
for(int i=0;i<A.length;i++)
printf("%d ",A.elem[i]);
}
int main()
{
SqList p;
int maxsize=0;
int value;
printf("input the sqlist size :");
scanf("%d",&maxsize);
initList(&p,maxsize);
printf("Input the insert value:");
scanf("%d",&value);
Insert(&p,value);
show(p);
return 0;
}