浅谈iOS 数据结构之链表

 更新时间:2017年09月07日 10:10:01   作者:hi_xgb  
这篇文章主要介绍了浅谈iOS 数据结构之链表,本文详细的介绍了单链表和双链表,具有一定的参考价值,有兴趣的可以了解一下

链表(Linked List)是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的,表现形式如下图所示:

单链表

双链表

数组和链表区别:

  • 数组:数组元素在内存上连续存放,可以通过下标查找元素;插入、删除需要移动大量元素,比较适用于元素很少变化的情况
  • 链表:链表中的元素在内存中不是顺序存储的,查找慢,插入、删除只需要对元素指针重新赋值,效率高

Objective-C 里没有现成的链表结构,下面我实现了非线程安全的单链表和双链表,以下都是具体的实现细节,完整代码可以在这儿下载

单链表

单链表提供了插入、删除、查询、反转等操作,具体实现如下:

BBSingleLinkedList.h

#import <Foundation/Foundation.h>

@interface BBSingleLinkedNode : NSObject <NSCopying>

@property (nonatomic, strong) NSString *key;
@property (nonatomic, strong) NSString *value;
@property (nonatomic, strong) BBSingleLinkedNode *next;

- (instancetype)initWithKey:(NSString *)key
      value:(NSString *)value;

+ (instancetype)nodeWithKey:(NSString *)key
      value:(NSString *)value;

@end

@interface BBSingleLinkedList : NSObject

- (void)insertNode:(BBSingleLinkedNode *)node;
- (void)insertNodeAtHead:(BBSingleLinkedNode *)node;
- (void)insertNode:(BBSingleLinkedNode *)newNode beforeNodeForKey:(NSString *)key;
- (void)insertNode:(BBSingleLinkedNode *)newNode afterNodeForKey:(NSString *)key;

- (void)bringNodeToHead:(BBSingleLinkedNode *)node;

- (void)removeNode:(BBSingleLinkedNode *)node;

- (BBSingleLinkedNode *)nodeForKey:(NSString *)key;
- (BBSingleLinkedNode *)headNode;
- (BBSingleLinkedNode *)lastNode;

- (NSInteger)length;
- (BOOL)isEmpty;

- (void)reverse;
- (void)readAllNode;

@end

BBSingleLinkedList.m

#import "BBSingleLinkedList.h"

@implementation BBSingleLinkedNode

- (instancetype)initWithKey:(NSString *)key
      value:(NSString *)value
{
 if (self = [super init]) {
  _key = key;
  _value = value;
 }
 return self;
}

+ (instancetype)nodeWithKey:(NSString *)key
      value:(NSString *)value
{
 return [[[self class] alloc] initWithKey:key value:value];
}

#pragma mark - NSCopying
- (id)copyWithZone:(nullable NSZone *)zone
{
 BBSingleLinkedNode *newNode = [[BBSingleLinkedNode allocWithZone:zone] init];
 newNode.key = self.key;
 newNode.value = self.value;
 newNode.next = self.next;
 return newNode;
}

@end

@interface BBSingleLinkedList ()

@property (nonatomic, strong) BBSingleLinkedNode *headNode;
@property (nonatomic, strong) NSMutableDictionary *innerMap;

@end

@implementation BBSingleLinkedList

- (instancetype)init
{
 self = [super init];
 if (self) {
  _innerMap = [NSMutableDictionary new];
 }
 return self;
}

#pragma mark - public
- (void)insertNodeAtHead:(BBSingleLinkedNode *)node
{
 if (!node || node.key.length == 0) {
  return;
 }
 
 if ([self isNodeExists:node]) {
  return;
 }
 
 _innerMap[node.key] = node;
 if (_headNode) {
  node.next = _headNode;
  _headNode = node;
 } else {
  _headNode = node;
 }
}

- (void)insertNode:(BBSingleLinkedNode *)node
{
 if (!node || node.key.length == 0) {
  return;
 }
 
 if ([self isNodeExists:node]) {
  return;
 }
 
 _innerMap[node.key] = node;
 
 if (!_headNode) {
  _headNode = node;
  return;
 }
 BBSingleLinkedNode *lastNode = [self lastNode];
 lastNode.next = node;
}

- (void)insertNode:(BBSingleLinkedNode *)newNode beforeNodeForKey:(NSString *)key
{
 if (key.length == 0 || !newNode || newNode.key.length == 0) {
  return;
 }
 
 if ([self isNodeExists:newNode]) {
  return;
 }
 
 if (!_headNode) {
  _headNode = newNode;
  return;
 }
 
 BBSingleLinkedNode *node = _innerMap[key];
 if (node) {
  _innerMap[newNode.key] = newNode;
  BBSingleLinkedNode *aheadNode = [self nodeBeforeNode:node];
  if (aheadNode) {
   aheadNode.next = newNode;
   newNode.next = node;
  } else {
   _headNode = newNode;
   newNode.next = node;
  }
  
 } else {
  [self insertNode:newNode]; //insert to tail
 }
}

- (void)insertNode:(BBSingleLinkedNode *)newNode afterNodeForKey:(NSString *)key
{
 if (key.length == 0 || !newNode || newNode.key.length == 0) {
  return;
 }
 
 if ([self isNodeExists:newNode]) {
  return;
 }
 
 if (!_headNode) {
  _headNode = newNode;
  return;
 }
 
 BBSingleLinkedNode *node = _innerMap[key];
 if (node) {
  _innerMap[newNode.key] = newNode;
  newNode.next = node.next;
  node.next = newNode;
 } else {
  [self insertNode:newNode]; //insert to tail
 }
}

- (void)removeNode:(BBSingleLinkedNode *)node
{
 if (!node || node.key.length == 0) {
  return;
 }
 [_innerMap removeObjectForKey:node.key];
 
 if (node.next) {
  node.key = node.next.key;
  node.value = node.next.value;
  node.next = node.next.next;
 } else {
  BBSingleLinkedNode *aheadNode = [self nodeBeforeNode:node];
  aheadNode.next = nil;
 }
}

- (void)bringNodeToHead:(BBSingleLinkedNode *)node
{
 if (!node || !_headNode) {
  return;
 }
 
 if ([node isEqual:_headNode]) {
  return;
 }
 
 BBSingleLinkedNode *aheadNode = [self nodeBeforeNode:node];
 aheadNode.next = node.next;
 node.next = _headNode;
 _headNode = node;
}

- (BBSingleLinkedNode *)nodeForKey:(NSString *)key
{
 if (key.length == 0) {
  return nil;
 }
 BBSingleLinkedNode *node = _innerMap[key];
 return node;
}

- (BBSingleLinkedNode *)headNode
{
 return _headNode;
}

- (BBSingleLinkedNode *)lastNode
{
 BBSingleLinkedNode *last = _headNode;
 while (last.next) {
  last = last.next;
 }
 return last;
}

- (void)reverse
{
 BBSingleLinkedNode *prev = nil;
 BBSingleLinkedNode *current = _headNode;
 BBSingleLinkedNode *next = nil;
 
 while (current) {
  next = current.next;
  current.next = prev;
  prev = current;
  current = next;
 }
 
 _headNode = prev;
}

- (void)readAllNode
{
 BBSingleLinkedNode *tmpNode = _headNode;
 while (tmpNode) {
  NSLog(@"node key -- %@, node value -- %@", tmpNode.key, tmpNode.value);
  tmpNode = tmpNode.next;
 }
}

- (NSInteger)length
{
 NSInteger _len = 0;
 for (BBSingleLinkedNode *node = _headNode; node; node = node.next) {
  _len ++;
 }
 return _len;
}

- (BOOL)isEmpty
{
 return _headNode == nil;
}

#pragma mark - private
- (BBSingleLinkedNode *)nodeBeforeNode:(BBSingleLinkedNode *)node
{
 BBSingleLinkedNode *targetNode = nil;
 BBSingleLinkedNode *tmpNode = _headNode;
 while (tmpNode) {
  if ([tmpNode.next isEqual:node]) {
   targetNode = tmpNode;
   break;
  } else {
   tmpNode = tmpNode.next;
  }
 }
 return targetNode;
}

- (BOOL)isNodeExists:(BBSingleLinkedNode *)node
{
 BBSingleLinkedNode *tmpNode = _headNode;
 while (tmpNode) {
  if ([tmpNode isEqual:node]) {
   return YES;
  }
  tmpNode = tmpNode.next;
 }
 return false;
}

@end

双链表

双链表提供了插入、删除、查询操作,具体实现如下:

BBLinkedMap.h

#import <Foundation/Foundation.h>

@interface BBLinkedNode : NSObject <NSCopying>

@property (nonatomic, strong) NSString *key;
@property (nonatomic, strong) NSString *value;
@property (nonatomic, strong) BBLinkedNode *prev;
@property (nonatomic, strong) BBLinkedNode *next;

- (instancetype)initWithKey:(NSString *)key
      value:(NSString *)value;

+ (instancetype)nodeWithKey:(NSString *)key
      value:(NSString *)value;

@end

@interface BBLinkedMap : NSObject

- (void)insertNodeAtHead:(BBLinkedNode *)node;
- (void)insertNode:(BBLinkedNode *)node;
- (void)insertNode:(BBLinkedNode *)newNode beforeNodeForKey:(NSString *)key;
- (void)insertNode:(BBLinkedNode *)newNode afterNodeForKey:(NSString *)key;
- (void)bringNodeToHead:(BBLinkedNode *)node;

- (void)readAllNode;
- (void)removeNodeForKey:(NSString *)key;
- (void)removeTailNode;

- (NSInteger)length;
- (BOOL)isEmpty;

- (BBLinkedNode *)nodeForKey:(NSString *)key;
- (BBLinkedNode *)headNode;
- (BBLinkedNode *)tailNode;

@end

BBLinkedMap.m

#import "BBLinkedMap.h"

@implementation BBLinkedNode

- (instancetype)initWithKey:(NSString *)key
      value:(NSString *)value
{
 if (self = [super init]) {
  _key = key;
  _value = value;
 }
 return self;
}

+ (instancetype)nodeWithKey:(NSString *)key
      value:(NSString *)value
{
 return [[[self class] alloc] initWithKey:key value:value];
}

#pragma mark - NSCopying
- (id)copyWithZone:(nullable NSZone *)zone
{
 BBLinkedNode *newNode = [[BBLinkedNode allocWithZone:zone] init];
 newNode.key = self.key;
 newNode.value = self.value;
 newNode.prev = self.prev;
 newNode.next = self.next;
 return newNode;
}

@end

@interface BBLinkedMap ()

@property (nonatomic, strong) BBLinkedNode *headNode;
@property (nonatomic, strong) BBLinkedNode *tailNode;
@property (nonatomic, strong) NSMutableDictionary *innerMap;

@end

@implementation BBLinkedMap

- (instancetype)init
{
 self = [super init];
 if (self) {
  _innerMap = [NSMutableDictionary new];
 }
 return self;
}

#pragma mark - public
- (void)insertNodeAtHead:(BBLinkedNode *)node
{
 if (!node || node.key.length == 0) {
  return;
 }
 
 if ([self isNodeExists:node]) {
  return;
 }
 
 _innerMap[node.key] = node;
 
 if (_headNode) {
  node.next = _headNode;
  _headNode.prev = node;
  _headNode = node;
 } else {
  _headNode = _tailNode = node;
 }
}

- (void)insertNode:(BBLinkedNode *)node
{
 if (!node || node.key.length == 0) {
  return;
 }
 
 if ([self isNodeExists:node]) {
  return;
 }
 
 if (!_headNode && !_tailNode) {
  _headNode = _tailNode = node;
  return;
 }
 
 _innerMap[node.key] = node;
 
 _tailNode.next = node;
 node.prev = _tailNode;
 _tailNode = node;
}

- (void)insertNode:(BBLinkedNode *)newNode beforeNodeForKey:(NSString *)key
{
 if (key.length == 0 || !newNode || newNode.key.length == 0) {
  return;
 }
 
 if ([self isNodeExists:newNode]) {
  return;
 }
 
 if (!_headNode && !_tailNode) {
  _headNode = _tailNode = newNode;
  return;
 }
 
 BBLinkedNode *node = _innerMap[key];
 if (node) {
  _innerMap[newNode.key] = newNode;
  if (node.prev) {
   node.prev.next = newNode;
   newNode.prev = node.prev;
  } else {
   _headNode = newNode;
  }
  node.prev = newNode;
  newNode.next = node;
 } else {
  [self insertNode:newNode]; //insert to tail
 }
 
}

- (void)insertNode:(BBLinkedNode *)newNode afterNodeForKey:(NSString *)key
{
 if (key.length == 0 || !newNode || newNode.key.length == 0) {
  return;
 }
 
 if ([self isNodeExists:newNode]) {
  return;
 }
 
 if (!_headNode && !_tailNode) {
  _headNode = _tailNode = newNode;
  return;
 }
 
 BBLinkedNode *node = _innerMap[key];
 if (node) {
  _innerMap[newNode.key] = newNode;
  if (node.next) {
   newNode.next = node.next;
   node.next.prev = newNode;
  } else {
   _tailNode = newNode;
  }
  newNode.prev = node;
  node.next = newNode;
 } else {
  [self insertNode:newNode]; //insert to tail
 }
}

- (void)bringNodeToHead:(BBLinkedNode *)node
{
 if (!node) {
  return;
 }
 if (!_headNode && !_tailNode) {
  _headNode = _tailNode = node;
  return;
 }
 
 if ([_headNode isEqual:node]) {
  return;
 }
 
 if ([_tailNode isEqual:node]) {
  _tailNode = node.prev;
  _tailNode.next = nil;
 } else {
  node.prev.next = node.next;
  node.next.prev = node.prev;
 }
 
 _headNode.prev = node;
 node.next = _headNode;
 node.prev = nil;
 _headNode = node;
}

- (void)removeNodeForKey:(NSString *)key
{
 if (key.length == 0) {
  return;
 }
 
 BBLinkedNode *node = _innerMap[key];
 if (!node) {
  return;
 }
 
 [_innerMap removeObjectForKey:key];
 
 if ([_headNode isEqual:node]) {
  _headNode = node.next;
 } else if ([_tailNode isEqual:node]) {
  _tailNode = node.prev;
 }
 
 node.prev.next = node.next;
 node.next.prev = node.prev;
 
}

- (void)removeTailNode
{
 if (!_tailNode || _tailNode.key.length == 0) {
  return;
 }
 
 [_innerMap removeObjectForKey:_tailNode.key];
 if (_headNode == _tailNode) {
  _headNode = _tailNode = nil;
  return;
 }
 
 _tailNode = _tailNode.prev;
 _tailNode.next = nil;
}

- (BBLinkedNode *)nodeForKey:(NSString *)key
{
 if (key.length == 0) {
  return nil;
 }
 BBLinkedNode *node = _innerMap[key];
 return node;
}

- (BBLinkedNode *)headNode
{
 return _headNode;
}

- (BBLinkedNode *)tailNode
{
 return _tailNode;
}

- (void)readAllNode
{
 BBLinkedNode *node = _headNode;
 while (node) {
  NSLog(@"node key -- %@, node value -- %@", node.key, node.value);
  node = node.next;
 }
}

- (NSInteger)length
{
 NSInteger _len = 0;
 for (BBLinkedNode *node = _headNode; node; node = node.next) {
  _len ++;
 }
 return _len;
}

- (BOOL)isEmpty
{
 return _headNode == nil;
}

#pragma mark - private
- (BOOL)isNodeExists:(BBLinkedNode *)node
{
 BBLinkedNode *tmpNode = _headNode;
 while (tmpNode) {
  if ([tmpNode isEqual:node]) {
   return YES;
  }
  tmpNode = tmpNode.next;
 }
 return false;
}

@end

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • ios app重提提交审核流程

    ios app重提提交审核流程

    本篇文章给大家讲述了在APP第一次没有审核通过后,重新提交的流程和注意的地方,学习一下吧。
    2017-12-12
  • 详解IOS图层转场动画

    详解IOS图层转场动画

    这篇文章主要为大家详细介绍了IOS图层转场动画, CATransition类实现层的转场动画,能够为层提供移出屏幕和移入屏幕的动画效果,感兴趣的小伙伴们可以参考一下
    2016-02-02
  • ios App加载本地HTML网页,点击网页链接跳转到app页面的方法

    ios App加载本地HTML网页,点击网页链接跳转到app页面的方法

    下面小编就为大家分享一篇ios App加载本地HTML网页,点击网页链接跳转到app页面的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-01-01
  • 解析Objective-C 中 `+load` 方法的执行顺序

    解析Objective-C 中 `+load` 方法的执行顺序

    在 Objective-C 中,+load 方法是在类或分类被加载到内存时调用的,它在程序启动过程中非常早的阶段执行,用于在类或分类被加载时进行一些初始化工作,这篇文章主要介绍了 Objective-C 中 `+load` 方法的执行顺序,需要的朋友可以参考下
    2024-07-07
  • iOS实现百度地图拖拽后更新位置以及反编码

    iOS实现百度地图拖拽后更新位置以及反编码

    百度地图已经开放了地图API,大家可以很方便的调用地图中的相应数据,并完成各项个性化的展示,下面这篇文章主要给大家介绍了关于iOS如何实现百度地图拖拽后更新位置以及反编码的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-12-12
  • IOS 基础之设置 tableview 的分割线

    IOS 基础之设置 tableview 的分割线

    这篇文章主要介绍了IOS 基础之设置 tableview 的分割线的相关资料,需要的朋友可以参考下
    2017-03-03
  • iOS底层实例解析Swift闭包及OC闭包

    iOS底层实例解析Swift闭包及OC闭包

    这篇文章主要为大家介绍了iOS底层实例解析Swift闭包及OC闭包,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-11-11
  • iOS在页面销毁时如何优雅的cancel网络请求详解

    iOS在页面销毁时如何优雅的cancel网络请求详解

    这篇文章主要给大家介绍了关于iOS在页面销毁时如何优雅的cancel网络请求的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-05-05
  • IOS入门笔记之地理位置定位系统

    IOS入门笔记之地理位置定位系统

    关于地理位置及定位系统,在iOS开发中也比较常见,接下来通过本文给大家介绍IOS入门笔记之地理位置定位系统,对ios地理位置定位系统感兴趣的朋友一起学习吧
    2016-01-01
  • 谈谈制作iOS Ad-Hoc测试应用

    谈谈制作iOS Ad-Hoc测试应用

    这篇文章主要介绍了谈谈制作iOS Ad-Hoc测试应用,AD-HOC测试是指随机测试,这种测试的特点是无特定的测试用例,有兴趣的可以了解一下。
    2016-12-12

最新评论