#import <Foundation/Foundation.h>
@interface LoopQueue : NSObject
-( instancetype )initWithItem:( NSObject *)item andCapcity:( NSUInteger )capacity;
-( instancetype )init;
-( void )enqueue:( NSObject *)item;
-( NSObject *)dequeue;
#import "LoopQueue.h"
@interface LoopQueue ()
@property ( nonatomic ) int first;
@property ( nonatomic ) int last;
@property ( nonatomic ) NSUInteger capacity;
@property ( nonatomic ) NSMutableArray *elementData;
@end
@implementation LoopQueue
-( instancetype )initWithItem:( NSObject *)item andCapcity:( NSUInteger )cap
{
self . first = - 1 ;
self . last = - 1 ;
self . capacity = cap;
self . elementData = [ NSMutableArray arrayWithCapacity :cap];
if (item == nil ) {
return self ;
}
[ self . elementData addObject :item];
return self ;
}
-( instancetype )init
{
self = [[ LoopQueue alloc ] initWithItem : nil andCapcity : DEFAULT_SIZE ];
return self ;
}
-( BOOL )isFull
{
return ( _first == 0 && _last == _capacity - 1 ) || _first == _last + 1 ;
}
-( BOOL )isEmpty
{
return self . first == - 1 ;
}
-( void )enqueue:( NSObject *)item
{
if (![ self isFull ]) {
if ( _last == _capacity - 1 || _last == - 1 ) {
self . elementData [ 0 ] = item;
_last = 0 ;
if ( _first == - 1 ){
_first = 0 ;
}
} else {
self . elementData [++ _last ] = item;
}
} else {
self . capacity ++;
self . last ++;
[ self . elementData addObject :item];
}
}
-( NSObject *)dequeue
{
if (![ self isEmpty ]) {
NSObject *tmp = self . elementData [ _first ];
if ( _first == _last ){
_last = _first = - 1 ;
} else if ( _first == _capacity - 1 ){
_first = 0 ;
} else {
_first ++;
}
return tmp;
} else {
NSLog ( @"Fail :Queue is Empty" );
return nil ;
}
}
#import <Foundation/Foundation.h>
@interface LoopQueue : NSObject
- ( instancetype )initWithItem:(NSObject *)item andCapcity:(NSUInteger )capacity;
- ( instancetype )init;
#import "LoopQueue.h"
#define Default_size 10
@interface LoopQueue ()
@property ( nonatomic ) int first;
@property ( nonatomic ) int last;
@property ( nonatomic , assign ) NSUInteger capacity;
@property ( nonatomic , copy ) NSMutableArray *elementData;
@end
@implementation LoopQueue
- ( instancetype )initWithItem:( NSObject *)item andCapcity:( NSUInteger )capacity{
self . first = -1;
self . last = -1;
self . capacity = capacity;
self . elementData = [ NSMutableArray arrayWithCapacity :capacity];
if (item == nil ) {
return self ;
}
[ self . elementData addObject :item];
return self ;
}
- ( instancetype )init{
self = [[ LoopQueue alloc ] initWithItem : nil andCapcity : Default_size ];
return self ;
}
- ( BOOL )isFull{
return ( _first == 0 && _last == _capacity -1)|| _first == _last + 1;
}
- ( BOOL )isEmpty{
return self . first == -1;
}
- ( void )enqueue:( NSObject *)item{
if (![ self isFull ]) {
if ( _last == _capacity -1 || _last == -1) {
self . elementData [0]=item;
_last = 0;
if ( _first == -1) {
_first = 0;
}
} else {
self . elementData [++ _last ] = item;
}
} else {
self . capacity ++;
self . last ++;
[ self . elementData addObject :item];
}
}
- ( NSObject *)dequeue{
if (![ self isEmpty ]) {
NSObject *temp = self . elementData [ _first ];
if ( _first == _last ) {
_last = _first = -1;
} else if ( _first == _capacity -1){
_first = 0;
} else {
_first ++;
}
return temp;
} else {
NSLog ( @" 空队列 " );
return nil ;
}
}
@end