OBJECTIVE-C implementation of a simple stack

Source: Internet
Author: User



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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.