Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I implement a linked list in objective-c? [duplicate]

Tags:

objective-c

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?

like image 307
TheLearner Avatar asked Jan 16 '23 13:01

TheLearner


1 Answers

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.

like image 68
ldiqual Avatar answered Jan 18 '23 23:01

ldiqual