#region 将节点插入到指定位置 /// <summary> /// 将节点插入到指定位置 /// </summary> /// <typeparam></typeparam> /// <param></param> /// <param></param> /// <param></param> /// <returns></returns> public Node<T> ChainListInsert<T, W>(Node<T> head, string key, Func<T, W> where, T data) where W : IComparable { if (head == null) return null;
if (where(head.data).CompareTo(key) == 0) { Node<T> node = new Node<T>();
node.data = data;
node.next = head.next;
head.next = node; }
ChainListInsert(head.next, key, where, data);
return head; } #endregion
<4> 删除节点:
这个也比较简单,不解释,图跟代码更具有说服力,口头表达反而让人一头雾水。 当然时间复杂度就为O(N),N是来自于查找到要删除的节点。
效果图:
代码段:
复制代码 代码如下: #region 将指定关键字的节点删除 /// <summary> /// 将指定关键字的节点删除 /// </summary> /// <typeparam></typeparam> /// <typeparam></typeparam> /// <param></param> /// <param></param> /// <param></param> /// <param></param> /// <returns></returns> public Node<T> ChainListDelete<T, W>(Node<T> head, string key, Func<T, W> where) where W : IComparable { if (head == null) return null;
//这是针对只有一个节点的解决方案 if (where(head.data).CompareTo(key) == 0) { if (head.next != null) head = head.next; else return head = null; } else { //判断一下此节点是否是要删除的节点的前一节点 while (head.next != null && where(head.next.data).CompareTo(key) == 0) { //将删除节点的next域指向前一节点 head.next = head.next.next; } }
ChainListDelete(head.next, key, where);
return head; } #endregion
<5> 按关键字查找节点:
这个思想已经包含到“插入节点”和“删除节点”的具体运用中的,其时间复杂度为O(N)。
代码段:
复制代码 代码如下: (编辑:焦作站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|