Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Queue/FIFO data structure for the iPhone?

Before I roll my own Queue using NSMutableArray, I'd like to know if there is something more standard available. I don't see anything in the Apple docs, but I'll be surprised if there is not a Queue implementation from somewhere that folks are using. Java spoils me!

like image 465
George Armhold Avatar asked Jul 09 '09 13:07

George Armhold


People also ask

What type of data structure is a queue FIFO?

A Queue is a linear structure which follows a particular order in which the operations are performed. The order is First In First Out (FIFO).

Does Swift have a queue data structure?

Queues are a data structure in which you can insert new data from one end and delete data from the other end. That means it works like FIFO – First In First Out. Enqueue operation is used to insert an element at the beginning.

Are queues LIFO or FIFO?

Queues are based on the FIFO principle, i.e., the element inserted at the first, is the first element to come out of the list.

Does queue use FIFO?

The primary difference between Stack and Queue Data Structures is that Stack follows LIFO while Queue follows FIFO data structure type. LIFO refers to Last In First Out. It means that when we put data in a Stack, it processes the last entry first.


3 Answers

Implementing a queue based on NSMutableArray is pretty easy, it's probably under 50 lines of code.

EDIT:

Found this with a quick google search:

@interface Queue:NSObject {
   NSMutableArray* objects;
}
- (void)addObject:(id)object;
- (id)takeObject;
@end

@implementation Queue

- (id)init {
   if ((self = [super init])) {
       objects = [[NSMutableArray alloc] init];    
   }
   return self;
}

- (void)dealloc {
    [objects release];
    [super dealloc];
}

- (void)addObject:(id)object {
   [objects addObject:object];
}

- (id)takeObject  {
   id object = nil;
   if ([objects count] > 0) {
       object = [[[objects objectAtIndex:0] retain] autorelease];
       [objects removeObjectAtIndex:0];
   }
   return object;
}

@end
like image 101
Matt Bridges Avatar answered Nov 13 '22 08:11

Matt Bridges


Cocoa itself doesn't have a Queue class, and there's not a standard per se, but there are several options, one of which may best fit your needs. See this question (and my answer).

Like you said, you can roll your own using NSMutableArray. If you just need a quick'n'dirty queue (and aren't worried about copying, encoding/decoding, enumeration, etc.) then the solution @Matt suggests is an easy approach. You should also consider adding queue methods to NSMutableArray via a category, which is nice in that your "queue" is also an array (so you can pass it for NSArray parameters), and you get all the NS(Mutable)Array functionality for free.

If performance is important, I recommend using a structure more ideally suited for removing the first element. I wrote CHCircularBufferQueue for my own framework for this very reason. (Not trying to toot my own horn, just trying to save others some time.)

like image 34
Quinn Taylor Avatar answered Nov 13 '22 09:11

Quinn Taylor


I have made a category containing just the deque method, based on Matt Bridges code.

@interface NSMutableArray (ShiftExtension)
// returns the first element of self and removes it
-(id)shift;
@end

@implementation NSMutableArray (ShiftExtension)
-(id)shift {
    if([self count] < 1) return nil;
    id obj = [[[self objectAtIndex:0] retain] autorelease];
    [self removeObjectAtIndex:0];
    return obj;
}
@end
like image 37
neoneye Avatar answered Nov 13 '22 10:11

neoneye