读书人

数据结构中遇到的有关问题error C2016

发布时间: 2013-03-21 10:08:17 作者: rapoo

数据结构中遇到的问题error C2016: C 要求一个结构或联合至少有一个成员
代码是:请大神们赐教
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
struct ElemType{
char cityname[20];
int number;
char introduct[20];
};
typedef struct{
ElemType elem[LIST_INIT_SIZE];
int length;
int listsize;
}SqList;
void CreatList_Sq(SqList *&L,ElemType *a,int n){//建立顺序表
L->elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L->elem) exit(0);
for(int i=0;i<n;i++)
{
strcpy(L->elem[i].cityname,a[i].cityname);
L->elem[i].number=a[i].number;
strcpy(L->elem[i].introduct,a[i].introduct);
}

}
void DisplayList(SqList *&L) //输出线性表
{
for(int i=0;i<L->length;i++)
printf("%s %d %s\n",L->elem[i].cityname,L->elem[i].number,L->elem[i].introduct);
printf("线性表的长度是:%d\n",L->length);
}
void LocateElem(SqList *&L) //查询线性表中的元素
{
int m;
printf("请输入要查询的城市的区号:");
scanf("%d",&m);
for(int i=0;i<L->length;i++)
{
if(m==L->elem[i].number)
printf("%s %d %s\n",L->elem[i].cityname,L->elem[i].number,L->elem[i].introduct);
}
}
int ListInsert(SqList *&L,int i){
//在线性表L中第i个位置前插入新的元素,
//i的合法值为1<=i<=L->length+1
ElemType *newbase;
if(i<1||i>L->length+1)
return 0;
if(L->length>=L->listsize){ //当前存储空间已满,增加分配
newbase=(ElemType *)realloc(L->elem,(L->listsize+LISTINCREMENT)*sizeof(ElemType));
if(!newbase) exit(0);
L->elem=newbase;
L->listsize+=LISTINCREMENT;
}
char n[20]="GuangDong";
int num=5;
char intro[20]="In southern China";
for(int j=L->length;j>i;j--)
{
strcpy(L->elem[j].cityname,L->elem[j-1].cityname);
L->elem[j].number=L->elem[j-1].number;
strcpy(L->elem[j].introduct,L->elem[j-1].introduct);
}
strcpy(L->elem[i].cityname,n);
L->elem[i].number=num;
strcpy(L->elem[i].introduct,intro);
L->length++;
printf("线性表的长度是:%d\n",L->length);
return 0;
}
void DeletList(SqList *&L,int i)
//删除线性表的第i个元素
{
for(int j=i;j<L->length;j++)
{
strcpy(L->elem[j].cityname,L->elem[j+1].cityname);
L->elem[j].number=L->elem[j+1].number;
strcpy(L->elem[j].introduct,L->elem[j+1].introduct);
}
L->length--;
printf("线性表的长度是:%d\n",L->length);
}
void DestroyList(SqList *&L)
{
free(L);
}
void main(){
struct ElemType b[4]={
{"BeiJing",1,"capital city"},
{"WuHan",2,"beautiful"},
{"XiaMen",3,"near sea"},
{"HanDan",4,"my home"}
};
SqList *List;
printf("原线性表中的元素是:\n");
CreatList_Sq(List,b,4);


DisplayList(List);
LocateElem(List);
printf("增加一个元素后的线性表是:\n");
ListInsert(List,3);
printf("删除第2个元素后的线性表是:\n");
DeletList(List,2);
DestroyList(List);
system("pause");
} 数据结构 c struct
[解决办法]
我觉得你这几处有问题:
1.typedef struct{
ElemType elem[LIST_INIT_SIZE];
int length;
int listsize;
}SqList;
指定了elem数组的长度,在定义SqList变量的时候就分配内存,你后面又用
malloc分配内存,重复。

2. CreatList_Sq(List,b,4);
传递变量,不如传递指针,CreateList()函数内部使用L->elem,说明传入的应该是指针。

3. void CreatList_Sq(SqList *&L,ElemType *a,int n)
SqList * &L此处“*”和“&”的用法有问题。

我改了一下程序,运行正常。

#include "stdafx.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <malloc.h>

#define LIST_INIT_SIZE 10
#define LISTINCREMENT 2

struct ElemType{
char cityname[20];
int number;
char introduct[20];
};
typedef struct{
int length;
int listsize;
ElemType *elem/*[LIST_INIT_SIZE]*/;
}SqList;

void CreatList_Sq(SqList * L,ElemType *a,int n){//建立顺序表
L->elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L->elem) exit(0);
for(int i=0;i<n;i++)
{
strcpy(L->elem[i].cityname,a[i].cityname);
L->elem[i].number=a[i].number;
strcpy(L->elem[i].introduct,a[i].introduct);
}
L->length = n;
}
void DisplayList(SqList * L) //输出线性表
{
for(int i=0;i<L->length;i++)
printf("%s %d %s\n",L->elem[i].cityname,L->elem[i].number,L->elem[i].introduct);
printf("线性表的长度是:%d\n",L->length);
}

void LocateElem(SqList * L) //查询线性表中的元素
{
int m;
printf("请输入要查询的城市的区号:");
scanf("%d",&m);
for(int i=0;i<L->length;i++)
{
if(m==L->elem[i].number)
printf("%s %d %s\n",L->elem[i].cityname,L->elem[i].number,L->elem[i].introduct);
}
}

int ListInsert(SqList *L,int i){
//在线性表L中第i个位置前插入新的元素,
//i的合法值为1<=i<=L->length+1
ElemType *newbase;
if(i<1
[解决办法]
i>L->length+1)
return 0;
if(L->length>=L->listsize){ //当前存储空间已满,增加分配
newbase=(ElemType *)realloc(L->elem,(L->listsize+LISTINCREMENT)*sizeof(ElemType));
if(!newbase) exit(0);
L->elem=newbase;
L->listsize+=LISTINCREMENT;
}
char n[20]="GuangDong";
int num=5;
char intro[20]="In southern China";
for(int j=L->length;j>i;j--)
{
strcpy(L->elem[j].cityname,L->elem[j-1].cityname);
L->elem[j].number=L->elem[j-1].number;
strcpy(L->elem[j].introduct,L->elem[j-1].introduct);
}
strcpy(L->elem[i].cityname,n);


L->elem[i].number=num;
strcpy(L->elem[i].introduct,intro);
L->length++;
printf("线性表的长度是:%d\n",L->length);
return 0;
}

void DeletList(SqList *L,int i)
//删除线性表的第i个元素
{
for(int j=i;j<L->length;j++)
{
strcpy(L->elem[j].cityname,L->elem[j+1].cityname);
L->elem[j].number=L->elem[j+1].number;
strcpy(L->elem[j].introduct,L->elem[j+1].introduct);
}
L->length--;
printf("线性表的长度是:%d\n",L->length);
}

void DestroyList(SqList *L)
{
printf("Destroy .\n");
free(L->elem);
}

int _tmain(int argc, _TCHAR* argv[])
{
struct ElemType b[4]={
{"BeiJing",1,"capital city"},
{"WuHan",2,"beautiful"},
{"XiaMen",3,"near sea"},
{"HanDan",4,"my home"}
};
SqList List;

printf("原线性表中的元素是:\n");
CreatList_Sq(&List,b,4);
DisplayList(&List);
LocateElem(&List);
printf("增加一个元素后的线性表是:\n");
ListInsert(&List,3);
printf("删除第2个元素后的线性表是:\n");
DeletList(&List,2);
DestroyList(&List);
return 0;
}



你可以试一下。

读书人网 >C语言

热点推荐