###链表
链表的优点是:采用动态存储分配,不会造成内存浪费和溢出;
另外,链表执行插入和删除操作十分方便,修改指针即可,不需要移动大量元素
###oc 实现
#import <Foundation/Foundation.h>
@interface OCLinkList : NSObject {
id data;
OCLinkList *head;
OCLinkList *next;
}
@end
@implementation OCLinkList
- (void)createLinkList {
head = [[OCLinkList alloc] init];
head->next = nil;
OCLinkList *ptr = head;
for (int i=1; i<=20; i++){
OCLinkList *node = [[OCLinkList alloc] init];
node->data = [NSNumber numberWithInt:i];
node->next = nil;
ptr->next = node;
ptr = node;
}
}
- (void)outputLinkList {
OCLinkList *ptr = head->next;
while (ptr != nil) {
printf("%d ", [ptr->data intValue]);
ptr = ptr->next;
}
printf("\n");
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
OCLinkList *list = [[OCLinkList alloc] init];
[list createLinkList];
[list outputLinkList];
}
return 0;
}