如何在objective-c中实现链接列表?(复制)

时间:2022-09-02 09:39:29

Possible Duplicate:
Creating Linked Lists in Objective C

可能的重复:在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?

我试着去理解一个链表是什么。在Objective-C中有没有实现它,或者是一些样本代码?

2 个解决方案

#1


7  

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

链表在C中是有用的,用于处理动态长度列表。这不是objective-C中的问题,因为你有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.

当然,您可以在objective-c应用程序中使用经典的链接列表。

#2


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.

不需要使用数组。MyEvent对象也可以是下一个MyEvent。你只需要创建下一个对象。你需要init。

e.g.

如。

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

child node.

子节点。

obj.nextEvent = [[MyEvent alloc]init];

#1


7  

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

链表在C中是有用的,用于处理动态长度列表。这不是objective-C中的问题,因为你有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.

当然,您可以在objective-c应用程序中使用经典的链接列表。

#2


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.

不需要使用数组。MyEvent对象也可以是下一个MyEvent。你只需要创建下一个对象。你需要init。

e.g.

如。

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

child node.

子节点。

obj.nextEvent = [[MyEvent alloc]init];