-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathch2_seqlist.cpp
More file actions
79 lines (61 loc) · 1.15 KB
/
ch2_seqlist.cpp
File metadata and controls
79 lines (61 loc) · 1.15 KB
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
// Example program
#include <stdio.h>
#include <stdlib.h>
#define maxsize 1000
typedef float datatype;
typedef struct
{
datatype data[maxsize];
int last;
}SeqList;
SeqList *init_seqlist()
{
SeqList *L;
L = (SeqList*)malloc(sizeof(SeqList));
L->last = -1;
return L;
}
int insert_seqlist(SeqList *L, int i, datatype e)
{
int j;
if (L->last == maxsize - 1)
{
printf("list is already full\n");
return -1;
}
if (i<1 || i>L->last+2 )
{
printf("wrong position\n");
return -1;
}
for (j=L->last; j>=i-1; --j)
{
L->data[j+1] = L->data[j];
}
L->data[i-1] = e;
L->last++;
return 1;
}
void print_seqlist(SeqList *L)
{
int i;
for (i=1; i<L->last; ++i)
{
printf("%f -> ", L->data[i-1]);
}
printf("%f\n", L->data[L->last]);
}
int main()
{
int i = 0;
SeqList* L = init_seqlist();
for (i=0; i<10; ++i)
{
insert_seqlist(L, i+1, i*i);
}
printf("list len = %d\n", L->last+1);
print_seqlist(L);
insert_seqlist(L, 5, 222);
print_seqlist(L);
return 1;
}