Skip to content

Commit 619caad

Browse files
Merge pull request #23 from jerryderry/master
implementation of linked-list-based stack in python
2 parents 2880bf3 + f74488c commit 619caad

File tree

6 files changed

+218
-0
lines changed

6 files changed

+218
-0
lines changed

object-c/08_stack/LinkedStack.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// LinkedStack.h
3+
// algo
4+
//
5+
// Created by Wenru Dong on 2018/10/8.
6+
// Copyright © 2018年 Wenru Dong. All rights reserved.
7+
//
8+
// Stack based upon linked list
9+
// 基于链表实现的栈
10+
11+
#import <Foundation/Foundation.h>
12+
13+
@interface LinkedStack : NSObject
14+
15+
- (BOOL)isEmpty;
16+
- (void)push:(int)value;
17+
- (int)pop;
18+
19+
@end

object-c/08_stack/LinkedStack.m

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// LinkedStack.m
3+
// algo
4+
//
5+
// Created by Wenru Dong on 2018/10/8.
6+
// Copyright © 2018年 Wenru Dong. All rights reserved.
7+
//
8+
9+
#import "LinkedStack.h"
10+
#import "ListNode.h"
11+
12+
@implementation LinkedStack
13+
{
14+
@private
15+
ListNode* _top;
16+
}
17+
18+
- (BOOL)isEmpty {
19+
return _top == nil;
20+
}
21+
22+
- (void)push:(int)value {
23+
ListNode *newTop = [ListNode nodeWithValue:value];
24+
newTop.next = _top;
25+
_top = newTop;
26+
}
27+
28+
- (int)pop {
29+
if ([self isEmpty]) {
30+
[NSException raise:NSRangeException format:@"The stack is empty."];
31+
}
32+
int value = _top.value;
33+
_top = _top.next;
34+
return value;
35+
}
36+
37+
- (NSString *)debugDescription {
38+
NSMutableString *info = [[NSMutableString alloc] init];
39+
ListNode *current = _top;
40+
while (current) {
41+
[info appendString:[NSString stringWithFormat:@"%d]", current.value]];
42+
current = current.next;
43+
}
44+
return [NSString stringWithString:info];
45+
}
46+
47+
@end
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//
2+
// LinkedStackTests.m
3+
// LinkedStackTests
4+
//
5+
// Created by Wenru Dong on 2018/10/8.
6+
// Copyright © 2018年 Wenru Dong. All rights reserved.
7+
//
8+
9+
#import <XCTest/XCTest.h>
10+
#import "LinkedStack.h"
11+
12+
@interface LinkedStackTests : XCTestCase
13+
14+
@end
15+
16+
@implementation LinkedStackTests
17+
18+
- (void)setUp {
19+
[super setUp];
20+
// Put setup code here. This method is called before the invocation of each test method in the class.
21+
}
22+
23+
- (void)tearDown {
24+
// Put teardown code here. This method is called after the invocation of each test method in the class.
25+
[super tearDown];
26+
}
27+
28+
- (void)testPush {
29+
LinkedStack *stack = [[LinkedStack alloc] init];
30+
for (int i = 0; i < 10; i++) {
31+
[stack push:i];
32+
}
33+
XCTAssertEqualObjects([stack debugDescription], @"9]8]7]6]5]4]3]2]1]0]");
34+
}
35+
36+
- (void)testPop {
37+
LinkedStack *stack = [[LinkedStack alloc] init];
38+
for (int i = 0; i < 10; i++) {
39+
[stack push:i];
40+
}
41+
[stack pop];
42+
XCTAssertEqualObjects([stack debugDescription], @"8]7]6]5]4]3]2]1]0]");
43+
for (int i = 0; i < 9; i++) {
44+
[stack pop];
45+
}
46+
XCTAssertThrowsSpecificNamed([stack pop], NSException, NSRangeException, @"The stack is empty.");
47+
}
48+
49+
//- (void)testPerformanceExample {
50+
// // This is an example of a performance test case.
51+
// [self measureBlock:^{
52+
// // Put the code you want to measure the time of here.
53+
// }];
54+
//}
55+
56+
@end

object-c/08_stack/ListNode.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// ListNode.h
3+
// algo
4+
//
5+
// Created by Wenru Dong on 2018/10/6.
6+
// Copyright © 2018年 Wenru Dong. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
@interface ListNode : NSObject
12+
13+
@property int value;
14+
@property ListNode *next;
15+
16+
- (instancetype)initWithValue:(int)value;
17+
+ (instancetype)nodeWithValue:(int)value;
18+
19+
@end

object-c/08_stack/ListNode.m

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// ListNode.m
3+
// algo
4+
//
5+
// Created by Wenru Dong on 2018/10/6.
6+
// Copyright © 2018年 Wenru Dong. All rights reserved.
7+
//
8+
9+
#import "ListNode.h"
10+
11+
@implementation ListNode
12+
13+
- (instancetype)initWithValue:(int)value {
14+
if (self = [super init]) {
15+
_value = value;
16+
}
17+
return self;
18+
}
19+
20+
+ (instancetype)nodeWithValue:(int)value {
21+
return [[self alloc] initWithValue:value];
22+
}
23+
24+
- (NSString*)debugDescription {
25+
return [NSString stringWithFormat:@"%d", _value];
26+
}
27+
28+
@end

python/08_stack/linked_stack.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""
2+
Stack based upon linked list
3+
基于链表实现的栈
4+
5+
Author: Wenru
6+
"""
7+
8+
from typing import Optional
9+
10+
class Node:
11+
12+
def __init__(self, data: int, next=None):
13+
self._data = data
14+
self._next = next
15+
16+
17+
class LinkedStack:
18+
"""A stack based upon singly-linked list.
19+
"""
20+
def __init__(self):
21+
self._top: Node = None
22+
23+
def push(self, value: int):
24+
new_top = Node(value)
25+
new_top._next = self._top
26+
self._top = new_top
27+
28+
def pop(self) -> Optional[int]:
29+
if self._top:
30+
value = self._top._data
31+
self._top = self._top._next
32+
return value
33+
34+
def __repr__(self) -> str:
35+
current = self._top
36+
nums = []
37+
while current:
38+
nums.append(current._data)
39+
current = current._next
40+
return " ".join(f"{num}]" for num in nums)
41+
42+
if __name__ == "__main__":
43+
stack = LinkedStack()
44+
for i in range(9):
45+
stack.push(i)
46+
print(stack)
47+
for _ in range(3):
48+
stack.pop()
49+
print(stack)

0 commit comments

Comments
 (0)