用OC基於鏈表實現鏈隊列

1、簡言html

前面已經用C++介紹過鏈隊列的基本算法,能夠去回顧一下https://www.cnblogs.com/XYQ-208910/p/11692065.html。少說多作,仍是上手擼代碼實踐一下才能更好的加深理解,本文采用OC面向對象的思想來實現一個鏈隊列。node

 

2、代碼算法

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

LinkQueueatom

//
//  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

 

3、結果spa

測試:指針

-(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];
}

 打印:code

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
相關文章
相關標籤/搜索