Skip to content

Commit

Permalink
implementation of linked-list-based stack in objective-c
Browse files Browse the repository at this point in the history
  • Loading branch information
jerryderry committed Oct 8, 2018
1 parent 50c35b7 commit f74488c
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 0 deletions.
19 changes: 19 additions & 0 deletions object-c/08_stack/LinkedStack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// LinkedStack.h
// algo
//
// Created by Wenru Dong on 2018/10/8.
// Copyright © 2018年 Wenru Dong. All rights reserved.
//
// Stack based upon linked list
// 基于链表实现的栈

#import <Foundation/Foundation.h>

@interface LinkedStack : NSObject

- (BOOL)isEmpty;
- (void)push:(int)value;
- (int)pop;

@end
47 changes: 47 additions & 0 deletions object-c/08_stack/LinkedStack.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// LinkedStack.m
// algo
//
// Created by Wenru Dong on 2018/10/8.
// Copyright © 2018年 Wenru Dong. All rights reserved.
//

#import "LinkedStack.h"
#import "ListNode.h"

@implementation LinkedStack
{
@private
ListNode* _top;
}

- (BOOL)isEmpty {
return _top == nil;
}

- (void)push:(int)value {
ListNode *newTop = [ListNode nodeWithValue:value];
newTop.next = _top;
_top = newTop;
}

- (int)pop {
if ([self isEmpty]) {
[NSException raise:NSRangeException format:@"The stack is empty."];
}
int value = _top.value;
_top = _top.next;
return value;
}

- (NSString *)debugDescription {
NSMutableString *info = [[NSMutableString alloc] init];
ListNode *current = _top;
while (current) {
[info appendString:[NSString stringWithFormat:@"%d]", current.value]];
current = current.next;
}
return [NSString stringWithString:info];
}

@end
56 changes: 56 additions & 0 deletions object-c/08_stack/LinkedStackTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// LinkedStackTests.m
// LinkedStackTests
//
// Created by Wenru Dong on 2018/10/8.
// Copyright © 2018年 Wenru Dong. All rights reserved.
//

#import <XCTest/XCTest.h>
#import "LinkedStack.h"

@interface LinkedStackTests : XCTestCase

@end

@implementation LinkedStackTests

- (void)setUp {
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.
}

- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
}

- (void)testPush {
LinkedStack *stack = [[LinkedStack alloc] init];
for (int i = 0; i < 10; i++) {
[stack push:i];
}
XCTAssertEqualObjects([stack debugDescription], @"9]8]7]6]5]4]3]2]1]0]");
}

- (void)testPop {
LinkedStack *stack = [[LinkedStack alloc] init];
for (int i = 0; i < 10; i++) {
[stack push:i];
}
[stack pop];
XCTAssertEqualObjects([stack debugDescription], @"8]7]6]5]4]3]2]1]0]");
for (int i = 0; i < 9; i++) {
[stack pop];
}
XCTAssertThrowsSpecificNamed([stack pop], NSException, NSRangeException, @"The stack is empty.");
}

//- (void)testPerformanceExample {
// // This is an example of a performance test case.
// [self measureBlock:^{
// // Put the code you want to measure the time of here.
// }];
//}

@end
19 changes: 19 additions & 0 deletions object-c/08_stack/ListNode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// ListNode.h
// algo
//
// Created by Wenru Dong on 2018/10/6.
// Copyright © 2018年 Wenru Dong. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface ListNode : NSObject

@property int value;
@property ListNode *next;

- (instancetype)initWithValue:(int)value;
+ (instancetype)nodeWithValue:(int)value;

@end
28 changes: 28 additions & 0 deletions object-c/08_stack/ListNode.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// ListNode.m
// algo
//
// Created by Wenru Dong on 2018/10/6.
// Copyright © 2018年 Wenru Dong. All rights reserved.
//

#import "ListNode.h"

@implementation ListNode

- (instancetype)initWithValue:(int)value {
if (self = [super init]) {
_value = value;
}
return self;
}

+ (instancetype)nodeWithValue:(int)value {
return [[self alloc] initWithValue:value];
}

- (NSString*)debugDescription {
return [NSString stringWithFormat:@"%d", _value];
}

@end

0 comments on commit f74488c

Please sign in to comment.