工具小函数集合
2012-03-15 22:04
183 查看
// 从左边开始,跳过所有无效字符 const char* skip_chars_left(const char* p, const char* chars) { while ( p && *p ) { if ( !strchr(chars, *p) ) { break; } ++p; } return p; }
// 从右边开始,跳过所有无效字符 const char* skip_chars_right(const char* p, const char* chars) { const char* last = p; while ( p && *p ) { if ( !strchr(chars, *p) ) // 没有找到过滤字符,则跟随移动 { last = p; } ++p; } return (last ? ++last : p); // 往后跳一个字符 }
// 单链表节点数据模型 struct list_node_t { void* data; list_node_t* next; }; // 反转单向链表 list_node_t* reverse_list(list_node_t* head) { list_node_t* p = head; list_node_t* q = head; head = 0; // 反转后的新链表 while ( p ) { // 在新链表的头部插入节点p q = p->next; p->next = head; head = p; p = q; // p和q指向下一个节点 } return head; } // 判断单链表中间是否存在环结构 // 第一种实现:反转链表,判断是否回到头结点 bool find_circle(list_node_t* head) { return (head ? (head == reverse_list(head)) : false); } // 第二种实现:快慢指针方法,判断快慢指针是否相遇 bool find_circle(list_node_t* head) { list_node_t* fast = head; list_node_t* slow = head; while ( fast && fast->next && fast->next->next ) // 快指针到达单链表尾结束 { if ( fast == slow ) // 相遇则存在环 { return true; } fast = fast->next->next; slow = slow->next; } return false; }
相关文章推荐
- 常用的Http工具函数集合
- jQuery笔记——工具函数——对象和集合操作
- 16个jQuery工具函数
- SQL集合函数中case when then 使用技巧
- JS常用工具函数整理
- 一个用于网络的工具函数库
- 函数越小越好、Linq简化代码、集合初始化、??运算符、Using
- 一些C++中字符串处理的函数集合
- cuda中同步有关函数集合
- 实现集合类的帮助函数
- CGRect相关工具函数
- 子模集合函数(Submodular set function)
- 创业干货分享:初创公司移动应用开发工具库(集合)
- PHP中文处理函数集合
- 集合函数与分组
- Ext Js常用的工具函数(八)
- 在chrome开发者工具中观察函数调用栈、作用域链与闭包
- 泛型集合和string.Split()函数
- Photoshop基本手法集合:高级工具的应用
- 工具函数:把一个工作区中非空字段…