【问题标题】:Objective-C FIFO queueObjective-C 先进先出队列
【发布时间】:2017-10-27 18:34:09
【问题描述】:

我想实现一个具有简单线程安全的 FIFO 队列类。我不想使用标准 Objective-C 框架中的任何类(NSObject 除外),这意味着我不想使用 NSMutableArray。

你能确定我的解决方案是否正确吗?谢谢!

#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

【问题讨论】:

  • Q 超出了 SO 的范围。
  • 阅读他们的question guidelines 后,您可能希望将其移至代码审查。

标签: objective-c queue


【解决方案1】:

你能看看我的解决方案是否正确吗?谢谢!

你测试了吗?

您需要考虑的一些事项:

  • 快速阅读表明这是一种“先进先出”的设计...(查看获取对象)
  • 添加对象花费的时间太长(O(N) 与 O(1))
  • 如果您使用 ARC 进行内存管理会更好(您的 [super dealloc] 表示您正在使用 MRR/MRC)

听听 cmets 中的 Josh Caswell,这真的不是这类问题的论坛。

HTH

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-28
    • 2012-03-28
    • 1970-01-01
    • 1970-01-01
    • 2018-06-30
    • 1970-01-01
    • 2016-04-13
    相关资源
    最近更新 更多