iOS 链表
来源:互联网 发布:淘宝虚假交易怎么清洗, 编辑:程序博客网 时间:2024/05/18 02:21
#import <Foundation/Foundation.h>@interface ListNode : NSObject/** 添加节点 @param p 节点指针 @param value 节点值 @return 头结点, 其中头结点 value 为 0 */+ (ListNode *)addNode:(ListNode *)p andVlaue:(id)value;/** 遍历单链表 @param head 头节点 */+ (void)traveraslList:(ListNode *)head resultBlock:(void(^)(NSInteger i))block;/** 单链表逆置 @param head 头节点 */+ (void)reverseList:(ListNode *)head;/** 全链表反转 */+ (ListNode *)reverse:(ListNode *)node;@end
#import "ListNode.h"@interface ListNode(){ id val; ListNode *next;}@end@implementation ListNode+ (ListNode *)addNode:(ListNode *)p andVlaue:(id)value{ if (p == nil) { p = [[ListNode alloc]init]; p->val = value; p->next = nil; }else{ p->next = [ListNode addNode:p->next andVlaue:value]; } return p;}+ (void)traveraslList:(ListNode *)head resultBlock:(void (^)(NSInteger))block{ if (head->next == nil) { if (block) { block([head->val integerValue]); } return; } if (block) { block([head->val integerValue]); } [ListNode traveraslList:head->next resultBlock:block];}+ (void)reverseList:(ListNode *)head{ ListNode *p = head; ListNode *s = p->next; while (s->next != nil) { ListNode *t = s->next; s->next = p; p = s; s = t; } s->next = p; head->next->next = nil; //尾指针置为空 head->next = s; //赋值到头指针最后一位}+ (ListNode *)reverse:(ListNode *)node{ ListNode *current,*pnext,*prev; if (!node) return nil; if (!node->next) return node; current = node; pnext = current->next; current->next = nil; while (pnext) { prev = pnext->next; pnext->next = current; pnext = prev; } node = current; return node;}@end
0 0
- iOS 链表
- iOS开发算法--链表
- iOS
- iOS
- IOS
- iOS
- iOS
- IOS
- ios
- iOS
- iOS
- IOS
- iOS
- ios
- ios ~~~~~
- ios
- IOS
- IOS
- 控件AutoCompleteTextView 自动提示
- jsp 中 out.write()不能显示数字或乱码
- Swift - JPush极光推送的使用(发送自定义消息/通知/别名/tag)
- Linux Shell 文本处理工具集锦
- 分布式计算漫谈
- iOS 链表
- WebStorm 2016 最新版激活(activation code方式)
- lcd
- iOS 通过设置颜色去除UISearchBar的灰色背景
- CSV操作(导出和导入)
- sockaddr_in , sockaddr , in_addr区别Socket编程函数集(非常有用)
- Androidstudio加载工程很慢解决方案
- iOS插入排序
- iOS编译时报错The run destination John的 iPhone is not valid for Running the scheme 'Test'.