4

Possible Duplicate:
Creating Linked Lists in Objective C

I am trying to understand what a linked list is. Is there anyway of implementing it in Objective-C or some sample code?

Community
  • 1
  • 1
TheLearner
  • 19,387
  • 35
  • 95
  • 163
  • try this link.. http://stackoverflow.com/questions/4262223/creating-linked-lists-in-objective-c – AppleDelegate Sep 17 '12 at 10:40
  • 1
    implementing it yourself would be a pretty good way to learn? just use a [generic reference](http://en.wikipedia.org/wiki/Linked_list) to understand what they are – wattson12 Sep 17 '12 at 10:40
  • please referred this link [http://stackoverflow.com/questions/4262223/creating-linked-lists-in-objective-c?answertab=active#tab-top][1] [1]: http://stackoverflow.com/questions/4262223/creating-linked-lists-in-objective-c?answertab=active#tab-top – Romit M. Sep 17 '12 at 10:45
  • @RomitMewada but wouldn't you still need something to store all these 'MyEvent' objects? Like an Array? – TheLearner Sep 17 '12 at 10:48

2 Answers2

7

Linked list are useful in C to handle dynamic-length lists. This is not an issue in objective-C as you have NSMutableArray.

So the equivalent of:

struct my_list {
  int value;
  struct my_list *next;
};
struct my_list list1;
struct my_list list2;
list1.next = &list2;
list1.value = 5;
list2.value = 10;

Would be:

NSMutableArray* array = [[NSMutableArray alloc] init];
[array addObject:[NSNumber numberWithInt:5]];
[array addObject:[NSNumber numberWithInt:10]];

Of course, you can use classic linked-list in an objective-c application.

ldiqual
  • 15,015
  • 6
  • 52
  • 90
  • 3
    It is an issue for certain use cases. It's much faster to add items to the beginning of a linked list, as the rest of the list does not have to be touched. In an array they'd have to be shifted backwards. Also, in certain (immutable) data structures you can share common branches easily. Of course, if all you do is add items to the _end_, then an NSMutableArray would be better. – Oliver Mason Mar 11 '14 at 16:50
  • Even when adding to the end linked lists are superior to arrays. `NSMutableArray`s have a certain **capacity** which is the number of elements that it can contain without reallocating. Appending to linked lists performs in `O(1)`, while appending to the end of arrays takes `amortized O(1)`, which is different. So no, using `NSMutableArray` is not the same thing in all accounts. However, if performance is not an issue, go ahead. If it is, you should consider what you'll be doing most often, because indexing in linked lists is slow (`O(n)`), for instance. – André Fratelli Jul 09 '15 at 01:09
  • What does this line do: `list1.next = &list2;`? – ArielSD Jun 02 '17 at 15:05
  • 1
    @ArielSD assigns the address of `list2` struct to the `next` field of `list1`. – Dan M. Jul 04 '17 at 04:16
2

there is no need to use of array. MyEvent object also conitan next MyEvent. so you just create next of next object. so just you need to init.

e.g.

MyEvent *obj = [[MyEvent alloc]init];

child node.

obj.nextEvent = [[MyEvent alloc]init];
Romit M.
  • 898
  • 11
  • 28