单向链表定义
单向链表是由一个个结点组成的
每个结点包括数值和指向下一个结点的指针
头结点数值为null 尾结点指向为null
链表和数组的区别
1.数组静态分配内存,链表动态分配内存。
2.数组在内存中是连续的,链表是不连续的。
3.数组利用下标定位,查找的时间复杂度是O(1),链表通过遍历定位元素。查找的时间复杂度是O(N)。
4.数组插入和删除需要移动其他元素,时间复杂度是O(N),链表的插入或删不需要移动其他元素,时间复杂度是O(1)。
单向链表具体实现
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface Node : NSObject
- (instancetype)initWithData:(NSString*)dataString nextNode:(nullable Node*)nextNode;
@property (nonatomic ,strong) NSString *dataString;
@property (nonatomic ,strong ,nullable) Node *nextNode;
@end
NS_ASSUME_NONNULL_END
#import "Node.h"
@implementation Node
- (instancetype)initWithData:(NSString*)dataString nextNode:(nullable Node*)nextNode
{
self = [super init];
if (self) {
self.dataString = dataString;
self.nextNode = nextNode;
}
return self;
}
@end
#import <Foundation/Foundation.h>
#import "Node.h"
NS_ASSUME_NONNULL_BEGIN
@interface LinkList : NSObject
@property (nonatomic ,assign) NSInteger num;
@property (nonatomic ,strong) Node *firstNode;
- (void)addData:(NSString*)dataString atIndex:(NSInteger)index;
- (void)addData:(NSString*)dataString;
- (void)removeLastObject;
- (void)removeDataAtIndex:(NSInteger)index;
- (NSInteger)indexForDataString:(NSString*)dataString;
- (NSString*)getFirstObject;
- (NSString*)getLastObject;
- (void)printAllData;
- (BOOL)isEmpty;
@end
NS_ASSUME_NONNULL_END
#import "LinkList.h"
@implementation LinkList
-(instancetype)init
{
self = [super init];
if (self) {
self.firstNode = [[Node alloc]init];
self.num = 0;
}
return self;
}
- (void)addData:(NSString*)dataString atIndex:(NSInteger)index
{
if (index > self.num) {
NSLog(@"不合法");
return;
}
Node *previousNode = self.firstNode;
for (NSInteger i = 0; i <= index - 1; i ++) {
previousNode = previousNode.nextNode;
}
Node *curNode = previousNode.nextNode;
Node *newNode = [[Node alloc]initWithData:dataString nextNode:curNode];
previousNode.nextNode = newNode;
self.num++;
}
- (void)addData:(NSString*)dataString
{
Node *node = self.firstNode;
for (NSInteger i = 0; i < self.num; i ++) {
node = node.nextNode;
if (node.nextNode == NULL) {
Node *newNode = [[Node alloc]initWithData:dataString nextNode:NULL];
node.nextNode = newNode;
}
}
self.num++;
}
- (void)removeLastObject
{
Node *previousNode = self.firstNode;
for (NSInteger i = 0; i <= self.num - 1; i ++) {
previousNode = previousNode.nextNode;
}
previousNode.nextNode = NULL;
self.num--;
}
- (void)removeDataAtIndex:(NSInteger)index
{
if (index > self.num-1) {
NSLog(@"不合法");
return;
}
Node *previousNode = self.firstNode;
for (NSInteger i = 0; i <= index - 1; i ++) {
previousNode = previousNode.nextNode;
}
Node *curNode = previousNode.nextNode;
Node *nextNode = curNode.nextNode;
previousNode.nextNode = nextNode;
self.num--;
}
- (NSInteger)indexForDataString:(NSString*)dataString
{
Node *previousNode = self.firstNode;
for (NSInteger i = 0; i <= self.num - 1; i ++) {
previousNode = previousNode.nextNode;
if ([previousNode.dataString isEqualToString:dataString]) {
return i;
}
}
return -1;
}
- (NSString*)getFirstObject
{
Node *node = self.firstNode;
return node.nextNode.dataString;
}
- (NSString*)getLastObject
{
Node *node = self.firstNode;
for (NSInteger i = 0; i < self.num; i ++) {
node = node.nextNode;
if (node.nextNode == NULL) {
return node.dataString;
}
}
return nil;
}
- (void)printAllData
{
NSMutableArray *array = [NSMutableArray array];
Node *node = self.firstNode;
for (NSInteger i = 0; i < self.num; i ++) {
node = node.nextNode;
[array addObject:node.dataString];
}
NSLog(@"链表最终数据为:%@",array);
}
- (BOOL)isEmpty
{
return self.num == 0 ? YES : NO;
}
@end
#import "ViewController.h"
#import "LinkList.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
LinkList *list = [[LinkList alloc]init];
[list addData:@"a" atIndex:0];
[list addData:@"b" atIndex:1];
[list addData:@"c" atIndex:2];
[list printAllData];
[list addData:@"d" atIndex:1];
[list printAllData];
NSLog(@"长度为:%ld",(long)list.num);
[list addData:@"e"];
[list addData:@"f"];
[list addData:@"h" atIndex:0];
[list printAllData];
NSString *first = list.getFirstObject;
NSLog(@"%@",first);
NSString *last = list.getLastObject;
NSLog(@"%@",last);
[list removeDataAtIndex:2];
[list printAllData];
[list removeLastObject];
[list printAllData];
NSInteger row = [list indexForDataString:@"b"];
NSLog(@"%ld",(long)row);
}
@end