Skip to content

Instantly share code, notes, and snippets.

@yoman07
Created February 16, 2014 19:05
Show Gist options
  • Save yoman07/9039065 to your computer and use it in GitHub Desktop.
Save yoman07/9039065 to your computer and use it in GitHub Desktop.
Stack objective-c
//
// 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
//
// 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