作业 - 动态数组

main.m

 1 #import <Foundation/Foundation.h>
 2 #include "Array.h"
 3 #include "Object.h"
 4 #include "Student.h"
 5 
 6 int main(int argc, const char * argv[]) {
 7     @autoreleasepool {
 8         
 9         Array *arr = ArrayCreate();
10         Anypointer student;
11         
12         student = (Object*)StudentNew("小明", 20);
13         ArrayAdd(arr, student);
14         OBJECT_RELEASE(student);
15         
16         student = (Object*)StudentNew("大明", 21);
17         ArrayAdd(arr, student);
18         OBJECT_RELEASE(student);
19         
20         student = (Object*)StudentNew("小李", 22);
21         ArrayAdd(arr, student);
22         OBJECT_RELEASE(student);
23         
24         student = (Object*)StudentNew("小张", 24);
25         ArrayAdd(arr, student);
26         OBJECT_RELEASE(student);
27         
28         student = (Object*)StudentNew("小马", 28);
29         ArrayAdd(arr, student);
30         OBJECT_RELEASE(student);
31         
32         student = (Object*)StudentNew("小陈", 29);
33         ArrayAdd(arr, student);
34         OBJECT_RELEASE(student);
35         
36         student = (Object*)StudentNew("小蔡", 25);
37         ArrayAdd(arr, student);
38         OBJECT_RELEASE(student);
39         
40         student = (Object*)StudentNew("小王", 26);
41         ArrayAdd(arr, student);
42         OBJECT_RELEASE(student);
43         
44         printlist(arr);
45         
46         printf("删除小李\n");
47         
48         ArrayRemoveAt(arr, 2);
49         
50         printlist(arr);
51         
52         printf("加入老王\n");
53         student = (Object*)StudentNew("老王", 50);
54         ArrayInsertAt(arr, student, 6);
55         
56         printlist(arr);
57         
58         ArrayDestory(arr);
59     }
60     return 0;
61 }

Array.h

 1 #ifndef Array_h
 2 #define Array_h
 3 
 4 #include "Object.h"
 5 #include <stdio.h>
 6 
 7 typedef Object* Anypointer;
 8 
 9 typedef struct Array_ {
10            int retainCount_;
11            int length_;        //实际长度
12            int capacity_;      //分配的内存长度
13     Anypointer *students_;     //数组类型
14 }Array;
15 
16 Array *ArrayCreate();          //创建数组
17 
18 int ArrayGetLength(Array *arr); //获取长度
19 
20 void ArrayAdd(Array *arr, Anypointer student); //添加元素
21 
22 void ArrayInsertAt(Array* arr, Anypointer student, int index);//插入元素
23 
24 void ArrayRemoveAt(Array *arr, int index);//删除元素
25 
26 Anypointer ArrayGetAt(Array *arr, int index);//获取指定元素
27 
28 void printlist(Array *arr);//打印数组
29 
30 void ArrayDestory(Array *arr); //数组销毁
31 
32 #endif /* Array_h */

Array.c

 1 #include "Array.h"
 2 #include "Object.h"
 3 #include "Student.h"
 4 #include <stdlib.h>
 5 #include <string.h>
 6 #include <assert.h>
 7 
 8 
 9 static Anypointer *allocMemoryByCapacity(Array *arr){
10     return malloc(sizeof(Anypointer) * arr->capacity_);
11 }
12 
13 Array *ArrayCreate(){                                   //创建数组
14     Array *arr       = malloc(sizeof(Array));
15     arr -> length_   = 0;
16     arr -> capacity_ = 10;
17     arr -> students_ = allocMemoryByCapacity(arr);
18     return arr;
19 }
20 
21 int ArrayGetLength(Array *arr){                         //获取实际长度
22     return arr->length_;
23 }
24 
25 void ArrayAdd(Array *arr, Anypointer student){          //添加元素
26     if (arr->length_ >= arr->capacity_) {
27         arr->capacity_ *= 2;
28         Anypointer *oldValue = arr->students_;
29         arr->students_ = allocMemoryByCapacity(arr);
30         memcpy(arr->students_, oldValue, arr->length_);
31         free(oldValue);
32     }
33     arr->students_[arr->length_] = student;
34     OBJECT_RETAIN(student);
35     arr->length_ ++;
36 }
37 
38 void ArrayInsertAt(Array* arr, Anypointer student, int index){ //插入元素
39     ArrayAdd(arr, student);
40     for (int i = arr->length_ - 1; i >= index; i --) {
41         arr->students_[i] = arr->students_[i-1];
42     }
43     arr->students_[index - 1] = student;
44     OBJECT_RETAIN(student);
45 }
46 
47 void ArrayRemoveAt(Array *arr, int index){      //删除元素
48     assert(index >= 0 && index < arr->length_);
49     OBJECT_RELEASE(ArrayGetAt(arr, index));
50     arr->length_ --;
51     for (int i = index; i < arr->length_; i++) {
52         arr->students_[i] = arr->students_[i+1];
53     }
54 }
55 
56 Anypointer ArrayGetAt(Array *arr, int index){   //获取元素
57     assert(index >= 0 && index <= arr->length_);
58     return arr->students_[index];
59 }
60 
61 void printlist(Array *arr){                     //打印数组
62     for (int i = 0; i < arr->length_; i++) {
63         printf("位置:%2d ", i+1);
64         printf("名字:%s ", StudentGetName((Student*)arr->students_[i]));
65         printf("年龄:%d \n", StudentGetAge((Student*)arr->students_[i]));
66     }
67 }
68 
69 void ArrayDestory(Array *arr){ //数组销毁
70     free(arr->students_);
71     free(arr);
72     printf("arr released.\n");
73 }

Object.h

 1 #ifndef Object_h
 2 #define Object_h
 3 #include <stdio.h>
 4 
 5 //引用计数法 管理内存
 6 
 7 typedef struct Object_{
 8            int retainCount_;
 9 }Object;
10 
11 #define OBJECT_RETAIN(obj)       ObjectRetain((Object*)obj)
12 #define OBJECT_RELEASE(obj)      ObjectRelease((Object*)obj)
13 #define OBJECT_RETAIN_COUNT(obj) ObjectRetainCount((Object*)obj)
14 
15 void ObjectRetain(Object *obj);
16 
17 void ObjectRelease(Object *obj);
18 
19 int  ObjectRetainCount(Object *obj);
20 
21 #endif /* Object_h */

Object.c

 1 #include "Object.h"
 2 #include <stdlib.h>
 3 
 4 void ObjectRetain(Object *obj){         //引用计数++
 5      obj->retainCount_ ++;
 6 }
 7 
 8 void ObjectRelease(Object *obj){        //引用计数--
 9     obj->retainCount_ --;
10     if (obj->retainCount_ <= 0) {
11         free(obj);
12         printf("Released the obj.\n");
13     }
14 }
15 
16 int ObjectRetainCount(Object *obj){     //获取引用计数
17     return obj->retainCount_;
18 }

Student.h

 1 #ifndef Student_h
 2 #define Student_h
 3 
 4 #include <stdio.h>
 5 #include <stdlib.h>
 6 #include "Object.h"
 7 
 8 typedef struct Student_{        //创建 Student 结构体
 9            int retainCount_;
10           char *name_;
11            int age_;
12 }Student;
13 
14 Student *StudentNew(char *name, int age);  //新建 Student
15 
16    char *StudentGetName(Student* student); //获取姓名
17 
18     int StudentGetAge(Student* student);   //获取年龄
19 
20 #endif /* Student_h */

Student.c

 1 #include "Student.h"
 2 
 3 Student* StudentNew(char *name, int age){       //新建 Student
 4     Student *student = malloc(sizeof(Student));
 5     OBJECT_RETAIN(student);
 6     student->name_   = name;
 7     student->age_    = age;
 8     return student;
 9 }
10 
11 char* StudentGetName(Student* student){         //获取姓名
12     return student->name_;
13 }
14 
15 int StudentGetAge(Student* student){            //获取年龄
16     return student->age_;
17 }

 

posted @ 2016-05-09 12:59  小样和牛牛  阅读(140)  评论(0编辑  收藏  举报