nil NSNull NULL,用着。。。

nil发一个不存在的消息不报错,但是NSNull是一个对象,发不存在的消息会crash
nil指的是一个oc类型的指针 a null pointer to an oc object;
Nil A null pointer to an oc class
NULL,A null pointer to anything else ,is for c style memory pointers
NSNull常用在不能使用nil集合中
Objective-C语言中可以使用NSArray或NSMutableArray来实现队列,但是它们是动态数组,插入和删除操作效率较低。如果需要实现一个高效的队列,可以使用循环队列。 循环队列是一种特殊的队列,它的队尾指针可以指向队头位置,形成一个环形结构。这样可以充分利用数组空间,提高队列的效率。 下面是Objective-C实现一个循环队列的示例代码: ``` @interface CircularQueue : NSObject @property(nonatomic, assign) NSInteger head; // 队头指针 @property(nonatomic, assign) NSInteger tail; // 队尾指针 @property(nonatomic, assign) NSInteger size; // 队列大小 @property(nonatomic, strong) NSMutableArray *queueArray; // 队列数组 - (instancetype)initWithSize:(NSInteger)size; // 初始化方法 - (BOOL)enqueue:(id)obj; // 入队方法 - (id)dequeue; // 出队方法 - (BOOL)isEmpty; // 判断队列是否为空 - (BOOL)isFull; // 判断队列是否已满 @end @implementation CircularQueue - (instancetype)initWithSize:(NSInteger)size { if (self = [super init]) { self.head = 0; self.tail = 0; self.size = size; self.queueArray = [NSMutableArray arrayWithCapacity:size]; for (NSInteger i = 0; i < size; i++) { [self.queueArray addObject:[NSNull null]]; } } return self; } - (BOOL)enqueue:(id)obj { if ([self isFull]) { return NO; } self.queueArray[self.tail] = obj; self.tail = (self.tail + 1) % self.size; return YES; } - (id)dequeue { if ([self isEmpty]) { return nil; } id obj = self.queueArray[self.head]; self.queueArray[self.head] = [NSNull null]; self.head = (self.head + 1) % self.size; return obj; } - (BOOL)isEmpty { return self.head == self.tail && self.queueArray[self.head] == [NSNull null]; } - (BOOL)isFull { return self.head == self.tail && self.queueArray[self.head] != [NSNull null]; } @end ``` 在上面的代码中,我们使用一个NSMutableArray来保存队列元素,使用head和tail两个指针来指示队头和队尾位置。enqueue方法用于入队操作,dequeue方法用于出队操作,isEmpty方法和isFull方法分别用于判断队列是否为空和已满。注意,在enqueue和dequeue方法中,我们使用取模运算来实现循环指针的功能。 使用循环队列可以有效提高队列的效率,特别是在需要频繁插入和删除元素的场景下。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值