C语言数据结构初学者,codeblocks报错
#include<stdio.h>#include<malloc.h>#include<stdlib.h>#defineTRUE1#defineFALSE0#define...
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define list_init_size 100
//线性表存储空间的初始分配量
#define LISTINCREMENT 10
//线性表存储空间的分配增量
typedef int Status;
typedef int ElemType;
typedef struct{
ElemType *elem;
//存储空间基址
int length;
//当前长度
int listsize;
//当前分配的存储容量(以sizeof(ElemType)为单位)
}SqList;
Status InitList_Sq(SqList &L){
//构造一个空的线性表L
L.elem =(ElemType * )malloc(list_init_size*sizeof(ElemType));
if(!L.elem )exit(OVERFLOW);
//存储分配失败
L.length =0;
//空表长度为0
L.listsize =list_init_size;//初始存储容量
return OK;
}
//Initlist_Sq
Status ListInsert_Sq(SqList &L,int i,ElemType e){
//在顺序线性表L中第i个位置之前插入新的元素e,
//i的合法值为1<=i<=ListLength_Sq(L)+1
ElemType *p,*q,*newbase;
//定义指针
if(i<1||i>L.length +1)
return ERROR;
//i值不合法
if(L.length >=L.listsize ){
//当前存储空间已满,增加分配
newbase=(ElemType * )realloc(L.elem ,(L.listsize +LISTINCREMENT)*sizeof(ElemType));
if(i<=L.length)
return i;
else return 0;
}
//LocateElem_Sq
void MergeList_Sq(SqList La,SqList Lb,SqList &Lc ){
//已知顺序线性表La和Lb的元素按值非递减排列
//归并La和Lb得到新的顺序线性表Lc,Lc的元素也按非递减排列
ElemType *pa,*pb,*pc,*pa_last,*pb_last;
pa=La.elem ;
pb=Lb.elem ;
Lc.listsize =Lc.length =La.length +Lb.length
pc=Lc.elem =(ElemType *)malloc(Lc.listsize *sizeof(ElemType));
if(!Lc.elem )exit(OVERFLOW); //存储分配失败
pa_last=La.elem +La.length -1;
pb_last=Lb.elem +Lb.length -1;
while(pa<=pa_last && pb<=pb_last) {
//归并 if(*pa<=*pb)
*pc++=*pa++;
else
*pc++=*pb++;
}
while(pa<=pa_last)
*pc++=*pa++;
//插入La的剩余元素
while(pb<=pb_last)
*pc++=*pb++;
//插入Lb的剩余元素
}
//MergeList_Sq
void main() {
SqList La,Lb,Lc;
InitList_Sq(La);
ListInsert_Sq(La,1,3);
ListInsert_Sq(La,2,5);
ListInsert_Sq(La,3,8);
ListInsert_Sq(La,4,11);
printf("La插入后:\n");
display(La);
InitList_Sq(Lb);
ListInsert_Sq(Lb,1,2);
ListInsert_Sq(Lb,2,6);
ListInsert_Sq(Lb,3,8);
ListInsert_Sq(Lb,4,9);
ListInsert_Sq(Lb,5,11);
ListInsert_Sq(Lb,6,15);
ListInsert_Sq(Lb,7,20);
printf("Lb插入后:\n");
display(Lb);
MergeList_Sq(La,Lb,Lc);
printf("归并后:\n");
display(Lc);
printf("\n");
int a=LocateElem_Sq( Lc, 5);
printf("%d\n",a);
}
||=== Build: Debug in 2.3 (compiler: GNU GCC Compiler) ===|
F:\ccc\2.3\main.c|24|error: expected ';', ',' or ')' before '&' token|
F:\ccc\2.3\main.c|35|error: expected ';', ',' or ')' before '&' token|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===| 展开
#include <malloc.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define list_init_size 100
//线性表存储空间的初始分配量
#define LISTINCREMENT 10
//线性表存储空间的分配增量
typedef int Status;
typedef int ElemType;
typedef struct{
ElemType *elem;
//存储空间基址
int length;
//当前长度
int listsize;
//当前分配的存储容量(以sizeof(ElemType)为单位)
}SqList;
Status InitList_Sq(SqList &L){
//构造一个空的线性表L
L.elem =(ElemType * )malloc(list_init_size*sizeof(ElemType));
if(!L.elem )exit(OVERFLOW);
//存储分配失败
L.length =0;
//空表长度为0
L.listsize =list_init_size;//初始存储容量
return OK;
}
//Initlist_Sq
Status ListInsert_Sq(SqList &L,int i,ElemType e){
//在顺序线性表L中第i个位置之前插入新的元素e,
//i的合法值为1<=i<=ListLength_Sq(L)+1
ElemType *p,*q,*newbase;
//定义指针
if(i<1||i>L.length +1)
return ERROR;
//i值不合法
if(L.length >=L.listsize ){
//当前存储空间已满,增加分配
newbase=(ElemType * )realloc(L.elem ,(L.listsize +LISTINCREMENT)*sizeof(ElemType));
if(i<=L.length)
return i;
else return 0;
}
//LocateElem_Sq
void MergeList_Sq(SqList La,SqList Lb,SqList &Lc ){
//已知顺序线性表La和Lb的元素按值非递减排列
//归并La和Lb得到新的顺序线性表Lc,Lc的元素也按非递减排列
ElemType *pa,*pb,*pc,*pa_last,*pb_last;
pa=La.elem ;
pb=Lb.elem ;
Lc.listsize =Lc.length =La.length +Lb.length
pc=Lc.elem =(ElemType *)malloc(Lc.listsize *sizeof(ElemType));
if(!Lc.elem )exit(OVERFLOW); //存储分配失败
pa_last=La.elem +La.length -1;
pb_last=Lb.elem +Lb.length -1;
while(pa<=pa_last && pb<=pb_last) {
//归并 if(*pa<=*pb)
*pc++=*pa++;
else
*pc++=*pb++;
}
while(pa<=pa_last)
*pc++=*pa++;
//插入La的剩余元素
while(pb<=pb_last)
*pc++=*pb++;
//插入Lb的剩余元素
}
//MergeList_Sq
void main() {
SqList La,Lb,Lc;
InitList_Sq(La);
ListInsert_Sq(La,1,3);
ListInsert_Sq(La,2,5);
ListInsert_Sq(La,3,8);
ListInsert_Sq(La,4,11);
printf("La插入后:\n");
display(La);
InitList_Sq(Lb);
ListInsert_Sq(Lb,1,2);
ListInsert_Sq(Lb,2,6);
ListInsert_Sq(Lb,3,8);
ListInsert_Sq(Lb,4,9);
ListInsert_Sq(Lb,5,11);
ListInsert_Sq(Lb,6,15);
ListInsert_Sq(Lb,7,20);
printf("Lb插入后:\n");
display(Lb);
MergeList_Sq(La,Lb,Lc);
printf("归并后:\n");
display(Lc);
printf("\n");
int a=LocateElem_Sq( Lc, 5);
printf("%d\n",a);
}
||=== Build: Debug in 2.3 (compiler: GNU GCC Compiler) ===|
F:\ccc\2.3\main.c|24|error: expected ';', ',' or ')' before '&' token|
F:\ccc\2.3\main.c|35|error: expected ';', ',' or ')' before '&' token|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===| 展开
展开全部
修改了语法错误,至少可以编译通过
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define list_init_size 100
/* 线性表存储空间的初始分配量 */
#define LISTINCREMENT 10
/* 线性表存储空间的分配增量 */
typedef int Statuss;
typedef int ElemType;
typedef struct {
ElemType *elem;
/* 存储空间基址 */
int length;
/* 当前长度 */
int listsize;
/* 当前分配的存储容量(以sizeof(ElemType)为单位) */
}SqList;
int InitList_Sq( SqList L )
{
/* 构造一个空的线性表L */
L.elem = (ElemType * ) malloc( list_init_size * sizeof(ElemType) );
if ( !L.elem )
exit( OVERFLOW );
/* 存储分配失败 */
L.length = 0;
/* 空表长度为0 */
L.listsize = list_init_size; /* 初始存储容量 */
return(OK);
};
/* Initlist_Sq */
int ListInsert_Sq( SqList L, int i, ElemType e )
{
/*
* 在顺序线性表L中第i个位置之前插入新的元素e,
* i的合法值为1<=i<=ListLength_Sq(L)+1
*/
ElemType *p, *q, *newbase;
/* 定义指针 */
if ( i < 1 || i > L.length + 1 )
return(ERROR);
/* i值不合法 */
if ( L.length >= L.listsize )
{
/* 当前存储空间已满,增加分配 */
newbase = (ElemType * ) realloc( L.elem, (L.listsize + LISTINCREMENT) * sizeof(ElemType) );
if ( i <= L.length )
return(i);
else return(0);
}
}
/* LocateElem_Sq */
void MergeList_Sq( SqList La, SqList Lb, SqList Lc )
{
/*
* 已知顺序线性表La和Lb的元素按值非递减排列
* 归并La和Lb得到新的顺序线性表Lc,Lc的元素也按非递减排列
*/
ElemType *pa, *pb, *pc, *pa_last, *pb_last;
pa = La.elem;
pb = Lb.elem;
Lc.listsize = Lc.length = La.length + Lb.length;
pc = Lc.elem = (ElemType *) malloc( Lc.listsize * sizeof(ElemType) );
if ( !Lc.elem )
exit( OVERFLOW ); /* 存储分配失败 */
pa_last = La.elem + La.length - 1;
pb_last = Lb.elem + Lb.length - 1;
while ( pa <= pa_last && pb <= pb_last )
{
/* 归并*/
if(*pa<=*pb)
*pc++ = *pa++;
else
*pc++ = *pb++;
}
while ( pa <= pa_last )
*pc++ = *pa++;
/* 插入La的剩余元素 */
while ( pb <= pb_last )
*pc++ = *pb++;
/* 插入Lb的剩余元素 */
}
/* MergeList_Sq */
void main()
{
SqList La, Lb, Lc;
InitList_Sq( La );
ListInsert_Sq( La, 1, 3 );
ListInsert_Sq( La, 2, 5 );
ListInsert_Sq( La, 3, 8 );
ListInsert_Sq( La, 4, 11 );
printf( "La插入后:\n" );
//display( La );
InitList_Sq( Lb );
ListInsert_Sq( Lb, 1, 2 );
ListInsert_Sq( Lb, 2, 6 );
ListInsert_Sq( Lb, 3, 8 );
ListInsert_Sq( Lb, 4, 9 );
ListInsert_Sq( Lb, 5, 11 );
ListInsert_Sq( Lb, 6, 15 );
ListInsert_Sq( Lb, 7, 20 );
printf( "Lb插入后:\n" );
//display( Lb );
MergeList_Sq( La, Lb, Lc );
printf( "归并后:\n" );
//display( Lc );
printf( "\n" );
//int a = LocateElem_Sq( Lc, 5 );
//printf( "%d\n", a );
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |