-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamic-sequential-list.cpp
More file actions
158 lines (147 loc) · 3.11 KB
/
Copy pathdynamic-sequential-list.cpp
File metadata and controls
158 lines (147 loc) · 3.11 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// dynamic sequential list
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
typedef int Elemtype;
typedef struct
{
Elemtype *data;
int length;
}Seqlist;
//initial
Seqlist* initiallist()
{
Seqlist *L = (Seqlist *)malloc(sizeof(Seqlist));
if(L==NULL) return NULL;
L->data = (Elemtype*)malloc(sizeof(Elemtype)*MAXSIZE);
if(L->data == NULL)
{
free(L);
return NULL;
}
L->length = 0;
return L;
}
//append element to the list
int appendElem(Seqlist *L, int append_elem)
{
//check if the list is full
if(L->length>=MAXSIZE)
{
printf("The list is full!\n");
return 0;
}
L->data[L->length] = append_elem;
L->length++;
return 1;
}
//delete element in the list
int deleteElem(Seqlist *L, int pos, Elemtype *e)
{
if(L->length == 0)
{
printf("Empty list!\n");
}
if(pos < 1 || pos > L->length)
{
printf("Can't find thre position!\n");
return 0;
}
*e = L->data[pos-1];// 修正: 使用pos-1而不是L->length
for(int i = pos-1;i<L->length - 1;i++)
{
L->data[i] = L->data[i+1];
}
L->length--;// 修正: 最后不要忘记让表长减1
return 1;
}
//modify element in the list
int modifyElem(Seqlist *L, int pos,int *prev,int next)
{
if(L->length == 0)
{
printf("Empty list!\n");
return 0;
}
if(pos<1 || pos>L->length)
{
printf("Can't fin the position!\n");
return 0;
}
*prev = L->data[pos-1];
L->data[pos-1] = next;
return 1;
}
void print(Seqlist *L)
{
for(int i=0;i<L->length;i++)
{
printf("%d ",L->data[i]);// 修正:使用 i 而不是 L->length
}
printf("\n");
}
//search the position of an element
int searchElem(Seqlist *L,int *search_pos,Elemtype elem)
{
for(int i = 0; i < L->length;i++)
{
if(L->data[i] == elem)
{
*search_pos = ++i;
return 1;
}
}
printf("Search failed!\n");
return 0;
}
int main()
{
// initial
Seqlist* list = initiallist();
if(list->data == NULL){
printf("Memory allocation failed!\n");
return 0;
}
printf("Initial!\nThe list occupies %zu Bytes!\n%d element in the list!\n",sizeof(*list->data)*MAXSIZE,list->length);
// add
appendElem(list,9);
appendElem(list,19);
appendElem(list,29);
print(list);
// delete
Elemtype e;
if(deleteElem(list,3,&e))
{
printf("delete %d\n",e);
}
else
{
printf("delete error\n");
}
print(list);
// modify
Elemtype modify_prev;
if(modifyElem(list,2,&modify_prev,18))
{
printf("18 replace the element %d\n",modify_prev);
}
else
{
printf("Modify error!\n");
}
print(list);
// search
int search_pos;
if(searchElem(list,&search_pos,18))
{
printf("18 is on the position %d in the list\n",search_pos);
}
else
{
printf("Search failed!\n");
}
free(list->data);
free(list);
}
//There are many repeated sentences in main and other functions.
//It just let me debug more convenient.