博客
关于我
Objective-C实现DoublyLinkedList双链表算法(附完整源码)
阅读量:797 次
发布时间:2023-02-18

本文共 2611 字,大约阅读时间需要 8 分钟。

Objective-C实现DoublyLinkedList双链表算法

以下是Objective-C中实现DoublyLinkedList(双链表)的代码示例,涵盖了基本操作如插入、删除和遍历等功能。

代码结构如下:

#import <Foundation/Foundation.h>

@interface Node : NSObject@property (nonatomic, strong) id data;@end

@implementation Node// 节点数据存储属性// 双链表节点类,用于存储数据和指向前后节点的指针@end

双链表是一种数据结构,具有双向链接的特点,支持从头尾两端进行操作。以下是双链表的实现步骤:

  • 节点类定义:定义一个节点类,包含数据存储属性和指向前后节点的指针。

  • 双链表类定义:创建一个管理双链表的类,包含节点操作方法。

  • 插入节点:实现在双链表中插入新节点的功能,支持头尾插入和任意位置插入。

  • 删除节点:实现删除节点的功能,支持按节点对象删除或按位置删除。

  • 遍历双链表:实现遍历双链表的方法,可以是按顺序遍历或倒序遍历。

  • 以下是完整的代码示例:

    #import <Foundation/Foundation.h>

    @interface DoublyLinkedList : NSObject{Node *head;Node *tail;}

    // 节点类@interface Node : NSObject{id data;Node *next;Node *previous;}@property (nonatomic, strong) id data;@property (nonatomic, strong) Node *next;@property (nonatomic, strong) Node *previous;@end

    // 双链表类实现@implementation DoublyLinkedList

    // 初始化双链表

    • (id)initWithHead:(id)head {self = [super init];self.head = (Node *)head;self.tail = (Node *)head;return self;}

    // 插入节点到双链表的头部

    • (DoublyLinkedList *)insertAtHead:(Node *)node {node.previous = nil;node.next = self.head;self.head = node;return self;}

    // 插入节点到双链表的尾部

    • (DoublyLinkedList *)insertAtTail:(Node *)node {node.next = nil;node.previous = self.tail;self.tail = node;return self;}

    // 插入节点到指定位置

    • (DoublyLinkedList *)insertAtPosition:(int)position:(Node *)node {// 检查位置是否合法if (position < 0 || position > self.count) {return self;}

      Node *currentNode = self.head;for (int i = 0; i < position; i++) {currentNode = currentNode.next;}

      currentNode.next = node;node.previous = currentNode;node.next = currentNode.next;currentNode.next = node;

      return self;}

    // 删除节点

    • (DoublyLinkedList *)deleteNode:(Node *)node {if (node.previous) {node.previous.next = node.next;}if (node.next) {node.next.previous = node.previous;}

      if (node == self.head) {self.head = node.next;}if (node == self.tail) {self.tail = node.previous;}

      return self;}

    // 删除指定位置的节点

    • (DoublyLinkedList *)deleteAtPosition:(int)position {Node *currentNode = self.head;for (int i = 0; i < position; i++) {currentNode = currentNode.next;}

      Node *nodeToDel = currentNode.next;currentNode.next = nodeToDel.next;nodeToDel.next = nil;

      if (nodeToDel == self.tail) {self.tail = currentNode;}

      return self;}

    // 遍历双链表

    • (void)traverse {Node *currentNode = self.head;while (currentNode != nil) {NSLog(@"当前节点数据:%@", currentNode.data);currentNode = currentNode.next;}}

    // 计算双链表的节点数量

    • (int)count {int count = 0;Node *currentNode = self.head;while (currentNode != nil) {count++;currentNode = currentNode.next;}return count;}

    @end

    以上代码实现了一个基本的DoublyLinkedList双链表,支持插入、删除和遍历等基本操作。开发者可以根据实际需求扩展功能,例如增加双链表的反转、清空等操作。

    注:上述代码仅为示例,具体实现中可能需要根据实际需求进行调整和优化。

    转载地址:http://dinfk.baihongyu.com/

    你可能感兴趣的文章
    Nginx的是什么?干什么用的?
    查看>>
    Nginx访问控制_登陆权限的控制(http_auth_basic_module)
    查看>>
    nginx负载均衡的五种算法
    查看>>
    Nginx配置ssl实现https
    查看>>
    Nio ByteBuffer组件读写指针切换原理与常用方法
    查看>>
    NI笔试——大数加法
    查看>>
    NLP 基于kashgari和BERT实现中文命名实体识别(NER)
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    Node.js安装与配置指南:轻松启航您的JavaScript服务器之旅
    查看>>
    NR,NF,FNR
    查看>>
    nrf开发笔记一开发软件
    查看>>
    NSSet集合 无序的 不能重复的
    查看>>
    nullnullHuge Pages
    查看>>
    Numpy如何使用np.umprod重写range函数中i的python
    查看>>
    oauth2-shiro 添加 redis 实现版本
    查看>>
    OAuth2.0_JWT令牌-生成令牌和校验令牌_Spring Security OAuth2.0认证授权---springcloud工作笔记148
    查看>>
    OAuth2.0_JWT令牌介绍_Spring Security OAuth2.0认证授权---springcloud工作笔记147
    查看>>
    OAuth2.0_介绍_Spring Security OAuth2.0认证授权---springcloud工作笔记137
    查看>>
    OAuth2.0_完善环境配置_把资源微服务客户端信息_授权码存入到数据库_Spring Security OAuth2.0认证授权---springcloud工作笔记149
    查看>>
    OAuth2.0_授权服务配置_Spring Security OAuth2.0认证授权---springcloud工作笔记140
    查看>>