Skip to content

Commit

Permalink
Merge pull request #93 from cuongtv51/Enhance-LinkedList
Browse files Browse the repository at this point in the history
Update issue #8, Enhance count function of LinkedList
  • Loading branch information
EvgenyKarkan committed Jan 20, 2015
2 parents ea1d34b + a734a62 commit 9d33a36
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions EKAlgorithms/Data Structures/LinkedList/EKLinkedList.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
#import "NSObject+EKComparisonForIOS.h"
#endif

@interface EKLinkedList ()
{
NSUInteger _count;
}

@end

@implementation EKLinkedList;

Expand All @@ -22,6 +28,7 @@ - (instancetype)initWithHead:(NSObject *)value

if (self) {
_head = [[EKNode alloc] initWithObject:value];
_count = 1;
}
return self;
}
Expand All @@ -32,14 +39,17 @@ - (void)addToFront:(NSObject *)value

if (self.tail == nil) {
EKNode *lastNode = self.head;
for (NSUInteger i = 1; i < self.count; i++) {
for (NSUInteger i = 1; i < _count; i++) {
lastNode = lastNode.next;
}
self.tail = lastNode;

}
node.next = self.head;
self.head.previous = node;
self.head = node;

_count++;
}

- (void)addToBack:(NSObject *)value
Expand All @@ -48,14 +58,16 @@ - (void)addToBack:(NSObject *)value

if (self.tail == nil) {
EKNode *lastNode = self.head;
for (NSUInteger i = 1; i < self.count; i++) {
for (NSUInteger i = 1; i < _count; i++) {
lastNode = lastNode.next;
}
self.tail = lastNode;
}
node.previous = self.tail;
self.tail.next = node;
self.tail = node;

_count++;
}

- (void)insertObject:(NSObject *)object atIndex:(NSUInteger)index
Expand Down Expand Up @@ -86,6 +98,7 @@ - (void)insertObject:(NSObject *)object atIndex:(NSUInteger)index
nextNode.previous = newNode;
newNode.next = nextNode;
}
_count++;
}

- (NSObject *)first
Expand Down Expand Up @@ -114,18 +127,7 @@ - (NSObject *)previous

- (NSUInteger)count
{
if (!self.head) {
return 0;
}

EKNode *currentNode = self.head;
NSUInteger i = 1;

while (currentNode.next) {
currentNode = currentNode.next;
i++;
}
return i;
return _count;
}

- (NSObject *)objectAtIndex:(NSUInteger)index
Expand Down Expand Up @@ -158,13 +160,14 @@ - (NSArray *)findObject:(NSObject *)object

- (BOOL)removeCurrent
{
//FIXME: improve code below
//FIXME: improve code below
NSLog(@"<# #> %@", [self currentValue]);

BOOL removed = NO;
if (self.current != nil) {
self.current.previous.next = self.current.next;
removed = YES;
_count--;
}
else {
removed = NO;
Expand All @@ -190,6 +193,7 @@ - (BOOL)removeObjectAtIndex:(NSUInteger)index
}

current.next = current.next.next;
_count--;

return YES;
}
Expand Down

0 comments on commit 9d33a36

Please sign in to comment.