[iOS开发站在巨人肩膀上]之How to implement a stack in Objective-C
2011-09-04 23:59
736 查看
本文来源:http://www.codeproject.com/Tips/226892/How-to-implement-a-stack-in-Objective-C.aspx
Stack is a very commonly used data structure. But Objective-C does not have this data structure. So I use
implement the basic functions push and pop.
HsuStack.h
Collapse | Copy
Code
HsuStack.m
Collapse | Copy
Code
Objective-C has a type called id, that acts in some ways like a void*, though it's meant strictly for objects. Objective-C differs from Java and C++ in that when you call a method on an object, it doesn't need to know the type. That method simply just has to
exist. This is refered to as message pasing in Objective-C.
Stack is a very commonly used data structure. But Objective-C does not have this data structure. So I use
NSMutableArrayto
implement the basic functions push and pop.
HsuStack.h
Collapse | Copy
Code
@interface HsuStack : NSObject { NSMutableArray* m_array; int count; } - (void)push:(id)anObject; - (id)pop; - (void)clear; @property (nonatomic, readonly) int count; @end
HsuStack.m
Collapse | Copy
Code
#import "HsuStack.h" @implementation HsuStack @synthesize count; - (id)init { if( self=[super init] ) { m_array = [[NSMutableArray alloc] init]; count = 0; } return self; } - (void)dealloc { [m_array release]; [self dealloc]; [super dealloc]; } - (void)push:(id)anObject { [m_array addObject:anObject]; count = m_array.count; } - (id)pop { id obj = nil; if(m_array.count > 0) { obj = [[[m_array lastObject]retain]autorelease]; [m_array removeLastObject]; count = m_array.count; } return obj; } - (void)clear { [m_array removeAllObjects]; count = 0; } @end
Objective-C has a type called id, that acts in some ways like a void*, though it's meant strictly for objects. Objective-C differs from Java and C++ in that when you call a method on an object, it doesn't need to know the type. That method simply just has to
exist. This is refered to as message pasing in Objective-C.
相关文章推荐
- [iOS开发站在巨人肩膀上]之How to implement Queue in Objective-C
- [iOS开发站在巨人肩膀上]之How to avoid memory leaks in iPhone applications
- [iOS开发站在巨人肩膀上]之Introduction to iOS Graphics APIs: Part 1
- How to get md5 and SHA1 in objective c (iOS sdk)
- How To Implement Search Bar in iOS 7 Using Storyboard
- How to get md5 and SHA1 in objective c (iOS sdk)
- How to implement common datastructures (List, Stack, Map) in plain Java - Tutorial
- How to convert from int to string in objective c
- How to use NSNotification in iOS?
- iOS 开发:调试错误“Unable to run app in Simulator“
- (iPhone/iPad开发)how to browse the document directory of iOS Devices
- How to Play, Record, and Edit Videos in iOS
- How To Make a Music Visualizer in iOS
- [iOS开发站在巨人肩膀上]之 iOS 4 in Action - Using Location Monitoring Service
- How to implement an ArrayList structure in Java - Tutorial
- How to implement an IPv6 ping command in VC++
- How to implement collapse all in windows tree structure such as regedit
- How To Use Git Source Control with Xcode in iOS 7
- Learn how to Use UIPageViewController in iOS
- How to copy an object in objective c