1. @interface DoubleNode : NSObject
    2. @property(nonatomic,assign) int no;
    3. @property(nonatomic,weak) DoubleNode *pre;
    4. @property(nonatomic,weak) DoubleNode *next;
    5. -(instancetype)initWithNo:(int)no;
    6. @end
    1. #import "DoubleNode.h"
    2. @implementation DoubleNode
    3. -(instancetype)initWithNo:(int)no{
    4. if (self = [super init]) {
    5. _no = no;
    6. }
    7. return self;
    8. }
    9. -(void)dealloc{
    10. NSLog(@"DoubleNode销毁%i",self.no);
    11. }
    12. @end
    1. #import <Foundation/Foundation.h>
    2. #import "DoubleNode.h"
    3. @interface DoubleNodeList : NSObject
    4. //是否为空链表
    5. @property(nonatomic,assign,readonly) BOOL isEmpty;
    6. //链表中节点个数
    7. @property(nonatomic,assign,readonly) int count;
    8. //peek:查看头节点数据
    9. -(int)peek;
    10. //插入头部,头插法
    11. -(void)push:(DoubleNode *)node;
    12. -(int)pop;
    13. -(DoubleNode *)get:(int)index;
    14. -(void)insert:(DoubleNode *)node AtIndex:(int)index;
    15. -(void)clear;
    16. -(void)reverse;
    17. @end
    1. #import "DoubleNodeList.h"
    2. @interface DoubleNodeList ()
    3. @property(nonatomic,strong) DoubleNode *head;
    4. @property(nonatomic,strong) DoubleNode *last;
    5. @property(nonatomic,assign) BOOL isEmpty;
    6. @property(nonatomic,assign) int count;
    7. @end
    8. @implementation DoubleNodeList
    9. //查看头节点数据
    10. -(int)peek{
    11. if (self.isEmpty) {
    12. return -1;
    13. }
    14. return self.head.no;
    15. }
    16. //在头部插入节点
    17. -(void)push:(DoubleNode *)node{
    18. if (self.isEmpty) {
    19. self.last = node;
    20. }else{
    21. if (!self.last.pre) {
    22. self.last.pre = node;
    23. }
    24. self.head.pre = node;
    25. }
    26. node.next = self.head;
    27. self.head = node;
    28. _count +=1;
    29. }
    30. //pop:删除头结点(和push对应,尾插法无视),返回被删除的数据
    31. -(int)pop{
    32. if (self.isEmpty) {
    33. return -1;
    34. }
    35. DoubleNode *tempnode = self.head;
    36. if (self.head == self.last) {
    37. self.head = nil;
    38. }
    39. if (self.count == 2) {
    40. self.last.pre = nil;
    41. }
    42. self.head = self.head.next;
    43. self.head.pre = nil;
    44. _count -= 1;
    45. return tempnode.no;
    46. }
    47. //通过指定索引位置,获取元素
    48. -(DoubleNode *)get:(int)index{
    49. if (index > self.count || index < 0 ) {
    50. //索引越界
    51. return nil;
    52. }else{
    53. int i= 0;
    54. DoubleNode *temp = self.head;
    55. if (index == i) {
    56. return temp;
    57. }
    58. while (temp.next) {
    59. temp = temp.next;
    60. if (index == ++i) {
    61. return temp;
    62. }
    63. }
    64. return nil;
    65. }
    66. }
    67. //insert:在指定索引位置,插入元素
    68. -(void)insert:(DoubleNode *)newNode AtIndex:(int)index{
    69. //判空处理
    70. if (self.isEmpty) {
    71. [self push:newNode];
    72. return;
    73. }
    74. if (index > self.count) {
    75. index = self.count;
    76. }
    77. DoubleNode *preNode = [self get:index-1];
    78. DoubleNode *nextNode = preNode.next;
    79. //索引是最后一个
    80. if (nextNode == nil) {
    81. self.last = newNode;
    82. self.last.pre = preNode;
    83. }
    84. newNode.next = nextNode;
    85. nextNode.pre = newNode;
    86. preNode.next = newNode;
    87. newNode.pre = preNode;
    88. // newNode.next = nextNode;
    89. // newNode.pre = preNode;
    90. //
    91. // preNode.next = newNode;
    92. // nextNode.pre = newNode;
    93. _count +=1;
    94. }
    95. -(void)clear{
    96. self.head = nil;
    97. self.last = nil;
    98. self.count = 0;
    99. NSLog(@"清空链表完成%i",self.count);
    100. }
    101. //-(void)add:(DoubleNode *)node{
    102. //
    103. // if(!self.head){
    104. // self.head= node;
    105. // }else{
    106. //
    107. // DoubleNode *temp = self.head;
    108. // while (YES) {
    109. //
    110. // //如果找到了节点的最后就跳出循环
    111. // if (temp.next == nil) {
    112. // break;
    113. // }
    114. // //如果没有找到节点最后,就让temp后移
    115. // temp = temp.next;
    116. // }
    117. //
    118. // //当退出while循环时,temp就指向了链表的最后
    119. // //将最后这个节点的next指向新的节点
    120. // temp.next = node;
    121. // }
    122. //
    123. // }
    124. //单链表:删除第k个位置的节点
    125. -(int)remove:(int)index{
    126. //判空处理
    127. if (self.isEmpty) {
    128. NSLog(@"%@",[NSString stringWithFormat:@"没有要删除的第%d个节点!",index]);
    129. return -1;
    130. }
    131. if (index == 0) {
    132. return [self pop];
    133. }
    134. DoubleNode *preNode = [self get:index-1];
    135. DoubleNode *current = preNode.next;
    136. current.pre = nil;
    137. DoubleNode *nextNode = current.next;
    138. if (nextNode == nil) {
    139. self.last = preNode;
    140. self.last.pre = preNode.pre;
    141. // self.last.next = nil;
    142. }else{
    143. nextNode.pre = preNode;
    144. }
    145. preNode.next = nextNode;
    146. _count -=1;
    147. return current.no;
    148. }
    149. -(void)reverse{
    150. if (self.count <= 1) {
    151. return;
    152. }
    153. DoubleNode *current = self.head.next;
    154. DoubleNode *prew = self.head;
    155. self.head.next = nil;
    156. while (current) {
    157. DoubleNode *temp = current.next;
    158. current.next = prew;
    159. prew.pre = current;
    160. prew = current;
    161. current = temp;
    162. NSLog(@"prew%i+next%i",prew.no,prew.next.no);
    163. }
    164. prew.pre = nil;
    165. }
    166. -(int)count{
    167. return _count;
    168. }
    169. -(BOOL)isEmpty{
    170. return self.count == 0;
    171. }
    172. @end