单链表练习题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了单链表练习题相关的知识,希望对你有一定的参考价值。
1.单链表归并
/** * 有序的两个单链表归并 * @param object $listA 待归并的单链表A * @param object $listB 待归并的单链表B * @return object $merge 归并之后的单链表 */ function mergeLinkList($listA, $listB) { $p_a = $listA->head->next; // A的工作指针 $p_b = $listB->head->next; // B的工作指针 $merge = new LinkList(); // 存放结果的链表 $m = $merge->head; while ($p_a && $p_b) { if ($p_a->data < $p_b->data) { $m->next = $p_a; // 指针传递 $p_a = $p_a->next; $m = $m->next; } else { $m->next = $p_b; $p_b = $p_b->next; $m = $m->next; } } while ($p_a) { $m->next = $p_a; // 指针传递 $p_a = $p_a->next; $m = $m->next; } while ($p_b) { $m->next = $p_b; // 指针传递 $p_b = $p_b->next; $m = $m->next; } return $merge; }
2.多项式加减运算
/* * @param object $polynomeA 用单链表存储的多项式A 数据格式是[‘index‘=>指数, ‘ratio‘=>系数] * @param object $polynomeB 用单链表存储的多项式B 数据格式是[‘index‘=>指数, ‘ratio‘=>系数] * @return object $polynomeC 计算之后的单链表 */ function mergePolynome($polynomeA, $polynomeB) { if(!is_object($polynomeA) || !is_object($polynomeB)) return false; $polynomeC = new LinkList(); $p_a = $polynomeA->head->next; $p_b = $polynomeB->head->next; $p_c = $polynomeC->head; // 归并 while ($p_a && $p_b) { if ($p_a->data[‘index‘] < $p_b->data[‘index‘]) { $p_c->next = $p_a; $p_a = $p_a->next; $p_c = $p_c->next; } else if($p_a->data[‘index‘] == $p_b->data[‘index‘]) { if($p_a->data[‘ratio‘] + $p_b->data[‘ratio‘] != 0) { $new_ration = $p_a->data[‘ratio‘] + $p_b->data[‘ratio‘]; $new_data = []; $new_data[‘index‘] = $p_a->data[‘index‘]; $new_data[‘ratio‘] = $new_ration; $new = new node($new_data); $p_c->next = $new; $p_c = $new; // 是当前的,不是下一个的 } $p_a = $p_a->next; $p_b = $p_b->next; } else { $p_c->next = $p_b; $p_b = $p_b->next; $p_c = $p_c->next; } } while ($p_a) { $p_c->next = $p_a; $p_a = $p_a->next; } while ($p_b) { $p_c->next = $p_b; $p_b = $p_b->next; } return $polynomeC; }
以上是关于单链表练习题的主要内容,如果未能解决你的问题,请参考以下文章