用OC基于数组实现循环队列
一、简言
使用数组实现循环队列时,始终会有一个空余的位置预留着,作为一个判决队列已满的条件(当然也可以采用其他方式)。在前面已经用C++介绍了基本的算法,可以去回顾一下https://www.cnblogs.com/XYQ-208910/p/11700048.html。光说不练嘴把式,现在本文使用OC面向对象的思想来实现一下。
二、代码
ArrayQueue
// // ArrayQueue.h // 运行时 // // Created by 夏远全 on 2019/10/19. // #import <Foundation/Foundation.h> NS_ASSUME_NONNULL_BEGIN @interface ArrayQueue : NSObject /** 构造一个循环队列 @param capacity 队列容量 @return 队列 */ +(instancetype)constrcutArrayQueueWithCapacity:(int)capacity; /** 入队列 @param element 元素 */ -(void)enQueueWithElement:(id)element; /** 出队列 @return 元素 */ -(id)deQueueElement; /** 队列是否为空 @return 布尔值 */ -(BOOL)isEmpty; /** 队列是否已满 @return 布尔值 */ -(BOOL)isFull; /** 获取元素个数 @return 个数 */ -(int)eleCount; @end NS_ASSUME_NONNULL_END
// // ArrayQueue.m // 运行时 // // Created by 夏远全 on 2019/10/19. // #import "ArrayQueue.h" @interface ArrayQueue () @property (nonatomic, strong) NSMutableArray *array; @property (nonatomic, assign) int front; @property (nonatomic, assign) int rear; @property (nonatomic, assign) int capacity; @end @implementation ArrayQueue /** 构造一个循环队列 @param capacity 队列容量 @return 队列 */ +(instancetype)constrcutArrayQueueWithCapacity:(int)capacity { ///断言一下 assert(capacity>0); ArrayQueue *arrayQueue = [[ArrayQueue alloc] init]; arrayQueue.array = [NSMutableArray array]; //初始化容器 arrayQueue.capacity = capacity; //初始化容量 arrayQueue.front = arrayQueue.rear = 0; //初始化首尾指针 return arrayQueue; } /** 入队列 @param element 元素 */ -(void)enQueueWithElement:(id)element { ///判断队列是否已满 if ([self isFull]) { NSLog(@"队列已满,元素%@不能被入列, front = %d , rear = %d", element, self.front, self.rear); return; } ///插入新元素 self.array[self.rear] = element; ///更新队尾指针 self.rear = (self.rear + 1) % self.capacity; NSLog(@"入队列的元素 = %@, front = %d , rear = %d", element, self.front, self.rear); } /** 出队列 @return 元素 */ -(id)deQueueElement { ///判断队列是否已空 if ([self isEmpty]) { NSLog(@"队列已空, front = %d , rear = %d", self.front, self.rear); return nil; } ///取出队头的元素 id element = self.array[self.front]; ///更新队头指针 self.front = (self.front + 1) % self.capacity; NSLog(@"出队列的元素 = %@, front = %d , rear = %d", element, self.front, self.rear); return element; } /** 队列是否为空 @return 布尔值 */ -(BOOL)isEmpty { if (self.front == self.rear) { return YES; } return NO; } /** 队列是否已满 @return 布尔值 */ -(BOOL)isFull { if (self.front == (self.rear + 1) % self.capacity) { return YES; } return NO; } /** 获取元素个数 @return 个数 */ -(int)eleCount { int eleCount = (self.rear - self.front + self.capacity) % self.capacity; NSLog(@"队列元素个数 = %d", eleCount); return eleCount; } @end
三、结果
测试
-(void)test_DataStructure_ArrayQueue { /// 构造循环队列 ArrayQueue *arrayQueue = [ArrayQueue constrcutArrayQueueWithCapacity:5]; /// enter Queue 入队列 [arrayQueue enQueueWithElement:@(10)]; [arrayQueue enQueueWithElement:@(30)]; [arrayQueue enQueueWithElement:@(50)]; [arrayQueue enQueueWithElement:@(80)]; [arrayQueue enQueueWithElement:@(100)]; /// 全部入队后 get eleCount 元素个数 [arrayQueue eleCount]; /// deque Queue 出队列 [arrayQueue deQueueElement]; [arrayQueue deQueueElement]; [arrayQueue deQueueElement]; [arrayQueue deQueueElement]; [arrayQueue deQueueElement]; /// 全部出队后 get eleCount 元素个数 [arrayQueue eleCount]; }
打印
2019-10-19 15:20:57.323138+0800 运行时[76006:2342668] 入队列的元素 = 10, front = 0 , rear = 1 2019-10-19 15:20:57.323314+0800 运行时[76006:2342668] 入队列的元素 = 30, front = 0 , rear = 2 2019-10-19 15:20:57.323435+0800 运行时[76006:2342668] 入队列的元素 = 50, front = 0 , rear = 3 2019-10-19 15:20:57.323544+0800 运行时[76006:2342668] 入队列的元素 = 80, front = 0 , rear = 4 2019-10-19 15:20:57.323639+0800 运行时[76006:2342668] 队列已满,元素100不能被入列, front = 0 , rear = 4 2019-10-19 15:20:57.323721+0800 运行时[76006:2342668] 队列元素个数 = 4 2019-10-19 15:20:57.323844+0800 运行时[76006:2342668] 出队列的元素 = 10, front = 1 , rear = 4 2019-10-19 15:20:57.323945+0800 运行时[76006:2342668] 出队列的元素 = 30, front = 2 , rear = 4 2019-10-19 15:20:57.324033+0800 运行时[76006:2342668] 出队列的元素 = 50, front = 3 , rear = 4 2019-10-19 15:20:57.324140+0800 运行时[76006:2342668] 出队列的元素 = 80, front = 4 , rear = 4 2019-10-19 15:20:57.324245+0800 运行时[76006:2342668] 队列已空, front = 4 , rear = 4 2019-10-19 15:20:57.324344+0800 运行时[76006:2342668] 队列元素个数 = 0
程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式!