自定义的可变数组
学过数据结构的人应该了解,数据结构是用来在内存中存储数据用的,在大学期间学习线性表,链表,十字链表,图,二叉树等的时候,都是要自己写创建,遍历,插入,删除等函数的实现。然而后面学了面向对象语言后,系统都自己封装好了,可以直接使用,条用方法即可。为了强化自己的动手编程能力,决定自己手写一个object-c中的可变数组,用链表实现。
申明部分
#import <Foundation/Foundation.h>
@interface HTCMutableArray : NSObject
// 计算数组中对象的个数
@property (nonatomic,assign,readonly) NSInteger count;
// 类方法创建对象
+ (instancetype)array;
// 添加对象到数组中
- (void)addObject:(id)object;
// 根据索引得到对象
- (id)objectAtIndex:(NSInteger)index;
// 插入对象到索引为index的数组中
- (void)insertObject:(id)object atIndex:(NSInteger)index;
// 从数组中移除索引为index的对象
- (void)removeObjectAtIndex:(NSInteger)index;
// 遍历数组中的对象,输出其地址
- (void)ergodicArray;
@end
实现部分
#import "HTCMutableArray.h"
/**
* 自定义可变数组,使用数据结构中的链表结构完成
*/
@interface HTCMutableArray ()
@property (nonatomic,strong) id object;
@property (nonatomic,strong) HTCMutableArray *next;
@end
@implementation HTCMutableArray
- (instancetype)init{
// 1.创建array对象时,记为头结点,不存储数据,
self = [super init];
_count = 0;
self.object = nil;
self.next = nil;
return self;
}
// 类方法创建对象
+ (instancetype)array{
HTCMutableArray *array = [[HTCMutableArray alloc] init];
return array;
}
// 添加对象到数组中
- (void)addObject:(id)object{
HTCMutableArray *array = [HTCMutableArray array];
HTCMutableArray *temp = self;
// 1.遍历链表,如果当前节点的next指针为nil,结束循环
while(temp.next){
temp = temp.next;
}
// 2.将尾节点的next指针赋给要添加的对象
temp.next = array;
array.object = object;
// 3.将要添加的对象的next指针设为nil
array.next = nil;
_count++;
}
// 根据索引得到对象
- (id)objectAtIndex:(NSInteger)index{
NSInteger tempIndex = 0;
// 1.取出头结点next(即第一个节点,因为头结点没有用来存储数据,只是一个起始标识)
HTCMutableArray *temp = self.next;
// 2.遍历链表,
while(temp){
if (index == tempIndex) break;
tempIndex++;
temp = temp.next;
}
// 3.返回节点中的object对象
return temp.object;
}
// 插入对象到索引为index的数组中
- (void)insertObject:(id)object atIndex:(NSInteger)index{
if (index > _count) {
return;
}
NSInteger tempIndex = 0;
HTCMutableArray *temp = self.next;
while (temp) {
if (index == tempIndex) {
HTCMutableArray *array = [HTCMutableArray array];
array.object = object;
array.next = temp.next;
temp.next = array;
_count++;
break;
}
temp = temp.next;
tempIndex++;
}
}
// 从数组中移除索引为index的对象
- (void)removeObjectAtIndex:(NSInteger)index{
HTCMutableArray *temp = self.next;
HTCMutableArray *remove = nil;
NSInteger tempIndex = 0;
while (temp) {
if (tempIndex == (index - 1)) {
remove = temp.next;
temp.next = temp.next.next;
remove = nil;
_count--;
break;
}
tempIndex++;
temp = temp.next;
}
}
// 遍历数组中的对象,输出其地址
- (void)ergodicArray{
HTCMutableArray *temp = self.next;
while (temp) {
NSLog(@"object pointer = %p",temp.object);
temp = temp.next;
}
}
@end
使用案例
#import <Foundation/Foundation.h>
#import "HTCMutableArray.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
// 1.定义3个对象
NSObject *obj1 = [[NSObject alloc] init];
NSObject *obj2 = [[NSObject alloc] init];
NSObject *obj3 = [[NSObject alloc] init];
// 2.创建自定义的数组对象
HTCMutableArray *array = [HTCMutableArray array];
// 3.将对象添加到数组中
[array addObject:obj1];
[array addObject:obj2];
[array addObject:obj3];
// 4.打印数组中对象的个数
NSLog(@"count = %ld",array.count);
// 5.插入一个新的对象
NSObject *obj5 = [[NSObject alloc] init];
[array insertObject:obj5 atIndex:1];
// 6.根据索引值获得对象
NSObject *obj4 = [array objectAtIndex:1];
// 7.输出5个对象的地址
NSLog(@"obj1=%p,obj2=%p,obj3=%p,obj4=%p,obj5=%p",obj1,obj2,obj3,obj4,obj5);
// 8.遍历数组,输出其地址,
[array ergodicArray];
}
return 0;
}
运行情况
第一次写blog,后面会继续坚持下去的,加油!