Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Just wondering the best way to create a manual array, without using NSMutalbleArray, I have being researching best possible solutions but without an elegant answer, what do you think, in Objective C what's the best way to create an NSMutableArray style object from scratch?
with a FIFO queue as the final solution, even a basic array structure would be a great hint! thanks,
–
Categories on NSMutableArray is the easiest way IMO. I have a category for stacks (LIFO) and queues (FIFO)
Header
#import <Foundation/Foundation.h>
@interface NSMutableArray (QueueStack)
-(id)queuePop;
-(void)queuePush:(id)obj;
-(id)stackPop;
-(void)stackPush:(id)obj;
Implementation
#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];
To Make and use a queue:
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];
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];
Even though I don't understand the problem with an NSMutableArray, here's a way you could implement a queue using a doubly linked list (Hopefully I got it right, I'm kinda tired ;)):
Note: I'm assuming usage of ARC.
//Node.h
@interface Node : NSObject
@property (strong)id value;
@property (strong)Node *previous;
@property (strong)Node *next;
//Node.m
@implementation
/Queue.h
@interface Queue : NSObject
- (void)enqueue:(id)objectToEnqueue;
- (id)dequeue;
//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;
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;
If you're looking for a way to do that in pure C, maybe this will help you:
C PROGRAM TO IMPLEMENT A QUEUE
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.