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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
| #include "stdio.h"
#include "stdlib.h"
#include "math.h"
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
/*In c, there there is no "TRUE" or "FALSE"*/
#define MAXSIZE 20
typedef int Status;
typedef int ElemType;
/*define a struct*/
typedef struct {
ElemType data[MAXSIZE];
int length;
}SqList;
/*initial the sequential list, as we can, L is a pointer*/
Status InitList(SqList *L) {
L->length=0;
return OK;
}
/*test if the initial sequential list is empty, not here the argument "L" is not a pointer*/
Status ListEmpty(SqList L) {
if(L.length==0)
return TRUE;
else
return FALSE;
}
/*if not empty, clean it*/
Status ClearList(SqList *L) {
L->length=0;
return OK;
}
/*if alreay exists, return its length*/
int ListLength(SqList L) {
return L.length;
}
/*get the i th element, return its value using e*/
Status GetElem(SqList L,int i,ElemType *e) {
if(L.length==0 || i<1 || i>L.length)
return ERROR;
*e=L.data[i-1];
return OK;
}
/*find the element e*/
int LocateElem(SqList L,ElemType e) {
int i;
if (L.length==0)
return 0;
for(i=0;i<L.length;i++)
{
if (L.data[i]==e)
break;
}
if(i>=L.length)
return 0;
return i+1;
}
/*if L already exists, insert element e in front of element i, add the length of L by 1*/
Status ListInsert(SqList *L,int i,ElemType e)
{
int k;
if (L->length==MAXSIZE)
return ERROR;
if (i<1 || i>L->length+1)
return ERROR;
if (i<=L->length) {
for(k=L->length-1;k>=i-1;k--)
L->data[k+1]=L->data[k];
}
L->data[i-1]=e;
L->length++;
return OK;
}
/*delete the i th element of L, returing its value using e, subtract the length by 1*/
Status ListDelete(SqList *L,int i,ElemType *e)
{
int k;
if (L->length==0)
return ERROR;
if (i<1 || i>L->length)
return ERROR;
*e=L->data[i-1];
if (i<L->length) {
for(k=i;k<L->length;k++)
L->data[k-1]=L->data[k];
}
L->length--;
return OK;
}
/*print out an element in L*/
Status visit(ElemType c) {
printf("%d ",c);
return OK;
}
/*if L already exists, using the visit function defined above to traverse L and print out the whole List*/
Status ListTraverse(SqList L){
int i;
for(i=0;i<L.length;i++)
visit(L.data[i]);
printf("\n");
return OK;
}
/*After all these steps above, now we will define our "main" function. */
int main() {
SqList L;
ElemType e;
Status i;
/*and blabla*/
}
|