读书人

帮小弟我看一下该如何填空(C代码)

发布时间: 2012-03-22 17:43:57 作者: rapoo

帮我看一下该怎么填空(C代码)
[code=C/C++][/code]
#include<stdio.h>
#include<malloc.h>
#define OK 1
#define ERROR 0
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define ElemType int

typedef struct
{
int *elem;
int length;
int listsize;
}SqList;

int InitList_Sq(SqList &L)
{
// 构造一个空的线性表L,该线性表预定义大小为LIST_INIT_SIZE
// 请补全代码

}

int Load_Sq(SqList &L)
{
// 输出顺序表中的所有元素
int i;
if(_________________________) printf("The List is empty!"); // 请填空
else
{
printf("The List is: ");
for(_________________________) printf("%d ",_________________________); // 请填空
}
printf("\n");
return OK;
}

int ListInsert_Sq(SqList &L,int i,int e)
{
// 在顺序线性表L中第i个位置之前插入新的元素e
// i的合法值为1≤i≤L.length +1
// 请补全代码

}

int ListDelete_Sq(SqList &L,int i, int &e)
{
// 在顺序线性表L中删除第i个位置的元素,并用e返回其值
// i的合法值为1≤i≤L.length
// 请补全代码

}

int main()
{
SqList T;
int a, i;
ElemType e, x;
if(_________________________) // 判断顺序表是否创建成功
{
printf("A Sequence List Has Created.\n");
}
while(1)
{
printf("1:Insert element\n2:Delete element\n3:Load all elements\n0:Exit\nPlease choose:\n");
scanf("%d",&a);
switch(a)
{
case 1: scanf("%d%d",&i,&x);
if(_________________________) printf("Insert Error!\n"); // 判断i值是否合法,请填空
else printf("The Element %d is Successfully Inserted!\n", x);
break;
case 2: scanf("%d",&i);
if(_________________________) printf("Delete Error!\n"); // 判断i值是否合法,请填空
else printf("The Element %d is Successfully Deleted!\n", e);
break;
case 3: Load_Sq(T);
break;
case 0: return 1;
}
}
}




[解决办法]
http://hi.baidu.com/wei_wei_qiang/blog/item/78cfa31f7ebc1a0b413417ae.html
[解决办法]
#include "stdafx.h"

#include<stdio.h>
#include<malloc.h>
#define OK 1
#define ERROR 0
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define ElemType int

typedef struct
{
int *elem;
int length;
int listsize;
}SqList;

int InitList_Sq(SqList &L)
{
L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L.elem)
return ERROR;
L.length=0;
L.listsize=LIST_INIT_SIZE;
return OK;
}

int Load_Sq(SqList &L)
{
int i;
if(L.length==0) printf("The List is empty!");
else
{
printf("The List is: ");
for(i=0;i<L.length;i++) printf("%d ",L.elem[i]);
}
printf("\n");
return OK;
}

int ListInsert_Sq(SqList &L,int i,int e)
{
int *newbase,*p,*q;
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) return ERROR;
L.elem=newbase;
L.listsize+=LISTINCREMENT;
}
q=&(L.elem[i-1]);
for(p=&(L.elem[L.length-1]);p>=q;--p)
*(p+1)=*p;
*q=e;
++L.length;
return OK;
}

int ListDelete_Sq(SqList &L,int i, int &e)
{
int *p,*q;
if(i<1||i>L.length)
return 0;
p=&(L.elem[i-1]);
e=*p;


q=L.elem+L.length-1;
for(++p;p<=q;++p)
*(p-1)=*p;
--L.length;
return OK;
}

int main()
{
SqList T;
int a, i;
ElemType e, x;
if(InitList_Sq(T)) // ÅжÏ˳Ðò±íÊÇ·ñ´´½¨³É¹¦
{
printf("A Sequence List Has Created.\n");
}
while(1)
{
printf("1:Insert element\n2:Delete element\n3:Load all elements\n0:Exit\nPlease choose:\n");
scanf("%d",&a);
switch(a)
{
case 1: scanf("%d%d",&i,&x);
if(!ListInsert_Sq(T,i,x))printf("Insert Error!\n");
else printf("The Element %d is Successfully Inserted!\n", x);
break;
case 2: scanf("%d",&i);
if(!ListDelete_Sq(T,i,e))printf("Delete Error!\n");
else printf("The Element %d is Successfully Deleted!\n",e);
break;
case 3: Load_Sq(T);
break;
case 0: return 1;
}
}
}

C/C++ code
VC6下可以编译通过
[解决办法]
有错误应该是你的编译器即vc++有问题

读书人网 >C++

热点推荐