本篇文章為大家展示了怎么在ios中實(shí)現(xiàn)一個(gè)隊(duì)列,內(nèi)容簡明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
為企業(yè)提供成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、網(wǎng)站優(yōu)化、營銷型網(wǎng)站建設(shè)、競價(jià)托管、品牌運(yùn)營等營銷獲客服務(wù)。創(chuàng)新互聯(lián)公司擁有網(wǎng)絡(luò)營銷運(yùn)營團(tuán)隊(duì),以豐富的互聯(lián)網(wǎng)營銷經(jīng)驗(yàn)助力企業(yè)精準(zhǔn)獲客,真正落地解決中小企業(yè)營銷獲客難題,做到“讓獲客更簡單”。自創(chuàng)立至今,成功用技術(shù)實(shí)力解決了企業(yè)“網(wǎng)站建設(shè)、網(wǎng)絡(luò)品牌塑造、網(wǎng)絡(luò)營銷”三大難題,同時(shí)降低了營銷成本,提高了有效客戶轉(zhuǎn)化率,獲得了眾多企業(yè)客戶的高度認(rèn)可!
接口部分
#import@interface XMQueue : NSObject ///隊(duì)列追加 - (void)add:(id) obj; ///隊(duì)列取出 - (id)next; ///返回隊(duì)列最前端,不刪除隊(duì)列內(nèi)元素 - (id)first; ///隊(duì)列內(nèi)是否還有元素 - (BOOL) isEmpty; ///隊(duì)列轉(zhuǎn)array - (NSArray *) arrayCopy; @end
實(shí)現(xiàn)方法
#import "XMQueue.h" @interface XMQueue() @property NSMutableArray *array; @property NSInteger currentIndex; @end @implementation XMQueue - (instancetype)init { self = [super init]; if (self) { self.array = [[NSMutableArray alloc] initWithCapacity:32]; } return self; } - (void)add:(id) obj{ if (self.currentIndex >= self.array.count-1) { NSMutableArray *newArray = [[NSMutableArray alloc] initWithCapacity:self.array.count*2]; for (id obj in self.array) { [newArray addObject:obj]; } self.array = newArray; } [self.array addObject:obj]; } - (id)next{ if (![self isEmpty]) { id obj = [self.array objectAtIndex:0]; [self.array removeObjectAtIndex:0]; return obj; }else{ return @"Empty"; } } - (id)first{ if (![self isEmpty]) { return self.array[0]; }else{ return @"Empty"; } } - (BOOL) isEmpty{ return self.array.count == 0; } @end
測試
XMQueue *queue = [[XMQueue alloc] init]; for (int i = 0; i < 5; i++) { [queue add:[NSNumber numberWithInt:i]]; } for (NSNumber *number in [queue arrayCopy]) { printf("%d,",[number intValue]); } printf("\n"); [queue add:[queue next]]; for (NSNumber *number in [queue arrayCopy]) { printf("%d,",[number intValue]); } printf("\n"); [queue add:[queue next]]; for (NSNumber *number in [queue arrayCopy]) { printf("%d,",[number intValue]); } printf("\n"); [queue add:[queue next]]; for (NSNumber *number in [queue arrayCopy]) { printf("%d,",[number intValue]); } printf("\n"); [queue add:[queue next]]; for (NSNumber *number in [queue arrayCopy]) { printf("%d,",[number intValue]); } printf("\n"); [queue add:[queue next]]; for (NSNumber *number in [queue arrayCopy]) { printf("%d,",[number intValue]); }
結(jié)果
上述內(nèi)容就是怎么在ios中實(shí)現(xiàn)一個(gè)隊(duì)列,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。