forked from wangzheng0822/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implementation of linked-list-based stack in objective-c
- Loading branch information
1 parent
50c35b7
commit f74488c
Showing
5 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
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
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 |
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
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 |
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
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 |
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
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 |
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
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 |