学习之余用OC写了个链表,由于之前一直写C语言,所以用了两种写法实现,两种都是类的方法实现不过实现方式不一样
第一种是变相的结构体写法,里面有浓浓的C语言结构体的味道,另一种就是普通两种类的写法
先看一下第一种写法
PS:一个SHNode类和一盒main
SHNode.h:
#import <Foundation/Foundation.h>
@interface SHNode : NSObject
@property SHNode *head;//链表的头,学懂了OC的类其实这个头可以省略,不过现在为了便于理解就写上了
@property SHNode *lastNode;//链表的当前的最后一个结点,因为链表采用的是尾插法,以前一直用的头插法这次换个写法
@property int data;//链表中要存的数据,可以自定义多个
@property SHNode *next;//指向下一个结点
-(void)append:(int)data;//插入结点的方法
-(void)print;//打印链表的方法
+(id)nodeList;//初始化链表的工厂方法,这里可以不用这个,只不过用了会方便一些,牵扯到设计模式就不多说了
@end
</pre><pre name="code" class="objc">SHNode.m:
#import "SHNode.h"
@implementation SHNode
/*
初始化头
*/
-(instancetype)init
{
if (self = [super init]) {
self.data = 0;
self.next = nil;
self.lastNode = self;
self.head = self;
}
return self.lastNode;
}
/*
往链表中插入结点
*/
-(void)append:(int)data
{
SHNode *node = [SHNode alloc];
node.next = self.lastNode.next;
self.lastNode.next = node;
node.data = data;
self.lastNode = node;
}
+(id)nodeList
{
SHNode *firstNode = [[SHNode alloc]init];
return firstNode;
}
/*
打印整个链表
*/
-(void)print
{
SHNode *head2 = self.head;//其实这里不需要写self.head,直接写SHNode *head2 = self也是可以的
while (head2!=nil) {
printf("%d ",head2.data);
head2 = head2.next;
}
}
@end
</pre><pre name="code" class="objc">main.m:
#import <Foundation/Foundation.h>
#import "SHNode.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
SHNode *head = [SHNode nodeList];
clock_t begin = clock();
for (int i = 1; i < 2000000; i++) {
[head append:i];
}
[head print];
clock_t end = clock();
unsigned long int timeUse = end - begin;//这里是测试创建链表耗费的时间
printf("\n\n%lu\n\n",timeUse);
printf("\n");
}
return 0;
}
总结:这个类纯粹是C语言中结构体的写法

下面来看下纯类的写法:
这个写法分成两个类,一个链表类一个结点类
先看下结点类:
SHNode.h
#import <Foundation/Foundation.h>
@interface SHNode : NSObject
@property int data;//结点中的数据
@property SHNode *next;//下一个结点的地址
- (id)initWithData:(int)data;//结点初始化
@end
</pre><pre name="code" class="objc">SHNode.m
<pre name="code" class="objc">#import "SHNode.h"
@implementation SHNode
- (instancetype)init
{
self = [super init];
if (self) {
self.data = 0;
self.next = nil;
}
return self;
}
- (id)initWithData:(int)data
{
self = [super init];
if (self) {
self.data = data;
self.next = nil;
}
return self;
}
@end
然后是链表类;
SHList.h
#import <Foundation/Foundation.h>
#import "SHNode.h"
@interface SHList : NSObject
@property SHNode *head;//定义链表的头
@property SHNode *hail;//定义链表的尾巴
-(void)printList;//打印链表
-(void)append:(int)data;//插入结点
+(id)listWithData:(int)data;//工厂方法
@end
</pre><pre name="code" class="objc">SHList.m
<pre name="code" class="objc">#import "SHList.h"
@implementation SHList
- (instancetype)initWithData:(int)data
{
self = [super init];
if (self) {
self.head = [[SHNode alloc]init];
self.head.data = data;
self.head.next = nil;
self.hail = self.head;
}
return self;
}
+(id)listWithData:(int)data
{
SHList *firstNode = [[SHList alloc]initWithData:data];
return firstNode;
}
-(void)append:(int)data
{
SHNode *node = [SHNode alloc];
node.data = data;
node.next = self.hail.next;
self.hail.next = node;
self.hail = node;
}
-(void)printList
{
SHNode *list;
list = self.head;
while (list != nil) {
printf("%d ",list.data);
list = list.next;
}
}
@end
接下来就是main函数啦

#import <Foundation/Foundation.h>
#import "SHList.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
SHList *newList = [SHList listWithData:0];
for (int i = 1; i < 200000; i ++) {
[newList append:i];
}
[newList printList];
printf("\n");
}
return 0;
}
以上就是所有的代码,如有不对之处还望指正


原创作品,允许转载,转载请以超链接形式指明出处