3

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.

7
  • 3
    Sure. Define a class that serves as a linked-list node, put the data in it you want, plus a pointer to the next node. For a doubly linked list you need pointers both directions. (It's convenient to define a different class that serves as the list header, with methods on it to add/find/remove nodes, etc.) Commented Sep 12, 2012 at 1:54
  • 1
    Is there a specific reason you need to create your own linked list? Will NSArray not do the job? If you really must create your own linked list you can look at any C linked list examples, Objective-C is a superset of C. Commented Sep 12, 2012 at 1:55
  • 1
    (BTW, a linked list is an excellent "learning opportunity". There's no rocket science involved -- the concepts are very simple -- but attention to detail is critical.) Commented Sep 12, 2012 at 1:57
  • 1
    Ah, did't realize it was for school... Linked lists are fun! And then you'll have a deep appreciation for what NSDictionaries and NSMutableArray are doing for you :-) Commented Sep 12, 2012 at 2:07
  • 1
    Like I said, with linked lists attention to detail is critical. Adding the first entry or removing the last is different from the others, and several other "gotchas". Nothing hard to understand, except that it easy to miss a detail if you're not really careful. (And even if you are careful you're still bound to miss something -- after 40 years I still expect a linked list to give me some "interesting" problem.) Commented Sep 12, 2012 at 2:08

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

On the other hand, I have had several problems where a linked list was, if not the only way, the most straight-forward and efficient way. And it's a good technique to learn -- something to have in your "toolkit".
Thank you. I shall try using this.
Yes, I am certainly an advocate of learning with the linked list and really the notion of dynamic data structures. I got the impression that OP was familiar with linked lists but not Objective-C... Still it seems fun to do a more robust abstract linked list implementation with objects.
Agreed... Coding your own linked list once is "Programming 101". Every one should do it once.
Agreed ... in part. Because learning a functional language (lisp, scheme etc.) should also be considered programming 101. And then you probably should concentrate on using lists instead of implementing them :p
0

Please check my implementation of some of the common data structures like linked list, stack, binary search tree in objective C. https://github.com/udaypatial/Data-Structures-in-Objective-C

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.