I will try to code from scratch in Objective-C some of the basic database structures and talk a little bit about them. This week I will start with a linked list.
If you are familiar with arrays you are familiar with liked lists. A linked list is a list of nodes, each node stores an object (may be an integer, a double, or any complicated class like a molecule) and a link or pointer to the next object in the list. You might think of it like a shoe box storing something (some data). Each shoe box have a string connected to another shoe box, if you want to locate something you just have to look for it on each box one at the time (following the string so you won't look in the same box twice). I will talk more about linked lists in a next post, but now lets code the nodes.
We will implement a linked list in Objective C.
First make the interface file and name it "LinkedListNode.h"
//2007 chuyandmac.blogspot.com, LinkedListNode.h
//Interface file for the a node to be
//used in a linked list data structure.
#import
@interface LinkedListNode : NSObject {
double value;
LinkedListNode *next;
}
-(id)initWithValue:(double)v;
-(void)setNextNode:(LinkedListNode *)n;
-(void)setValue:(double)v;
-(double)value;
-(LinkedListNode *)next;
@end
Second make the implementation file "LinkedListNode.m"
//2007 chuyandmac.blogspot.com, LinkedListNode.m
//Implementation file for the a node to
//be used in a linked list data structure.
#import "LinkedListNode.h"
@implementation LinkedListNode
-(id)initWithValue:(double) v{
if(self = [super init]){
[self setValue:v];
[self setNextNode:nil];
}
return self;
}
-(void)setNextNode:(LinkedListNode *)n{
next = n;
return;
}
-(void)setValue:(double)v{
value = v;
}
-(double)value{return value;}
-(LinkedListNode *)next{return next;}
@end
Finally make sure they work as expected, make the file "linkedListNode.m"
//2007 chuyandmac.blogspot.com, linkedListNodeTest.m
//Test LinkedListNode.m and LinkedListNode.h
//To compile use
//gcc -ObjC LinkedListNode.m linkedListNodeTest.m -framework Foundation
#import "./LinkedListNode.h"
int main(){
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLog(@"Testing LinkedListNode.h");
LinkedListNode *node = [[LinkedListNode alloc] initWithValue:2.0];
LinkedListNode *node2 = [[LinkedListNode alloc] initWithValue:3.0];
[node setNextNode:node2];
NSLog(@"node was initialiazed with value %f.",[node value]);
NSLog(@"the second node is linked an has a value %f."
,[[node next] value]);
[node release];
[node2 release];
[pool release];
}
Make sure everything is in the same folder and compile typing the following command in terminal.
gcc -ObjC LinkedListNode.m linkedListNodeTest.m -framework Foundation
An executable named a.out will be created, you can execute it typing in the terminal.
./a.out
The output will look like this. Enjoy!
2007-10-04 23:30:41.786 a.out[609]
Testing LinkedListNode.h
2007-10-04 23:30:41.786 a.out[609]
node was initialiazed with value 2.000000.
2007-10-04 23:30:41.786 a.out[609]
the second node is linked an has a value 3.000000.
Note: I'm still learning ObjC and I'm not familiar yet with memory leaks, I might have to modify this code just as to make sure I don't have them. If you notice any memory leak in this code, please let me know.
No comments:
Post a Comment