在iOS开发中,可以使用Objective-C或者Swift来实现顺序链表。以下是一个简单的Objective-C实现: 首先,创建一个Node类,表示链表中的每个节点: ```objective-c @interface Node : NSObject @property (nonatomic, strong) id value; @property (nonatomic, strong) Node *next; - (instancetype)initWithValue:(id)value; @end @implementation Node - (instancetype)initWithValue:(id)value { self = [super init]; if (self) { _value = value; _next = nil; } return self; } @end ``` 然后,创建一个LinkedList类,表示顺序链表: ```objective-c @interface LinkedList : NSObject @property (nonatomic, strong) Node *head; - (instancetype)init; - (void)addNodeWithValue:(id)value; - (void)removeNodeWithValue:(id)value; - (id)getValueAtPosition:(NSInteger)position; @end @implementation LinkedList - (instancetype)init { self = [super init]; if (self) { _head = nil; } return self; } - (void)addNodeWithValue:(id)value { Node *newNode = [[Node alloc] initWithValue:value]; if (!self.head) { self.head = newNode; } else { Node *current = self.head; while (current.next) { current = current.next; } current.next = newNode; } } - (void)removeNodeWithValue:(id)value { if (!self.head) { return; } if ([self.head.value isEqual:value]) { self.head = self.head.next; return; } Node *current = self.head; while (current.next) { if ([current.next.value isEqual:value]) { current.next = current.next.next; return; } current = current.next; } } - (id)getValueAtPosition:(NSInteger)position { Node *current = self.head; for (NSInteger i = 0; i < position && current; i++) { current = current.next; } return current ? current.value : nil; } @end ``` 以上代码中,LinkedList类的head属性指向链表的第一个节点。addNodeWithValue方法在链表的末尾添加一个新节点。removeNodeWithValue方法从链表中删除具有给定值的节点。getValueAtPosition方法返回链表中指定位置的节点的值。 [2023-11-08 00:28:50 | AI问答 | 519点数解答]