• OC_链表实现队列


    1. @interface Node : NSObject
    2. @property(nonatomic,strong)NSString *value;
    3. @property(nonatomic,strong)Node *next;

    Queue的头文件内容:

    1. #import <Foundation/Foundation.h>
    2. @class Node;
    3. @interface Queue : NSObject
    4. @property  (strong,nonatomic) Node  *first;
    5. @property  (strong,nonatomic)  Node  *last;
    6. @property  (assign,nonatomic) NSInteger  count;
    7. -(BOOL)isEmpty;
    8. -(NSInteger)size;
    9. -(void)enqueue:(NSString *)value;
    10. -(NSString *)dequeue;
    11. -(void)remove:(NSString *)value;
    12. @end
    13. Queue的实现内容:
    14. #import "Queue.h"
    15. #import "Node.h"
    16. @implementation Queue
    17. -(BOOL)isEmpty{
    18.     return self.count==0;
    19. }
    20. -(NSInteger)size{
    21.     return self.count;
    22. }
    23. -(void)enqueue:(NSString *)value{
    24.     Node  *oldLast=self.last;
    25.     self.last=[[Node alloc]init];
    26.     self.last.data=value;
    27.     self.last.next=NULL;
    28.     oldLast.next=self.last;
    29.     if ([self isEmpty]) {
    30.         self.first=self.last;
    31.     }else{
    32.         oldLast.next=self.last;
    33.     }
    34.     self.count=self.count+1;
    35. }
    36. -(NSString *)dequeue{
    37.     if ([self isEmpty]) {
    38.         return [NSString stringWithFormat:@"-1"];
    39.     }
    40.     NSString  *result=self.first.data;
    41.     self.first=self.first.next;
    42.     self.count=self.count-1;
    43.     return result;
    44. }
    45. -(void)remove:(NSString *)value{
    46.     //判断是不是头部节点
    47.     if ([self.first.data isEqualToString:value]) {
    48.         self.first=self.first.next;
    49.         self.count=self.count-1;
    50.     }else
    51.     {
    52.         Node  *node=self.first;
    53.         while (node!=NULL)
    54.         {
    55.             if ([node.next.data isEqualToString:value])
    56.             {
    57.                 node.next=node.next.next;
    58.                 self.count=self.count-1;
    59.                 break;
    60.             }
    61.             node=node.next;
    62.         }
    63.     }
    64. }
    65. @end
  • 相关阅读:
    团队冲刺六
    团队冲刺五
    【Mybaits学习】03_ CRUD基于注解
    【Mybaits学习】02_ 快速入门
    【Mybaits学习】01_ 初识
    深圳国际马拉松
    深圳南山半程马拉松
    Selenide使用笔记
    UI自动化测试01-环境搭建
    Java C3p0在Spring中运用
  • 原文地址:https://www.cnblogs.com/xjf125/p/4751929.html
Copyright © 2020-2023  润新知