Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ac10c59

Browse files
author
penghe
committed
day02 bug fix
1 parent 7d60cfc commit ac10c59

File tree

4 files changed

+19
-19
lines changed

4 files changed

+19
-19
lines changed

‎Day02/DataStructure_Stack.xcodeproj/xcuserdata/penghe.xcuserdatad/xcschemes/xcschememanagement.plist‎

Lines changed: 0 additions & 14 deletions
This file was deleted.

‎Day02/DataStructure_Stack/DSStack.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@
2121
- (void)clear;
2222
- (NSArray *)allObjectsFromStack;
2323

24+
- (void)compressedStack;
25+
2426
@end

‎Day02/DataStructure_Stack/DSStack.m‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,15 @@ - (void)push:(id)object
6969
if ([self isFull] && self.maxStackSize) {
7070

7171
NSMutableArray *newArray = [[NSMutableArray alloc] initWithCapacity:_maxStackSize*2];
72-
self.stackArray = [newArray mutableCopy];
72+
for (id object in self.stackArray)
73+
{
74+
@autoreleasepool {
75+
[newArray addObject:object];
76+
}
77+
}
78+
self.stackArray = newArray;
7379
_maxStackSize = _maxStackSize*2;
74-
80+
7581
}
7682
if (object != nil) {
7783
[self.stackArray addObject:object];
@@ -129,7 +135,13 @@ - (void)compressedStack
129135
int stackSize = (int)(self.stackArray.count);
130136
if( stackSize < capacitySize ) {
131137
NSMutableArray *newArray = [[NSMutableArray alloc] initWithCapacity:stackSize];
132-
self.stackArray = [newArray mutableCopy];
138+
for (id object in self.stackArray)
139+
{
140+
@autoreleasepool {
141+
[newArray addObject:object];
142+
}
143+
}
144+
self.stackArray = newArray;
133145
_maxStackSize = stackSize;
134146

135147
}

‎Day02/DataStructure_Stack/ViewController.m‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ @implementation ViewController
1919
- (void)viewDidLoad {
2020
[super viewDidLoad];
2121
// Do any additional setup after loading the view, typically from a nib.
22-
DSStack *testStack = [[DSStack alloc] initWithSize:4];
22+
DSStack *testStack = [[DSStack alloc] initWithSize:10];
2323
[testStack push:@"1"];
2424
[testStack push:@"2"];
2525
[testStack push:@"5"];
2626
NSLog(@"%@",testStack);
2727
[testStack popLastObject];
28-
28+
[testStack compressedStack];
2929

3030
}
3131

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /