zoukankan      html  css  js  c++  java
  • 用OC基于链表实现链队列

    一、简言

    前面已经用C++介绍过链队列的基本算法,可以去回顾一下https://www.cnblogs.com/XYQ-208910/p/11692065.html。少说多做,还是上手撸代码实践一下才能更好的加深理解,本文采用OC面向对象的思想来实现一个链队列。

    二、代码

    Node

    #import <Foundation/Foundation.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface Node : NSObject
    @property (nonatomic, assign) int data;
    @property (nonatomic, strong, nullable) Node *next;
    -(instancetype)initWithData:(int)data;
    @end
    
    NS_ASSUME_NONNULL_END
    #import "Node.h"
    
    @implementation Node
    
    -(instancetype)initWithData:(int)data {
        self = [super init];
        if (self) {
            self.data = data;
            self.next = nil;
        }
        return self;
    }
    
    @end

    LinkQueue

    //
    //  LinkQueue.h
    //  运行时
    //
    //  Created by 夏远全 on 2019/10/19.
    //
    
    #import <Foundation/Foundation.h>
    #import "Node.h"
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface LinkQueue : NSObject
    
    
    /**
     构造一个链队列
     @return 队列
     */
    +(instancetype)constrcutLinkQueue;
    
    
    /**
     入队列
     @param node 节点元素
     */
    -(void)enQueueWithNode:(Node *)node;
    
    
    /**
     出队列
     @return 节点元素
     */
    -(Node *)deQueue;
    
    
    /**
     队列是否为空
     @return 布尔值
     */
    -(BOOL)isEmpty;
    
    
    /**
     获取元素个数
     @return 个数
     */
    -(int)eleCount;
    
    
    @end
    
    NS_ASSUME_NONNULL_END
    //
    //  LinkQueue.m
    //  运行时
    //
    //  Created by 夏远全 on 2019/10/19.
    //
    
    #import "LinkQueue.h"
    
    @interface LinkQueue()
    @property (nonatomic, strong) Node *front;  //队列头指针
    @property (nonatomic, strong) Node *rear;   //队列尾指针
    @end
    
    @implementation LinkQueue
    
    /**
     构造一个链队列
     @return 队列
     */
    +(instancetype)constrcutLinkQueue {
        
        LinkQueue *linkQueue = [[LinkQueue alloc] init];
        Node *headNode = [[Node alloc] init];        //便于操作,创建一个头结点
        linkQueue.front = linkQueue.rear = headNode; //均指向头结点
        
        return linkQueue;
    }
    
    
    /**
     入队列
     @param node 节点元素
     */
    -(void)enQueueWithNode:(Node *)node {
        
        /// 尾节点的next指针指向新节点
        self.rear.next = node;
        
        /// 更改尾指针指向新节点
        self.rear = node;
        
        NSLog(@"入队列的元素 = %d",node.data);
    }
    
    
    /**
     出队列
     @return 节点元素
     */
    -(Node *)deQueue {
        
        if ([self isEmpty]) {
            return nil;
        }
        
        ///取出头结点的指向的首节点
        Node *node = self.front.next;
        
        ///更改头结点指针指向首节点的下一个节点
        self.front.next = node.next;
        
        ///判断取出的节点是否为尾指针指向的节点,如果是,队列元素则全部取完,此时将首尾指针均重新指向头结点
        if (self.rear == node) {
            self.rear = self.front;
        }
        
        NSLog(@"出队列的元素 = %d",node.data);
        
        return node;
    }
    
    /**
     队列是否为空
     @return 布尔值
     */
    -(BOOL)isEmpty {
        if (self.front == self.rear) {
            NSLog(@"当前队列已空");
            return YES;
        }
        return NO;
    }
    
    /**
     获取元素个数
     @return 个数
     */
    -(int)eleCount {
        if (self.front == self.rear) {
            NSLog(@"当前队列元素个数 = 0");
            return 0;
        }
        Node *p = self.front.next;
        int eleCount = 0;
        while (p) {
            eleCount ++;
            p = p.next;
        }
        NSLog(@"当前队列元素个数 = %d",eleCount);
        return eleCount;
    }
    
    @end

    三、结果

    测试:

    -(void)test_DataStructure_LinkQueue {
        
        ///构造链式队列
        LinkQueue *linkQueue = [LinkQueue constrcutLinkQueue];
        
        /// 构造元素
        Node *node1 = [[Node alloc] initWithData:1];
        Node *node2 = [[Node alloc] initWithData:2];
        Node *node3 = [[Node alloc] initWithData:3];
        Node *node4 = [[Node alloc] initWithData:4];
        Node *node5 = [[Node alloc] initWithData:5];
        
        /// enter Queue 入队列
        [linkQueue enQueueWithNode:node1];
        [linkQueue enQueueWithNode:node2];
        [linkQueue enQueueWithNode:node3];
        [linkQueue enQueueWithNode:node4];
        [linkQueue enQueueWithNode:node5];
        
        ///全部入队后 get eleCount 元素个数
        [linkQueue eleCount];
        
        /// deque Queue 出队列
        [linkQueue deQueue];
        [linkQueue deQueue];
        [linkQueue deQueue];
        [linkQueue deQueue];
        [linkQueue deQueue];
        [linkQueue deQueue];
        
        ///全部出队后  get eleCount 元素个数
        [linkQueue eleCount];
    }

     打印:

    2019-10-19 14:27:36.278796+0800 运行时[74840:2307484] 入队列的元素 = 1
    2019-10-19 14:27:36.278991+0800 运行时[74840:2307484] 入队列的元素 = 2
    2019-10-19 14:27:36.279105+0800 运行时[74840:2307484] 入队列的元素 = 3
    2019-10-19 14:27:36.279201+0800 运行时[74840:2307484] 入队列的元素 = 4
    2019-10-19 14:27:36.279289+0800 运行时[74840:2307484] 入队列的元素 = 5
    2019-10-19 14:27:36.279405+0800 运行时[74840:2307484] 当前队列元素个数 = 5
    2019-10-19 14:27:36.279493+0800 运行时[74840:2307484] 出队列的元素 = 1
    2019-10-19 14:27:36.279584+0800 运行时[74840:2307484] 出队列的元素 = 2
    2019-10-19 14:27:36.279665+0800 运行时[74840:2307484] 出队列的元素 = 3
    2019-10-19 14:27:36.279869+0800 运行时[74840:2307484] 出队列的元素 = 4
    2019-10-19 14:27:36.280227+0800 运行时[74840:2307484] 出队列的元素 = 5
    2019-10-19 14:27:36.280548+0800 运行时[74840:2307484] 当前队列已空
    2019-10-19 14:27:36.280845+0800 运行时[74840:2307484] 当前队列元素个数 = 0
  • 相关阅读:
    2017-2018-1 20155332 《信息安全系统设计基础》课程总结
    2017-2018-1 20155332 《信息安全系统设计基础》第14周学习总结
    2017-2018-1 20155332 实验五 通信协议设计
    2017-2018-1 20155332 《信息安全系统设计基础》第13周学习总结
    20155332 补交ch12课下作业
    20155332 补交课后测试——ch11网络编程
    # 2017-2018-1 20155332 实验四 外设驱动程序设计
    2017-2018-1 20155332《信息安全系统设计基础》第十一周学习总结
    2017-2018-1 20155332 20155213 实验四 木马及远程控制技术
    20155332 linux进程间通信(IPC)机制(未完成,待续)
  • 原文地址:https://www.cnblogs.com/XYQ-208910/p/11703743.html
Copyright © 2011-2022 走看看