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

 

全部评论

相关推荐

来个大佬救一下,为上投了都是石沉大海了,没实习经历的话怕秋招直接进不了面。什么实习这么难找,基本
心态爆炸了:现在正式的岗位都少,实习基本不咋招的,除了大厂,中小企业其实没那么多岗位需求,就算是有,大多都是招一两个廉价劳动力,同时,他们也会希望你一来就能干活的,没时间培训你,就让你了解公司的项目,你了解完就可以开始干活。再者是,很多低质量的实习其实用处没有那么大的。我去年也是找实习找到破防,最后去了一家深圳的小公司实习,工作对我来说很简单,甚至不如我在学校做的项目,秋招的时候,这段实习经历也并没有帮上什么忙,投递简历,依旧非常低的回复率。低回复率是常态,尤其是找实习,找不到,那就把重心放在优化自己的简历和项目,多看八股文,锻炼自己的面试能力,多看别人的面经,自己模拟面试,等秋招的时候,只要有那么寥寥几次,好好抓住那几次机会。
点赞 评论 收藏
分享
兄弟们,实习都是在接各种api,该怎么包装简历
仁者伍敌:感觉我自己做小项目也是各种api啊,我要怎么包装简历
点赞 评论 收藏
分享
Yki_:你要算时间成本呀,研究生两三年,博士三四年,加起来就五六年了,如果你本科去腾讯干五年,多领五年的年薪,加上公司内涨薪,可能到时候十五年总薪资也跟博士差不多
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务