@interface DoubleNode : NSObject
@property(nonatomic,assign) int no;
@property(nonatomic,weak) DoubleNode *pre;
@property(nonatomic,weak) DoubleNode *next;
-(instancetype)initWithNo:(int)no;
@end
#import "DoubleNode.h"
@implementation DoubleNode
-(instancetype)initWithNo:(int)no{
if (self = [super init]) {
_no = no;
}
return self;
}
-(void)dealloc{
NSLog(@"DoubleNode销毁%i",self.no);
}
@end
#import <Foundation/Foundation.h>
#import "DoubleNode.h"
@interface DoubleNodeList : NSObject
//是否为空链表
@property(nonatomic,assign,readonly) BOOL isEmpty;
//链表中节点个数
@property(nonatomic,assign,readonly) int count;
//peek:查看头节点数据
-(int)peek;
//插入头部,头插法
-(void)push:(DoubleNode *)node;
-(int)pop;
-(DoubleNode *)get:(int)index;
-(void)insert:(DoubleNode *)node AtIndex:(int)index;
-(void)clear;
-(void)reverse;
@end
#import "DoubleNodeList.h"
@interface DoubleNodeList ()
@property(nonatomic,strong) DoubleNode *head;
@property(nonatomic,strong) DoubleNode *last;
@property(nonatomic,assign) BOOL isEmpty;
@property(nonatomic,assign) int count;
@end
@implementation DoubleNodeList
//查看头节点数据
-(int)peek{
if (self.isEmpty) {
return -1;
}
return self.head.no;
}
//在头部插入节点
-(void)push:(DoubleNode *)node{
if (self.isEmpty) {
self.last = node;
}else{
if (!self.last.pre) {
self.last.pre = node;
}
self.head.pre = node;
}
node.next = self.head;
self.head = node;
_count +=1;
}
//pop:删除头结点(和push对应,尾插法无视),返回被删除的数据
-(int)pop{
if (self.isEmpty) {
return -1;
}
DoubleNode *tempnode = self.head;
if (self.head == self.last) {
self.head = nil;
}
if (self.count == 2) {
self.last.pre = nil;
}
self.head = self.head.next;
self.head.pre = nil;
_count -= 1;
return tempnode.no;
}
//通过指定索引位置,获取元素
-(DoubleNode *)get:(int)index{
if (index > self.count || index < 0 ) {
//索引越界
return nil;
}else{
int i= 0;
DoubleNode *temp = self.head;
if (index == i) {
return temp;
}
while (temp.next) {
temp = temp.next;
if (index == ++i) {
return temp;
}
}
return nil;
}
}
//insert:在指定索引位置,插入元素
-(void)insert:(DoubleNode *)newNode AtIndex:(int)index{
//判空处理
if (self.isEmpty) {
[self push:newNode];
return;
}
if (index > self.count) {
index = self.count;
}
DoubleNode *preNode = [self get:index-1];
DoubleNode *nextNode = preNode.next;
//索引是最后一个
if (nextNode == nil) {
self.last = newNode;
self.last.pre = preNode;
}
newNode.next = nextNode;
nextNode.pre = newNode;
preNode.next = newNode;
newNode.pre = preNode;
// newNode.next = nextNode;
// newNode.pre = preNode;
//
// preNode.next = newNode;
// nextNode.pre = newNode;
_count +=1;
}
-(void)clear{
self.head = nil;
self.last = nil;
self.count = 0;
NSLog(@"清空链表完成%i",self.count);
}
//-(void)add:(DoubleNode *)node{
//
// if(!self.head){
// self.head= node;
// }else{
//
// DoubleNode *temp = self.head;
// while (YES) {
//
// //如果找到了节点的最后就跳出循环
// if (temp.next == nil) {
// break;
// }
// //如果没有找到节点最后,就让temp后移
// temp = temp.next;
// }
//
// //当退出while循环时,temp就指向了链表的最后
// //将最后这个节点的next指向新的节点
// temp.next = node;
// }
//
// }
//单链表:删除第k个位置的节点
-(int)remove:(int)index{
//判空处理
if (self.isEmpty) {
NSLog(@"%@",[NSString stringWithFormat:@"没有要删除的第%d个节点!",index]);
return -1;
}
if (index == 0) {
return [self pop];
}
DoubleNode *preNode = [self get:index-1];
DoubleNode *current = preNode.next;
current.pre = nil;
DoubleNode *nextNode = current.next;
if (nextNode == nil) {
self.last = preNode;
self.last.pre = preNode.pre;
// self.last.next = nil;
}else{
nextNode.pre = preNode;
}
preNode.next = nextNode;
_count -=1;
return current.no;
}
-(void)reverse{
if (self.count <= 1) {
return;
}
DoubleNode *current = self.head.next;
DoubleNode *prew = self.head;
self.head.next = nil;
while (current) {
DoubleNode *temp = current.next;
current.next = prew;
prew.pre = current;
prew = current;
current = temp;
NSLog(@"prew%i+next%i",prew.no,prew.next.no);
}
prew.pre = nil;
}
-(int)count{
return _count;
}
-(BOOL)isEmpty{
return self.count == 0;
}
@end