c语言数据结构的问题,困扰已久,寻求指导!
一个顺序表的建立程序,在vc6下编辑的!在仔细查找了若干遍后还是没发现错误的原因,希望高手给指点迷津!!
#include <stdio.h>
#include <stdlib.h>
#define OVERFLOW -2
#define OK 1
#define ERROR 0
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef int ElemType;
typedef int Status;
typedef struct
{
ElemType *elem;
int length;
int listsize;
}SqList;
Status InitList_Sq(SqList &L)
{
L.elem=(ElemType *)malloc (LIST_INIT_SIZE*sizeof(ElemType));
if(!L.elem)
exit(ERROR);
L.length=0;
L.listsize=LIST_INIT_SIZE;
return OK;
}
void CreateList(SqList &L,int len)
{
if(len>LIST_INIT_SIZE)
{
L.elem=(ElemType *)malloc (L.elem,len*sizeof(ElemType));
L.listsize=len;
}
printf("请输入顺序表元素:\n");
for(int i=0;i<=len-1;i++)
{
scanf("%d",L.elem[i]);
}
L.length=len;
printf("建立顺序表为:\n");
for(i=0;i<=len-1;i++)
{
printf("%d",L.elem[i]);
}
printf("\n顺序表一共%d个元素。\n",L.length);
}
void main()
{
SqList L;
int Sqlen;
if(!InitList_Sq(L))
{
printf("初始化顺序表失败!\n");
exit(ERROR);
}
printf("输入顺序表个数:");
scanf("%d",Sqlen);
CreateList(L,Sqlen);
}
错误提示如下:
C:\Program Files\Microsoft Visual Studio\MyProjects\file1\file1.c(18) : error C2143: syntax error : missing ')' before '&'
C:\Program Files\Microsoft Visual Studio\MyProjects\file1\file1.c(18) : error C2143: syntax error : missing '{' before '&'
C:\Program Files\Microsoft Visual Studio\MyProjects\file1\file1.c(18) : error C2059: syntax error : '&'
C:\Program Files\Microsoft Visual Studio\MyProjects\file1\file1.c(18) : error C2059: syntax error : ')'
C:\Program Files\Microsoft Visual Studio\MyProjects\file1\file1.c(28) : error C2143: syntax error : missing ')' before '&'
C:\Program Files\Microsoft Visual Studio\MyProjects\file1\file1.c(28) : error C2143: syntax error : missing '{' before '&'
C:\Program Files\Microsoft Visual Studio\MyProjects\file1\file1.c(28) : error C2059: syntax error : '&'
C:\Program Files\Microsoft Visual Studio\MyProjects\file1\file1.c(28) : error C2059: syntax error : ')'
C:\Program Files\Microsoft Visual Studio\MyProjects\file1\file1.c(52) : warning C4013: 'InitList_Sq' undefined; assuming extern returning int
C:\Program Files\Microsoft Visual Studio\MyProjects\file1\file1.c(59) : warning C4013: 'CreateList' undefined; assuming extern returning int
[解决办法]
L.elem=(ElemType *)malloc (L.elem,len*sizeof(ElemType));
==》
L.elem=(ElemType *)malloc (len*sizeof(ElemType));
[解决办法]
//C语言输入时候scanf("%d",&i)#include <stdio.h>#include <stdlib.h>#define OVERFLOW -2#define OK 1#define ERROR 0#define LIST_INIT_SIZE 100#define LISTINCREMENT 10typedef int ElemType;typedef int Status;typedef struct{ ElemType *elem; int length; int listsize;}SqList;Status InitList_Sq(SqList &L){ L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType)); if(!L.elem) exit(ERROR); L.length=0; L.listsize=LIST_INIT_SIZE; return OK;}void CreateList(SqList &L,int len){ int i; if(len>LIST_INIT_SIZE) { L.elem=(ElemType *)realloc(L.elem,len*sizeof(ElemType)); //此时应该重新分配内存 L.listsize=len; } printf("请输入顺序表元素:\n"); for(i=0;i<=len-1;i++) //i放在外面定义 { scanf("%d",&L.elem[i]); } L.length=len; printf("建立顺序表为:\n"); for(i=0;i<=len-1;i++) { printf("%-3d",L.elem[i]); } printf("\n顺序表一共%d个元素。\n",L.length);}int main(){ SqList L; int Sqlen; if(!InitList_Sq(L)) { printf("初始化顺序表失败!\n"); exit(ERROR); } printf("输入顺序表个数:"); scanf("%d",&Sqlen); //这里要加&, CreateList(L,Sqlen); return 0;}
[解决办法]
发我qq或文件,我给你调试,先记下
[解决办法]
我运行就一个问题啊:
realloc()函数使用错误,
这个函数有两个参数一个指针,一个是大小,
指针应该指向想扩展的变量。
所以应该是:
realloc(&L,len*sizeof(ElemType));
关于realloc()的用法可以看一下:
http://blog.csdn.net/qq7959501/archive/2009/10/11/4650150.aspx
[解决办法]
#include <stdlib.h>
#include<stdio.h>
#define OVERFLOW -2
#define OK 1
#define ERROR 0
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef int ElemType;
typedef int Status;
typedef struct
{
ElemType *elem;
int length;
int listsize;
}SqList;
Status InitList_Sq(SqList &L)
{
L.elem=(ElemType *)malloc (LIST_INIT_SIZE*sizeof(ElemType));
if(!L.elem)
exit(ERROR);
L.length=0;
L.listsize=LIST_INIT_SIZE;
return OK;
}
void CreateList(SqList &L,int len)//主要就是上面这两处
{
if(len>LIST_INIT_SIZE)
{
L.elem=(ElemType *)realloc(L.elem,len*sizeof(ElemType));
L.listsize=len;
}
printf("请输入顺序表元素:\n");
for(int i=0;i<=len-1;i++)
{
scanf("%d",&L.elem[i]);
}
L.length=len;
printf("建立顺序表为:\n");
for(i=0;i<=len-1;i++)
{
printf("%d",L.elem[i]);
}
printf("\n顺序表一共%d个元素。\n",L.length);
}
void main()
{
SqList L;
int Sqlen;
if(!InitList_Sq(L))
{
printf("初始化顺序表失败!\n");
exit(ERROR);
}
printf("输入顺序表个数:");
scanf("%d",&Sqlen);
CreateList(L,Sqlen);
}