设计一个算法,将x插入到一个有序的线性表的适当位置,并保持线性表的有序性

whdugh 2013-01-12 09:37:57
#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;
}

有一处错误 这个要怎么改 谢谢
...全文
4995 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
lm_whales 2013-01-16
  • 打赏
  • 举报
回复
void Insert(SqList *&A, ElemType x) 这是C++语法 SqList *&A 指针的引用,并且在这里引用是不必要的。 C要这样写:指针 void Insert(SqList *A, ElemType x); 下面是修改过的: #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) { int i = 0, j; if(!A)return;/*加上这个吧!*/ if(A->length >MaxSize)return;/*已满不插入!*/ 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)); p->length =8;/*p->length没有赋初值,添加这一句8<10 可以插入 */ 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; }
whdugh 2013-01-16
  • 打赏
  • 举报
回复
引用 8 楼 JackyRao 的回复:
void Insert(SqList *&A, ElemType x) // *&什么意思啊
俺也没有搞懂啊 关于 *L、*&L和&L到底有何差别,编译时也老出现这样的问题
兆帅 2013-01-16
  • 打赏
  • 举报
回复

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();单字符行输入
仔细看看,,,
JackyRao 2013-01-15
  • 打赏
  • 举报
回复
void Insert(SqList *&A, ElemType x) // *&什么意思啊
小白晒太阳 2013-01-14
  • 打赏
  • 举报
回复
引用 6 楼 whdugh 的回复:
引用 3 楼 Tro_picana 的回复:C/C++ code ? 1234567891011121314151617181920212223242526272829303132333435363738394041 #include <stdio.h> #include <stdlib.h> #define MaxSize 10 typedef c……
个人觉得关于不是1位的数字(0-9)应该没给明确的字符值吧,一般的0-9字符转换ASCII码值为48-57。也就是说你输入10不属于字符,scanf按字符读取时可能就读取了1或是什么。 其实楼主既然定义的ElemType为字符,大可不必考虑用它来排列插入的数字后的顺序。如果你要排列数字,你可以把定义成int的ElemType,就是typedef int ElemType,然后输入输出改成%d不也可以吗?
whdugh 2013-01-13
  • 打赏
  • 举报
回复
引用 3 楼 Tro_picana 的回复:
C/C++ code ? 1234567891011121314151617181920212223242526272829303132333435363738394041 #include <stdio.h> #include <stdlib.h> #define MaxSize 10 typedef char ElemType; typedef struct{ E……
嗯 运行了下 没有问题 太谢谢你了 不过我在输入4个元素: 2 4 6 10 输出:2 4 6 8 1 这个是怎么回事啊
iceheart 2013-01-13
  • 打赏
  • 举报
回复
首选数据结构红黑树,其次链表,最后用数组。使用用数组的话,定位插入位置用2分查找
SKATE11 2013-01-12
  • 打赏
  • 举报
回复
void Insert(SqList *&A, ElemType x) //error1:excepted ';',','or ')'before '&'token.//把&去掉 把“A.”改为“A->”即可
小白晒太阳 2013-01-12
  • 打赏
  • 举报
回复
#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试试看行不
东大坡居士 2013-01-12
  • 打赏
  • 举报
回复

void Insert(SqList *&A, ElemType x)  //error1:excepted ';',','or ')'before '&'token.
//把&去掉即可
囧囧囧1024 2013-01-12
  • 打赏
  • 举报
回复
LZ可以参考参考~

#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;
}

69,368

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧