顺序表的初始化问题

来源:百度知道 编辑:UC知道 时间:2024/05/07 03:22:04
#include<stdio.h>
#include<malloc.h>
#define List_init_size 100
#define Listincrement 10
typedef struct{
char *elem;
int length;
int listsize;
}Sqlist;

int Initlist_Sq(Sqlist &L)
L.elem=(char*)malloc(List_init_size*sizeof(char));
if(!L.elem)return 0;
L.length=0;
L.listsize=List_init_size;
return 1;

main (){

Sqlist L;
int i;
if(!Initlist_Sq(L))
printf("初始化失败,结束\n");
else
{ scanf("%d",&L.length);
for(i=0;i<L.length;i++);
scanf("%c",&L.elem[i]);
}
}
为什么我每次调试都有错误 请高手指点下
楼下的那位大侠 我照你的方法修改了下 貌似现在就两个错误
error C2449: found '{' at file scope (missing function header?)
error C2059: syntax error : '}'
这两个的意思应该是少了一个头``和那个扩号出什么问题了吧
不好意思 我刚刚学习数据结构 看着头晕

int Initlist_Sq(Sqlist &L)
L.elem=(char*)malloc(List_init_size*sizeof(char));
if(!L.elem)return 0;
L.length=0;
L.listsize=List_init_size;
return 1;

这段括号没打.

main里面for(i=0;i<L.length;i++); 最后的分号要去掉

另外,输入字符型数据时要小心,因为回车键实际上也会被当成字符输入.因此,应该在scanf字符数据前清空一下输入缓冲区.应该这样
for(i=0;i<L.length;i++)
{
fflush(stdin);
scanf("%c",&L.elem[i]);
}

//顺序表操作
#include<iostream>
#include<iomanip>
#define M 10
using namespace std;
double *create();
void print(double *a);
void insert(double *a,int i,double x);
void del(double *a,double x);

int main()
{
double *a;
a=create();
insert(a,2,100);
print(a);
del(a,100);
cout<<endl;
print(a);

system("pause");
return 0;
}

//建立顺序表
double *create()
{