• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Newbie Question: implementing a stack
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Newbie Question: implementing a stack


  • Subject: Re: Newbie Question: implementing a stack
  • From: Dave DeLong <email@hidden>
  • Date: Sat, 20 Dec 2008 03:42:08 -0700

Sorry to jump in to this a little late, but a guy in our CocoaHeads group wrote a framework as part of his Master's thesis work. It's a datastructure framework and contains a bunch of datastructures not available (publicly) in Cocoa, such as stacks, queues, dequeues, avl/ rb/aa-trees, treaps, etc. It's open sourced under the LGPL license, and you can check it out of svn from our CocoaHeads page on it:

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

Cheers,

Dave

On 19 Dec, 2008, at 12:22 PM, Steve Wetzel wrote:

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

_______________________________________________

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


References: 
 >Newbie Question: implementing a stack (From: Steve Wetzel <email@hidden>)

  • Prev by Date: Re: Using runtime functions to match up object types safely when setting properties
  • Next by Date: Re: Question Regarding the Memory Address of Objects
  • Previous by thread: Re: Newbie Question: implementing a stack
  • Next by thread: Drawing in a NSView out side of drawRect
  • Index(es):
    • Date
    • Thread