Skip to content
Browse files

insertObject: before/after/between methods

  • Loading branch information...
1 parent ccb691a commit a528057bdfca80ca43dfeaa0c3b59d61cd94a716 @illoyd committed Mar 26, 2011
Showing with 35 additions and 0 deletions.
  1. +4 −0 NSLinkedList.h
  2. +31 −0 NSLinkedList.m
View
4 NSLinkedList.h
@@ -46,6 +46,10 @@ struct LNode {
- (void)pushNodeBack:(LNode *)n; // adds a node object to the end of the list
- (void)pushNodeFront:(LNode *)n; // adds a node object to the begining of the list
+// Insert objects
+- (void)insertObject:(id)anObject beforeNode:(LNode *)node;
+- (void)insertObject:(id)anObject afterNode:(LNode *)node;
+- (void)insertObject:(id)anObject betweenNode:(LNode *)previousNode andNode:(LNode *)nextNode;
//- (void)replaceObjectAtIndex:(int) withObject:(id); // replaces object at a given index with the passed object
View
31 NSLinkedList.m
@@ -71,6 +71,37 @@ - (void)pushFront:(id)anObject {
}
+- (void)insertObject:(id)anObject beforeNode:(LNode *)node {
+ [self insertObject:anObject betweenNode:node->prev andNode:node];
+}
+
+
+- (void)insertObject:(id)anObject afterNode:(LNode *)node {
+ [self insertObject:anObject betweenNode:node andNode:node->next];
+}
+
+
+- (void)insertObject:(id)anObject betweenNode:(LNode *)previousNode andNode:(LNode *)nextNode {
+
+ if (anObject == nil) return;
+
+ LNode *n = LNodeMake(anObject, nextNode, previousNode);
+
+ if (previousNode)
+ previousNode->next = n;
+ else
+ first = n;
+
+ if (nextNode)
+ nextNode->prev = n;
+ else
+ last = n;
+
+ size++;
+
+}
+
+
- (void)addObject:(id)anObject {
[self pushBack:anObject];
}

0 comments on commit a528057

Please sign in to comment.
Something went wrong with that request. Please try again.