Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linked list creation in objective c

Tags:

objective-c

Is there a way to create linked list in objective c. I'm a newbie and so far I've researched in apple developer guides, there isn't any function predefined for linked list. Is doubly linked list same as linked list in objective-c?

Please help.

like image 319
user1661826 Avatar asked Aug 31 '26 11:08

user1661826


1 Answers

First thing to keep in mind is that Objective-C is C, it's just a lot more, too.

Next thing is that Objects are passed around as (essentially) pointers (which the compiler knows point to objects).

So you can certainly make and manage your own linked list, and you can do it with structs or objects. With objects (perhaps preferred), just create a @property in the class for the "next" object, and use it as you please. Similarly for a doubly-linked list, have a @property for previous.

Perhaps one of the best arguments against caring about liked lists in Objective-C is that usually you have the Cocoa Frameworks at hand, and there is such rich host of features which we used to have to implement ourselves with linked lists. For instance, the conceptually simple NSMutableArray, or NSDictionary are great examples of well-built components which usually spare us the need for linked lists. Going further, Core Data, etc...

A simple abstract linked list class might look like this:

@interface LinkedNode : NSObject 
  @property (nonatomic, strong) id nextNode;
@end

then you use it as you would expect:

id currentNode = myFirstNode;
do {
  [currentNode someMessage];
}
while(currentNode = currentNode.nextNode);

Keep in mind that this is really no "better" than doing it with structs. For the "better" business, move to the Cocoa classes and implement at a "higher level", so-to-speak.

like image 182
Chris Trahey Avatar answered Sep 03 '26 05:09

Chris Trahey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!