不典型地插入双连接列表
Not typical inserting into doubly-linked list
我有一个任务我不知道如何'bite'。
编写一种方法,该方法将一对节点插入双链接列表中 使用键的键,每个节点都用给定的键
键 在函数返回插入的对数。
我应该如何解释?如何实施它?
模板< ...
int insert (double where, double before, double after);
似乎很合理。对于每个匹配where
的密钥,您必须在它之前插入before
,然后在其之后插入after
。
所以,假设您有:
||- 1 <-> 2 <-> 3 <-> 2 <-> 2 -||
然后呼叫:
quant = insert (2, 3.14, 2.72);
最终将修改列表如下:
||- 1 <-> 3.14 <-> 2 <-> 2.72 <-> 3 <-> 3.14 -+
|
+---------------------------------------------+
|
+-> 2 <> 2.72 <> 3.14 <> 2 <> 2.72 -||
并返回值3
。
除了在节点之前和之后插入的行为外,这与任何其他插入操作完全一样,事实是A doubly 链接列表实际上使事情变得更容易。这种野兽的伪代码是:
# HEAD is pointer to first element, TAIL to last.
# Process entire list, doing the insertions as needed.
def insert(where, before, after):
count = 0
curr = HEAD
while curr != null:
if curr.data == where:
count = count + 1
insertBefore(curr, before)
insertAfter(curr, after)
curr = curr.next # see note (a) below.
curr = curr.next
return count
# Insert before a node, special processing needed for head.
def insertBefore(curr, value):
node = new element
node.data = value
node.next = curr
if curr == HEAD:
HEAD = node
else:
curr.prev.next = node
def insertAfter(curr, value):
node = new element
node.data = value
node.next = curr.next
curr.next = node
if curr == TAIL:
TAIL = node
(a)您 May 想知道为什么在发生插入的情况下,我两次将指针推进两次。如果您考虑where
和after
都具有相同的值(例如7
)。
没有额外的进步,您会在列表中找到7
,然后在其后立即插入另一个7
。然后,您会发现 7
,然后在其后立即插入另一个7
。这会持续一段时间。如果您愿意,请继续尝试,完成后回来。
...
好吧,希望不会太很久就意识到没有某种干预就不会停止,例如 ctrl-c 无限记忆: - )
进步可确保您实际上不会处理在列表中添加的节点,只有在该过程启动时已经在其中的节点。
相关文章:
- Pybind11:将元组列表从Python传递到C++
- 从网链套接字请求连接设备的列表
- 如何在等效列表中查找最小的连接标签
- deleteNode函数出现以中断连接列表
- 从简单连接的列表中删除最大值
- C++ 链表合并排序的实现在连接 1 个以上节点的子列表时失败
- C++ 套接字接受,连接的客户端列表
- 似乎无法将我的标题文件与我的CPP文件连接到链接列表中
- 如何在C++中将边列表转换为邻接矩阵,其中包含连接到顶点的边数而不是一条边?
- 不典型地插入双连接列表
- 如何在C++中连接列表列表?
- C++中将不同类型的变量列表连接到逗号分隔字符串的最快方法是什么?
- 重载+运算符以连接2个双列表
- 如何通过运算符重载连接两个单独链接的列表
- C++-WinAPI获取所有连接的USB设备的列表
- 连接到网络的所有设备的列表
- 连接的串行端口C Qt的列表
- OpenCV如何在二进制图像中查找连接组件的列表
- 如何打印连接设备列表
- 0 (1) std或boost列表连接