Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the iOS SDK provide queues and stacks?

Tags:

stack

ios

queue

I'm writing an iPhone app, and I'm surprised that there seem to be no NSQueue or NSStack classes in Apple's Foundation Framework. I see that it would be quite easy to roll my own, starting with an NSMutableArray, so I'll do that unless I've missed something. Have I missed something?

like image 709
Tommy Herbert Avatar asked Sep 06 '10 15:09

Tommy Herbert


7 Answers

Here's my Stack class, in case it's useful to those who come after me. As you can see, the pop method involves enough code that you'd want to factor it out.

Stack.h:

#import <Foundation/Foundation.h>

@interface Stack : NSObject {
    NSMutableArray *contents;
}

- (void)push:(id)object;
- (id)pop;

@end

Stack.m

#import "Stack.h"

@implementation Stack

// superclass overrides

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

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

// Stack methods

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

- (id)pop {
    id returnObject = [[contents lastObject] retain];
    if (returnObject) {
            [contents removeLastObject];
    }
    return [returnObject autorelease];
}

@end
like image 73
Tommy Herbert Avatar answered Oct 03 '22 00:10

Tommy Herbert


as far as I know there is no generic class avaialbe. Try using the NSMutableArray, add via addObject and get first/last via objectAtIndex and removeObjectAtIndex.

like image 20
Michael W. Avatar answered Oct 02 '22 00:10

Michael W.


Another easy way would be to extend NSMutableArray's capabilities by making use of Objective C's categories. You can do that by adding two files to your project:

NSMutableArray+Stack.h

@interface NSMutableArray (StackExtension)

- (void)push:(id)object;
- (id)pop;

@end

NSMutableArray+Stack.m

#import "NSMutableArray+Stack.h"

@implementation NSMutableArray (StackExtension)

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

- (id)pop {
    id lastObject = [self lastObject];
    [self removeLastObject];
    return lastObject;
}

@end

Now you can use a regular NSMutableArray in every other file of your project like a stack and call push or pop on that object. Don't forget to #import NSMutableArray+Stack.h in those files. Here is some sample code how you can use your new NSMutableArray as a stack:

NSMutableArray *myStack = [[NSMutableArray alloc] init]; // stack size = 0

NSString *aString = @"hello world";
[myStack push:myString];            // stack size = 1

NSString *anotherString = @"hello universe";
[myStack push:anotherString];       // stack size = 2

NSString *topMostStackObject; 

topMostStackObject = [myStack pop]; // stack size = 1
NSLog("%@",topMostStackObject);

topMostStackObject = [myStack pop]; // stack size = 0
NSLog("%@",topMostStackObject);

The log output will be:

hello universe
hello world
like image 31
Mischa Avatar answered Oct 01 '22 00:10

Mischa


I'm a bit late to this party, but are you aware of CHDataStructures?

http://cocoaheads.byu.edu/code/CHDataStructures

like image 39
occulus Avatar answered Oct 01 '22 00:10

occulus


I have put a working iOS Objective C queue object on GitHub. The code was taken from various posts and by no means is owned by me.

https://github.com/esromneb/ios-queue-object/

If you see any problems please fork, and make a pull request!

like image 5
benathon Avatar answered Oct 03 '22 00:10

benathon


Yes, an NSMutableArray doubles as a stack or queue. (It would be slightly inefficient as a queue.)

You could also use C++'s stack and queue adapter, but it makes memory management a bit messy if you want to store Objective-C objects with it.

like image 1
kennytm Avatar answered Oct 01 '22 00:10

kennytm


ObjectiveSugar is a very popular CocoaPod that provides, among a bunch of other great stuff, push and pop API calls on NSMutableArray. Sure, it's not in the iOS SDK, but I'm sharing it here because I was looking for the same thing, and this was the solution I went with (and it certainly didn't hurt that we were already using this CocoaPod in our codebase).

like image 1
Gabe Avatar answered Oct 01 '22 00:10

Gabe