C语言顺序表算法

来源:百度知道 编辑:UC知道 时间:2024/06/06 06:29:10
这个算法是练习本给出的,要填写部分代码

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define OVERFLOW -2
#define OK 1
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef struct
{
int *elem;
int length;
int listsize;
}SqList;
int InitList_Sq(SqList&L,int n){

//创建含有n个元素的顺序表
//在算法2.3的基础上插入n个元素
//请补全代码
☆☆☆☆☆这里就是要我填的
}
int Load_Sq(SqList&L){
int i;
for(__________________☆☆☆☆要填的)
printf("%d",l.elem[i]);
printf("\n");
return OK;
}
void main()
{
SqList T;
int n;
scanf("%d",&n);
if(InitList_Sq(T,n))
{
printf("The List is:");
Load_Sq(T);
}
}

这个是里面提到的算发2.3

Status InitList_Sq(SqList&L){
L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L.elem)exit(OVERFLOW);
L.length=0;

空格很简单,只要在Load_Sq()函数中的第一个printf语句的格式控制符中加入一个空格即可,修改如下:
//---------------------------------------------------------------------------
#include<stdio.h>
#include<stdlib.h>
#define OVERFLOW -2
#define OK 1
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef int ElemType;

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

int InitList_Sq(SqList *L,int n)
{
int i;
L->elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(L->elem==NULL)exit(OVERFLOW);
L->length=0;
L->listsize=LIST_INIT_SIZE;
if (n>LIST_INIT_SIZE) {
exit(OVERFLOW);
}
for (i=0; i<n; ++i) {
scanf("%d",&(L->elem[i]));
++(L->length);
}
return OK;
}

int Load_Sq(SqList *L){
int i;
for(i=0;i<L->length;++i)
printf("%d ",L->elem[i]);/*注意这里*/
printf(&