The stack, as a data structure, is a special linear table that can only be inserted and deleted at one end. It stores the data according to the advanced principle, the first data is pressed into the bottom of the stack, the final data is at the top of the stack, and the data is read from the top of the stack (the last data is read first). The stack has a memory function, and the stack's insertion and deletion operations do not need to change the bottom pointer.
Stacks are special linear tables that allow insertions and deletions on the same side. One end of the allowed insert and delete operations is called the top of the stack (top ), the other end is the bottom of the stack (bottom),the stack bottom is fixed, and the stack top floats, and the number of elements in the stack is zero, called the empty stack. Insertion is generally called a stack (PUSH), and deletion is called a fallback (POP). A stack is also called a LIFO table.
Implementation code:
// StackForImplement.h
/ **
Define block
@param obj callback value
* /
typedef void (^ StackBlock) (id obj);
// Simple implementation of a stack
@interface StackForImplement: NSObject
/ **
Push
@param obj specifies the stack object
* /
-(void) push: (id) obj;
/ **
Pop out
* /
-(id) popObj;
/ **
Is empty
* /
-(BOOL) isEmpty;
/ **
Stack length
* /
-(NSInteger) stackLength;
/ **
Start traversing from the bottom of the stack
@param block callback traversal result
* /
-(void) enumerateObjectsFromBottom: (StackBlock) block;
/ **
Traverse from the top
* /
-(void) enumerateObjectsFromtop: (StackBlock) block;
/ **
All elements are out of the stack, and the elements are returned while being out of the stack
* /
-(void) enumerateObjectsPopStack: (StackBlock) block;
/ **
Clear
* /
-(void) removeAllObjects;
/ **
Returns the top element of the stack
* /
-(id) topObj;
@end
// StackForImplement.m
@interface StackForImplement ()
// Store stack data
@property (nonatomic, strong) NSMutableArray * stackArray;
@end
@implementation StackForImplement
-(void) push: (id) obj {
[self.stackArray addObject: obj];
}
-(id) popObj {
if ([self isEmpty]) {
return nil;
} else {
return self.stackArray.lastObject;
}
}
-(BOOL) isEmpty {
return! self.stackArray.count;
}
-(NSInteger) stackLength {
return self.stackArray.count;
}
-(void) enumerateObjectsFromBottom: (StackBlock) block {
[self.stackArray enumerateObjectsWithOptions: NSEnumerationConcurrent usingBlock: ^ (id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
block? block (obj): nil;
}];
}
-(void) enumerateObjectsFromtop: (StackBlock) block {
[self.stackArray enumerateObjectsWithOptions: NSEnumerationReverse usingBlock: ^ (id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
block? block (obj): nil;
}];
}
-(void) enumerateObjectsPopStack: (StackBlock) block {
__weak typeof (self) weakSelf = self;
NSUInteger count = self.stackArray.count;
for (NSUInteger i = count; i> 0; i-) {
if (block) {
block (weakSelf.stackArray.lastObject);
[self.stackArray removeLastObject];
}
}
}
-(void) removeAllObjects {
[self.stackArray removeAllObjects];
}
-(id) topObj {
if ([self isEmpty]) {
return nil;
} else {
return self.stackArray.lastObject;
}
}
-(NSMutableArray *) stackArray {
if (! _stackArray) {
_stackArray = [NSMutableArray array];
}
return _stackArray;
}
@end
Simple implementation, as above.
OBJECTIVE-C implementation of a simple stack