1、簡言html
使用數組實現循環隊列時,始終會有一個空餘的位置預留着,做爲一個判決隊列已滿的條件(固然也能夠採用其餘方式)。在前面已經用C++介紹了基本的算法,能夠去回顧一下http://www.javashuo.com/article/p-pzucqaux-mu.html。光說不練嘴把式,如今本文使用OC面向對象的思想來實現一下。算法
2、代碼數組
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
3、結果atom
測試spa
-(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