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