Introduction: Recently in learning data structure, here with objective-c simple implementation of the stack. It's really easy to use objective-c, because I used the cocoa framework to provide nsmutablearray as a collection of storage elements, and it's convenient to manipulate the collection elements.
However, the following implementation method may not be optimal because Nsmutablearray is not the most lightweight collection container. I don't know yet how to write an optimized stack implementation, but also need to meet this requirement: The stored element is the OC object.
The advantage of using Nsmutablearray as a collection of storage elements: a chain-like storage structure similar to the C language implementation stack does not overflow (or array subscript out-of-bounds) as in C-language arrays.
Objective-c Source:
File StackForNSObject.h:
1 #import<Foundation/Foundation.h>2 3 //as long as the parameter is a block of ID type4typedefvoid(^stackblock) (IDOBJC);5 6 @interfaceStackfornsobject:nsobject7 8 //into the stack9-(void) Push: (ID) objet;Ten //out of the stack One-(ID) poptopelement; A //returns the top element of the stack --(ID) topelement; - //is empty the-(BOOL) isEmpty; - //the length of the stack --(Nsinteger) stacklength; - //traversal, starting at the bottom of the stack +-(void) Traversalelementfrombottom: (stackblock) block; - //Traverse from top +-(void) Traversalelementfromtop: (stackblock) block; A //all elements go out of the stack and return elements on the side of the stack at-(void) Traversalelementpopstack: (stackblock) block; - //Clear --(void) removeallobjects; - //returns the top element of the stack --(ID) Topelemet; - in @end
Files: stackfornsobject.m
1 #import "StackForNSObject.h"2 3 @interfaceStackfornsobject ()4 5 /** MaxSize*/6 //@property (nonatomic,assign) Nsinteger maxSize;7 8 //When you have a stack on the stack , use a strong reference to remember to release the reference9 /** Nsmutablearray*/Ten@property (nonatomic,strong) Nsmutablearray *Stackarray; One A /** Top of Stack*/ - @property (nonatomic,assign) Nsinteger top; - the /** Stack*/ - //@property (nonatomic,weak) stackblock Stackblock; - - @end + - @implementationStackfornsobject + A //Initialize at - //into the stack --(void) Push: (ID) objet{ - [Self.stackarray Addobject:objet]; - } - in //out of the stack --(ID) poptopelement{ to IDOBJC =[Self.stackarray Lastobject]; + [Self.stackarray Removelastobject]; - returnOBJC; the } * $ //returns the top element of the stackPanax Notoginseng-(ID) topelement{ - return[Self.stackarray Lastobject]; the } + A //is empty the-(BOOL) isempty{ + returnSelf.stackArray.count; - } $ $ //the length of the stack --(Nsinteger) stacklength{ - returnSelf.stackArray.count; the } - Wuyi //from the bottom to start traversing the-(void) Traversalelementfrombottom: (stackblock) block{ -Nsenumerator *OBJC =[Self.stackarray Objectenumerator]; Wu for(IDElementinchOBJC) { - block (element); About } $ } - - //Traverse from top --(void) Traversalelementfromtop: (stackblock) block{ A //get the number of stored elements first +Nsinteger count =Self.stackArray.count; the for(Nsinteger i = count; i >0; I--) { - //working with the last element $ block ([Self.stackarray objectatindex:i]); the } the } the the //all elements out of the stack while traversing --(void) Traversalelementpopstack: (stackblock) block{ in //get the number of stored elements first theNsinteger count =Self.stackArray.count; the for(Nsinteger i = count; i >0; I--) { About //working with the last element the block (self.stackArray.lastObject); the [Self.stackarray Removelastobject]; the } + } - the //returns the top element of the stackBayi-(ID) topelemet{ the returnSelf.stackArray.lastObject; the } - - //Clear the-(void) removeallobjects{ the [Self.stackarray removeallobjects]; the } the - #pragmaMark-Lazy Loading the-(nsmutablearray*) stackarray{ the if(_stackarray = =Nil) { the_stackarray =[Nsmutablearray array];94 } the return_stackarray; the } the-(Nsinteger) top{98_top =Self.stackArray.count; About return_top; - }101 102 #pragmaMark-Automatically empties when the object does not exist103- (void) dealloc{104 [Self.stackarray removeallobjects]; the }106 107 @end
Test code:
The source I back up in the Baidu Cloud: Link: Http://pan.baidu.com/s/1pKPrnsR Password: eug9
Data structures-data structures that are simple to implement with objective-c: stacks