Newbie Question: implementing a stack
Newbie Question: implementing a stack
- Subject: Newbie Question: implementing a stack
- From: Steve Wetzel <email@hidden>
- Date: Fri, 19 Dec 2008 13:22:22 -0600
Hi All,
I am new to this list and new to mac programming as well. I am
working on implementing a stack as a category of NSMutableArray. I
want this stack to be able to store objects. This is what I have so
far:
//
// Stack.h
// Stack implements a basic stack object in an NSMutableArray object.
#import <Foundation/Foundation.h>
@interface NSMutableArray (Stack)
-(void)push:(id)obj; //push obj of on the stack
-(id)pop; //pop top item off the stack
-(id)peek; //look at the top item on the stack
-(void)clear; //remove all objects from the stack
-(NSUInteger)size; //return the number of items on the stack
@end
//
// Stack.m
// Stack
#import "Stack.h"
@implementation NSMutableArray (Stack)
-(void)push:(id)obj
{
[self addObject:obj];
}
-(id)pop
{
id popedObj;
if([self count]) {
popedObj = [[[self lastObject] retain] autorelease];
[self removeLastObject];
} else {
popedObj = nil;
}
return popedObj;
}
-(id)peek; //look at the top item on the stack
{
return [self lastObject];
}
-(void)clear; //remove all objects from the stack
{
[self removeAllObjects];
}
-(NSUInteger)size; //return the number of items on the stack
{
return [self count];
}
@end
My question involves storing objects on the stack. As I understand it
when I store an object on the stack, I am simply storing a pointer to
the object on the stack. If I create an NSString object called foo
and do the following:
NSString *foo = [[NSString alloc] initWithString:@"foo text"];
[FractionStack push:foo];
I have put a copy of the pointer to foo on the stack. If I now change
foo, the top element on the stack changes also. I do not want this to
happen but I am not sure as to the best approach to make sure this
does not happen.
Would it be best to copy the object to another object - say fooCopy
then push fooCopy on the stack? Or would it be better to copy the
object inside of the push method? And either way, I am struggling
with memory management. If I first copy foo to fooCopy before placing
it on the stack then I release fooCopy, will the element on the stack
still be able to reference it? do I need to retain it in my push
method?
Also, I am thinking I likely will need to release these objects when I
pop them and when I clear the stack. Is that correct?
I am really trying to get my head wrapped around this but I have been
struggling with this for some time. If, in addition to some words of
wisdom, you can point me in the direction of how I could create a
simple test of this in my code to work things out on my own that would
be appreciated also.
As I said I am new to this so if my post is not in keeping with the
way things work on this list please let me know.
Thanks in advance,
Steve
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden