Skip to content

Commit 5bf1fe9

Browse files
committed
update clang
1 parent 0cf2d4c commit 5bf1fe9

File tree

5 files changed

+83
-12
lines changed

5 files changed

+83
-12
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
所有代码都经过具体测试运行,具体环境为:
66
- 操作系统:ubuntu14.04 64位
7-
- Go语言版本: Go 1.3.1
7+
- Go语言版本: Go 1.4
88
- Javascript执行环境: Nodejs 0.11.14
9-
- Java版本: Java 1.7.0_65
9+
- Java版本: Java 1.8.0_25
1010
- C语言编译器: gcc 4.8.2
1111

1212

@@ -20,7 +20,9 @@
2020

2121

2222
### 列表
23-
列表是非常常见的一种数据结构,比如日常所见的购物清单、待办事项等等。它提供了对列表数据的一系列操作,比如:添加、删除、修改、遍历等功能。当我们把这样的具体问题抽象成用列表去解决的时候,往往可以简化问题。
23+
列表是非常常见的一种数据结构,比如日常所见的购物清单、待办事项等等。
24+
它提供了对列表数据的一系列操作,比如:添加、删除、修改、遍历等功能。
25+
当我们把这样的具体问题抽象成用列表去解决的时候,往往可以简化问题。
2426

2527
具体实现: [Go]()[Javascript]()[Java]()[C]()
2628

c/arraylist/arraylist.c

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ ArrayList *listCreate() {
2020
list->get = listGet;
2121
list->set = listSet;
2222
list->append = listAppend;
23+
list->insert = listInsert;
24+
list->remove = listRemove;
25+
list->clear = listClear;
26+
list->destory = listDestory;
2327

2428
return list;
2529
}
@@ -52,8 +56,34 @@ void listAppend(ArrayList *list, void *value) {
5256
list->dataStore[list->theSize++] = value;
5357
}
5458

59+
void listInsert(ArrayList *list, unsigned int index, void *value) {
60+
assert(index < list->theSize);
61+
listIsRealloc(list);
62+
63+
memmove(list->dataStore+index+1, list->dataStore+index,
64+
sizeof(void *) * (list->theSize-index));
65+
66+
list->dataStore[index] = value;
67+
list->theSize++;
68+
}
5569

70+
void listRemove(ArrayList *list, unsigned int index) {
71+
assert(index < list->theSize);
72+
memmove(list->dataStore+index, list->dataStore+index+1,
73+
sizeof(void *) * (list->theSize-index-1));
74+
75+
list->theSize--;
76+
}
5677

78+
void listClear(ArrayList *list) {
79+
free(list->dataStore);
80+
list->dataStore = malloc(sizeof(void *) * ARRAYLIST_DEAUFALT_SIZE);
81+
assert(list->dataStore);
82+
list->theSize = 0;
83+
list->capacity = ARRAYLIST_DEAUFALT_SIZE;
84+
}
5785

58-
59-
86+
void listDestory(ArrayList *list) {
87+
free(list->dataStore);
88+
free(list);
89+
}

c/arraylist/arraylist.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ typedef struct ArrayList {
1010
void *(*get)(struct ArrayList *, unsigned int);
1111
void (*set)(struct ArrayList *, unsigned int, void *);
1212
void (*append)(struct ArrayList *, void *);
13-
//void (*insertAfter)(ArrayList *, unsigned int, void *);
14-
//void (*remove)(ArrayList *, unsigned int);
15-
//void (*clear)(ArrayList *);
13+
void (*insert)(struct ArrayList *, unsigned int, void *);
14+
void (*remove)(struct ArrayList *, unsigned int);
15+
void (*clear)(struct ArrayList *);
16+
void (*destory)(struct ArrayList *);
1617
} ArrayList;
1718

1819
/* Prototypes */
@@ -21,8 +22,9 @@ int listSize(ArrayList *list);
2122
void *listGet(ArrayList *list, unsigned int index);
2223
void listSet(ArrayList *list, unsigned int index, void *value);
2324
void listAppend(ArrayList *list, void *value);
24-
void listInsertAfter(ArrayList *list, unsigned int index, void *value);
25+
void listInsert(ArrayList *list, unsigned int index, void *value);
2526
void listRemove(ArrayList *list, unsigned int index);
2627
void listClear(ArrayList *list);
28+
void listDestory(ArrayList *list);
2729

2830
#endif /* __ARRAYLIST_H__ */

c/arraylist/arraylist_test.c

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include "arraylist.c"
44

55
int main() {
6+
printf("\nRunning arraylist.c tests.\n\n");
7+
68
ArrayList *list = listCreate();
79
assert(list->size(list) == 0);
810

@@ -27,7 +29,42 @@ int main() {
2729
int item2_new = *(int *)list->get(list, 1);
2830
assert(item2_new == 2);
2931

30-
printf("\n ArrayList test pass...\n\n");
32+
list->destory(list);
33+
list = NULL;
34+
assert(!list);
35+
36+
int a1 = 1;
37+
int a2 = 2;
38+
int a3 = 3;
39+
int a4 = 4;
40+
ArrayList *list2 = listCreate();
41+
list2->append(list2, &a1);
42+
list2->append(list2, &a2);
43+
list2->append(list2, &a3);
44+
list2->insert(list2, 2, &a4);
45+
assert(list2->size(list2) == 4);
46+
47+
int r1 = *(int *)list2->get(list2, 0);
48+
int r2 = *(int *)list2->get(list2, 1);
49+
int r3 = *(int *)list2->get(list2, 2);
50+
int r4 = *(int *)list2->get(list2, 3);
51+
assert(r1 == 1);
52+
assert(r2 == 2);
53+
assert(r3 == 4);
54+
assert(r4 == 3);
55+
56+
list2->remove(list2, 0);
57+
r1 = *(int *)list2->get(list2, 0);
58+
assert(r1 == 2);
59+
assert(list2->size(list2) == 3);
60+
61+
list2->clear(list2);
62+
assert(list2->size(list2) == 0);
63+
64+
list2->destory(list2);
65+
list2 = NULL;
66+
assert(!list2);
67+
68+
printf("All arraylist.c tests completed.\n\n");
3169
return 0;
3270
}
33-

java/arraylist/MyArrayList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
public class MyArrayList<AnyType> {
77
private static final int DEFAULT_CAPACITY = 10;
88

9-
private int size;
9+
private int size;
1010
private AnyType[] items;
1111

1212
public MyArrayList() {

0 commit comments

Comments
 (0)