帮助使用 C++ 中的双向链表

发布于 2024-11-09 11:52:28 字数 522 浏览 0 评论 0原文

所以我在解决这个问题时遇到了一些麻烦。 我创建了一个名为 dlist 的双向链表(其中包含一个 linknode 辅助类)。我有另一个名为 DeckOps 的类,它与链接列表一起使用。 dlist 已经包含 insertFront、removeFront、insertRear、removeRear 函数和打印函数。目前,我将文件名传递给 DeckOps,然后读取文件,将 int 输入到列表的每个链接节点中(从后面输入以保持与文件中相同的顺序)。

现在我的问题是,我需要能够在列表中找到一个数字,所以我假设我需要一个查找函数。然后我需要能够选择数字下方的所有内容并将其与数字上方的所有内容交换。我认为单独的交换功能会很好吗? 我这里的主要问题是如何选择一个组并与另一个组交换。

谢谢

示例: 1 2 3 4 5 6 7 8 9 find 6 find 4 将 6 以下的所有内容与 4 以上的所有内容交换 7 8 9 4 5 6 1 2 3 结果 编辑: 在写出这个例子时刚刚意识到程序需要知道我搜索的数字是否接近顶部或底部。

不是寻找解决方案,只是寻求一些帮助。

so I am having a little trouble wrapping my head around this problem.
I have created a doubly linked list called dlist (which contains a linknode helper class). I have another class called DeckOps that is used along with the linked list. dlist already contains functions to insertFront, removeFront, insertRear, removeRear and a print function. Currently I have it working where I pass a filename to DeckOps, which then reads in a file, inputing an int into each link node of the list (putting in from rear to hold same order as in the file).

Now my problem, I need to be able to find a number in the list, so I assume I will need a find function. and then I need to be able to select everything below a number and swap it with everything above a number. a seperate swap function would be nice i think?
My main issue here is how to select a group and swap with another group.

thanks

example:
1 2 3 4 5 6 7 8 9 find 6 find 4 swap everything below 6, with everything above 4
7 8 9 4 5 6 1 2 3 result
EDIT:
just realized in writing out this example is the program needs to know if the number i search for is closer to the top or to the bottom.

not looking for a solution, just some help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感悟人生的甜 2024-11-16 11:52:28

只是好奇……有什么理由不使用 STL 容器类吗?

你真的需要重新发明轮子吗?您是否有认为 STL 无法处理的需求?您认为您可以编写比 STL“更好”的代码吗?您是否正在为没有 STL 支持的嵌入式设备或不起眼的处理器编写代码?

即使不是STL,你考虑过Boost++吗?

您可能在这里问了错误的问题......也许您应该问自己是否可以从其余的开发中抽出时间来重新发明轮子。

Just curios ... is there any reason not to use an STL container class?

Do you really need to reinvent the wheel? Do you have a requirement that you think STL can't handle? Do you think you can code "better" then the STL? Are you perhaps coding for an embedded device or obscure processor with no STL support?

Even if not STL, did you consider Boost++ ?

You may be asking the wrong question here ... perhaps you should be asking yourself if can spare time from the rest of your development to reinvent the wheel.

椒妓 2024-11-16 11:52:28

如果我正确地解释了“交换”的含义,则需要实现 列表拼接< /a>.您可以将开头的范围移至新列表,然后将其移回原始列表的末尾,也可以一次完成所有操作。后者只是稍微复杂一些,主要是前提条件是插入点不在要移动的范围内。

std::list 示例

int main() {
  std::list<int> L;
  for (int n = 0; n != 10; ++n) L.push_back(n);
  std::list<int>::iterator x = std::find(L.begin(), L.end(), 6);
  assert(x != L.end());  // We know this is true in this example.

  std::list<int> temp;
  temp.splice(temp.end(), L, L.begin(), x);
  temp.splice(temp.begin(), L, x);
  L.splice(L.end(), temp);

  std::copy(L.begin(), L.end(), std::ostream_iterator<int>(std::cout, ", "));

  return 0;
}

输出:

  7, 8, 9, 6, 0, 1, 2, 3, 4, 5, 
# ^-----^     ^--------------^
#   \... swapped with .../

If I correctly interpret what you mean by "swap", you need to implement list splicing. You can either move the range at the start to a new list, then move it back to the end of the original list, or you can do it all at once. The latter is only slightly more complicated, mostly in terms of preconditions that the insertion point isn't within the range to be moved.

Example with std::list:

int main() {
  std::list<int> L;
  for (int n = 0; n != 10; ++n) L.push_back(n);
  std::list<int>::iterator x = std::find(L.begin(), L.end(), 6);
  assert(x != L.end());  // We know this is true in this example.

  std::list<int> temp;
  temp.splice(temp.end(), L, L.begin(), x);
  temp.splice(temp.begin(), L, x);
  L.splice(L.end(), temp);

  std::copy(L.begin(), L.end(), std::ostream_iterator<int>(std::cout, ", "));

  return 0;
}

Output:

  7, 8, 9, 6, 0, 1, 2, 3, 4, 5, 
# ^-----^     ^--------------^
#   \... swapped with .../
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文