operation

1
2
InitList(L): 初始化线性表
ListEmpty(L): 判断线性表是否为空

任务

1
2
3
4
5
/*
1.定义线性表类型的对象L。
2.初始化线性表L,并输出其长度。
3.判断线性表是否为空。
*/

代码实现:

C

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<stdio.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0

#define MAXSIZE 20

typedef int ElemType;//抽象数据类型,是线性表中数据元素的数据类型。
typedef int Status;//抽象数据类型,是函数返回值的类型

typedef struct
{
ElemType data[MAXSIZE];
int length;
}SqList;

Status InitList(SqList L)
{
L.length = 0;
return OK;
}

Status ListEmpty(SqList L){
if(L.length == 0)
return TRUE;
else
return FALSE;
}
int main()
{
SqList L;//定义一个顺序结构的线性表。
Status i;

i = InitList(L);//初始化线性表L。

printf("L.length = %d\n",L.length);//输出线性表的长度。

i = ListEmpty(L);//判断线性表是否为空

printf("L是否为空?i=%d(1:是 0:否)\n",i);
}

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<iostream>
using namespace std;
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0

#define MAXSIZE 20

typedef int ElemType;//抽象数据类型,是线性表中数据元素的数据类型。
typedef int Status;//抽象数据类型,是函数返回值的类型

typedef struct
{
ElemType data[MAXSIZE];
int length;
}SqList;

Status InitList(SqList L)
{
L.length = 0;
return OK;
}

Status ListEmpty(SqList L){
if(L.length == 0)
return TRUE;
else
return FALSE;
}
int main()
{
SqList L;//定义一个顺序结构的线性表。
Status i;

i = InitList(L);//初始化线性表L。

cout<<"L.length = "<<L.length<<endl;//输出线性表的长度。

i = ListEmpty(L);//判断线性表是否为空

cout<<"L是否为空?i="<< i <<"(1:是 0:否)"<<endl;
}