@interface Node : NSObject@property(nonatomic,assign) int no;@property(nonatomic,strong) Node *next;-(instancetype)initWithNo:(int)no;@end@implementation Node-(instancetype)initWithNo:(int)no{ if (self = [super init]) { _no = no; } return self;}@end
#import "Node.h"NS_ASSUME_NONNULL_BEGIN@interface NodeList : NSObject//是否为空链表@property(nonatomic,assign,readonly) BOOL isEmpty;//链表中节点个数@property(nonatomic,assign,readonly) int count;//peek:查看头节点数据-(int)peek;//插入头部,头插法-(void)push:(Node *)node;//插入尾部,尾插法-(void)add:(Node *)node;-(Node *)get:(int)index;-(void)insert:(Node *)node AtIndex:(int)index;-(void)clear;-(int)remove:(int)index;-(void)reverse;@end
#import "NodeList.h"@interface NodeList ()@property(nonatomic,strong) Node *head;@property(nonatomic,assign) BOOL isEmpty;@property(nonatomic,assign) int count;@end@implementation NodeList//查看头节点数据-(int)peek{ if (self.isEmpty) { return -1; } return self.head.no;}//在头部插入节点-(void)push:(Node *)node{ //判空处理// if (!self.head) {// return;// }// 方式一:先用一个临时节点记录头结点的下一个节点,然后将头结点指向新节点,新节点再指向临时节点// if (self.head.next == nil) {// self.head.next = node;// }else{// Node *tempNode = self.head.next;// self.head.next = node;// node.next = tempNode;// }// //方式二:先把新节点指向头结点的下一个节点,再让头结点指向新节点(比较常用) if (self.head == nil) { self.head = node; }else{ node.next = self.head; self.head = node; }}-(void)add:(Node *)node{ if(!self.head){ self.head= node; }else{ Node *temp = self.head; while (YES) { //如果找到了节点的最后就跳出循环 if (temp.next == nil) { break; } //如果没有找到节点最后,就让temp后移 temp = temp.next; } //当退出while循环时,temp就指向了链表的最后 //将最后这个节点的next指向新的节点 temp.next = node; } }//通过指定索引位置,获取元素-(Node *)get:(int)index{ if (index > self.count || index < 0 ) { //索引越界 return nil; }else{ int i= 0; Node *temp = self.head; if (index == i) { return temp; } while (temp.next) { temp = temp.next; if (index == ++i) { return temp; } } return nil; }}//insert:在指定索引位置,插入元素-(void)insert:(Node *)newNode AtIndex:(int)index{ //判空处理 if (!self.head) { return; } if (self.head.next == nil) { self.head.next = newNode; } else{ Node *pNode = self.head; int i = 1; while (i < index && pNode.next != nil) { pNode = pNode.next; i++; } newNode.next = pNode.next; pNode.next = newNode; }}-(void)clear{ int i = self.count; while (i) { i--; [self remove:i]; } self.head = nil; NSLog(@"清空链表完成%i",self.count);}//正序遍历链表//+(NSArray<NSNumber *>*)printFromHeadWithNode:(Node *)headNode printPrefixText:(NSString *)text {//// //判空处理// if (!headNode || !headNode.next) {// return nil;// }//// Node *pNode = headNode.next;// NSMutableArray *items = [NSMutableArray array];// while (pNode!= nil) {// [items addObject:@(pNode.no)];// pNode = pNode.next;// }// NSLog(@"%@:%@",text,[items componentsJoinedByString:@"->"]);// return items;//}//单链表:删除第k个位置的节点-(int)remove:(int)index{ //判空处理 if (!self.head) { NSLog(@"%@",[NSString stringWithFormat:@"没有要删除的第%d个节点!",index]); return -1; } Node *pNode = self.head; Node *p = pNode;//移动指针 int i = 0; while (pNode.next != nil && i < index) { p = pNode; pNode = pNode.next; i++; } if (pNode != nil) { p.next = p.next.next; NSLog(@"删除成功:%i",pNode.no); return pNode.no; } NSLog(@"%@",[NSString stringWithFormat:@"没有要删除的第%d个节点!",index]); return -1;}//pop:删除头结点(和push对应,尾插法无视),返回被删除的数据-(void)pop{}-(void)reverse{ Node *current = self.head; Node *prew = nil;//current之前节点(先驱节点) Node *temp = nil; while (current) { // 记录后移下一个节点 temp = current.next; // 当前节点的next 指向上一个节点 current.next = prew; // 之前的节点后移指向当前节点 prew = current; // 当前节点后移 current = temp; self.head = prew; NSLog(@"prew%i+next%i",prew.no,prew.next.no); }}-(int)count{ if(!self.head){ return 0; }else{ int i=1; Node *temp=self.head; while(temp.next){ temp=temp.next; i++; } return i; }}-(BOOL)isEmpty{ return self.count == 0;}@end