Created
February 16, 2014 19:05
-
-
Save yoman07/9039065 to your computer and use it in GitHub Desktop.
Stack objective-c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Stack.h | |
// testing | |
// | |
// Created by Roman Barzyczak on 16.02.2014. | |
// | |
// | |
#import <Foundation/Foundation.h> | |
typedef struct StackElement StackElement; | |
struct StackElement { | |
__unsafe_unretained id value; | |
StackElement *prev; | |
}; | |
@interface Stack : NSObject | |
- (void) push:(id)value; | |
- (id) pop; | |
- (BOOL) isEmpty; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Stack.m | |
// testing | |
// | |
// Created by Roman Barzyczak on 16.02.2014. | |
// | |
// | |
#import "Stack.h" | |
@interface Stack () | |
@property (nonatomic) StackElement *top; | |
@end | |
@implementation Stack | |
- (BOOL) isEmpty { | |
return self.top == nil ? YES : NO; | |
} | |
- (void) push:(id)value { | |
StackElement *stackElement = malloc(sizeof(StackElement)); | |
stackElement->value = value; | |
if([self isEmpty]) { | |
stackElement->prev = NULL; | |
self.top = stackElement; | |
} else { | |
StackElement *lastTop = self.top; | |
self.top = stackElement; | |
stackElement->prev = lastTop; | |
} | |
} | |
- (id) pop { | |
if([self isEmpty]) { | |
return nil; | |
} | |
StackElement *lastTop = self.top; | |
self.top = self.top->prev; | |
return lastTop->value; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment