2012-10-29 201 views
3

想知道創建手動數組的最佳方式,而不使用NSMutalbleArray,我一直在研究可能的最佳解決方案,但沒有一個優雅的答案,您認爲Objective C中的最佳方法是什麼從頭開始創建一個NSMutableArray樣式對象?使用FIFO隊列作爲最終解決方案,即使是基本的數組結構也是一個很好的提示!謝謝, 約翰目標C - 手動排隊FIFO隊列

+0

的任'id *'或基於鏈表的解決方案將工作... – 2012-10-29 20:37:21

+1

NSMutableArray有什麼問題? –

+0

如果您需要綁定它,爲什麼不使用正常的數組並管理push和pop indeces? –

回答

14

NSMutableArray上的類別是IMO最簡單的方法。我有棧(LIFO)和隊列類別(FIFO)

頁眉

#import <Foundation/Foundation.h> 

@interface NSMutableArray (QueueStack) 
-(id)queuePop; 
-(void)queuePush:(id)obj; 
-(id)stackPop; 
-(void)stackPush:(id)obj; 
@end 

實施

#import "NSMutableArray+QueueStack.h" 

@implementation NSMutableArray (QueueStack) 
// Queues are first-in-first-out, so we remove objects from the head 
-(id)queuePop { 
    @synchronized(self) 
    { 
    if ([self count] == 0) { 
     return nil; 
    } 

    id queueObject = [[[self objectAtIndex:0] retain] autorelease]; 

    [self removeObjectAtIndex:0]; 

    return queueObject; 
    } 
} 

// Add to the tail of the queue 
-(void)queuePush:(id)anObject { 
    @synchronized(self) 
    { 
    [self addObject:anObject]; 
    } 
} 

//Stacks are last-in-first-out. 
-(id)stackPop { 
    @synchronized(self) 
    { 
    id lastObject = [[[self lastObject] retain] autorelease]; 

    if (lastObject) 
     [self removeLastObject]; 

    return lastObject; 
    } 
} 

-(void)stackPush:(id)obj { 
    @synchronized(self) 
    { 
    [self addObject: obj]; 
    } 
} 
@end 

製作和使用隊列:

NSMutableArray *queue = [NSMutableArray array]; 

//Put an item in the queue 
[queue queuePush:myObj]; 

//Retrieve an item, (this will be the first one) 
MyCoolObject *myObject = [queue queuePop]; 
1

即使我不明白NSMutableArray的問題,這是一種使用雙向鏈接來實現隊列的方法編輯列表(希望我得到它的權利,我有點累了)):

注:我假設使用ARC。

//Node.h 
@interface Node : NSObject 

@property (strong)id value; 
@property (strong)Node *previous; 
@property (strong)Node *next; 


//Node.m 
@implementation 
@end 


/Queue.h 
@interface Queue : NSObject 

- (void)enqueue:(id)objectToEnqueue; 
- (id)dequeue; 

@end 

//Queue.m 
@interface Queue() 
{ 
    Node *start; 
} 

@implementation 

- (void)enqueue:(id)objectToEnqueue 
{ 
    Node *node = [Node new]; 
    node.value = objectToEnqueue; 

    if (nil == start) 
    { 
     node.previous = node; 
     node.next = node; 
     start = node; 
    } 
    else 
    { 
     node.previous = start.previous; 
     node.next = start; 
     start.previous = node; 
     start = node; 
    } 
} 

- (id)dequeue 
{ 
    if (nil == start) 
     return nil; 

    Node *node = start.previous; 

    start.previous = start.previous.previous; 
    start.previous.next = start; 

    id objectToDequeue = node.value; 

    return objectToDequeue; 

} 
@end 

如果你正在尋找一種方式做,在純C,也許這將幫助你:

C PROGRAM TO IMPLEMENT A QUEUE

+0

可愛的回答歡呼 – Woodstock

2

陣列與FIFO方式:

if (mutArr.count == 5) { 
     for (int j = 0; j < 4; j++) { 
      [mutArr exchangeObjectAtIndex:j withObjectAtIndex:j+1]; 
     } 
     [mutArr removeObjectAtIndex:4]; 
     [mutArr addObject:mutDict]; 
    }else{ 
     [mutArr addObject:mutDict]; 
    }