2017-10-27 4 views
-2

간단한 스레드 안전성을 갖는 FIFO 큐 클래스를 구현하고 싶습니다. 표준 Objective-C 프레임 워크에서 NSObject 이외의 다른 클래스를 사용하고 싶지 않습니다. NSMutableArray를 사용하고 싶지 않습니다.Objective-C FIFO 큐

내 해결책이 맞습니까? 고맙습니다!

#import <Foundation/NSObject.h> 

@interface Queue : NSObject 
{ 
    id _value; 
    Queue *tail; 
} 
/* Puts an object at the end of the queue. The object is retained. */ 
- (void) putObject: (id)object; 

/* Gets an object from the beginning of the queue. The object is 
* removed from the queue. If there are no objects in the queue, 
* returns nil. The object is autoreleased. 
*/ 
- (id) getObject; 
@end 

@implementation Queue 
- (id) init 
{ 

    return self; 
} 

- (void) dealloc 
{ 

    [super dealloc]; 
} 

- (void) putObject: (id)object 
{ 

    if(tail) 
    { 
     [tail putObject:object]; 
    }else{ 
     tail = [[Queue alloc]init]; 
     _object = object; 
    } 
} 

- (id) getObject 
{ 
    return _value; 
} 
@end 
+0

Q는 범위를 벗어났습니다. –

+1

[질문 가이드 라인] (https://codereview.stackexchange.com/help/how-to-ask)을 읽은 후 코드 검토로 이동할 수 있습니다. –

답변

0

당신은 내 솔루션은 올바른 자체 수 있다면? 고맙습니다!

테스트를 받으셨습니까? 당신이 고려하는

어떤 것들은 :

  • 빠른 읽기이 객체가 소요 추가
  • 를 (객체를 얻는 보면) ... 디자인 "단지 첫번째에서, 첫번째의"이다 제안 너무 오래 (O 대 O (N)는 (1))
  • 당신은 메모리 관리를 위해 ARC를 사용하는 경우 더 나은 것

듣고 조쉬 캐스 웰에 (당신의 [super dealloc]은/MRC MRR을 사용하는 표시) 의견에서, 이것은 실제로 이러한 유형의 질문에 대한 포럼이 아닙니다.

HTH