Thursday, October 4, 2007

Scientific programing an database structures. The Linked List part 1/3

In my experience many times database structures are not necessary while doing basic scientific simulations. Most of the time we are dealing with fixed length vectors or matrices. Some of you might disagree with me and might point out that appropriate data management is always necessary. In any case is unfortunate that when the occasion arises this structures are often neglected.

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, LinkedListNode.h
//Interface file for the a node to be
//used in a linked list data structure.


@interface LinkedListNode : NSObject {
double value;
LinkedListNode *next;

-(void)setNextNode:(LinkedListNode *)n;
-(LinkedListNode *)next;


Second make the implementation file "LinkedListNode.m"

//2007, 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;

value = v;

-(double)value{return value;}
-(LinkedListNode *)next{return next;}


Finally make sure they work as expected, make the file "linkedListNode.m"

//2007, 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.


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: