[英]reverse doubly linked list in c#
I'am trying to reverse circular doubly linked list, which looks like this: 我正在尝试反向循环双链表,如下所示:
Here is my Node class: 这是我的Node类:
private class Node<T>
{
public T Data { get; set; }
public Node<T> PreviousNode { get; set; }
public Node<T> NextNode { get; set; }
public Node(object data, Node<T> next, Node<T> previous)
{
Data = (T) data;
PreviousNode = previous;
NextNode = next;
}
}
And here is part of my Linked list class, here is my reverse funtion is stored: 这是我的链接列表类的一部分,这是我存储的反向函数:
public class DoublyLinkedList<T> :IList<T>
{
private Node<T> headerNode;
public DoublyLinkedList()
{
headerNode = new Node<T>(null, null, null);
headerNode.NextNode = headerNode;
headerNode.PreviousNode = headerNode;
Count = 0;
}
public void Insert(int index, T item)
{
Node<T> node;
if (index == Count)
node = new Node<T>(item, headerNode, headerNode.PreviousNode);
else
{
Node<T> tmp = FindNodeAt(index);
node = new Node<T>(item, tmp, tmp.PreviousNode);
}
node.PreviousNode.NextNode = node;
node.NextNode.PreviousNode = node;
Count++;
}
public void Reverse()
{
Node<T> temp;
for (Node<T> node = headerNode.NextNode; node != headerNode; node = node.NextNode)
{
}
}
I completly stuck with this Reverse() function. 我完全坚持使用此Reverse()函数。 Any help?
有什么帮助吗?
The algorithm for reversing a linked list is very simple: 反向链接列表的算法非常简单:
So, break it down into smaller pieces. 因此,将其分解成较小的部分。 Can you write methods that (1) check if a list is empty (2) or one element (3) remove the first item from a non-empty list, (4) put an item onto the head of a list ?
您是否可以编写以下方法(1)检查列表是否为空(2)或一个元素(3)从非空列表中删除第一项,(4)将项目放在列表的开头? If you can write those four methods then you can combine them together to write Reverse.
如果您可以编写这四种方法,则可以将它们组合在一起以编写Reverse。
This is how you should be approaching your programming problems; 这就是解决编程问题的方式。 programming is all about breaking complex problems down into simpler problems, solving the simpler problems, and combining the solutions.
编程就是将复杂的问题分解成更简单的问题,解决这些更简单的问题,并组合解决方案。
Rather than reverse the list why not write a couple of methods that get the "next" and "previous" node of the list depending on a direction flag that's passed in: 与其反转列表,不如编写一些方法来根据传入的方向标记获取列表的“下一个”和“上一个”节点:
public Node Next(Node current, bool forward)
{
return forward ? current.NextNode : current.PreviousNode;
}
public Node Previous(Node current, bool forward)
{
return forward ? current.PreviousNode : current.NextNode;
}
You could replace bool forward
by an enum with values Forwards
& Backwards
if that'd make the code easier to read. 如果可以使代码更易于阅读,则可以用值为
Forwards
& Backwards
的枚举代替bool forward
。
If you want to reverse the current list - then this should work : 如果您想撤消当前列表-那么应该可以:
public void Reverse()
{
if (count < 2)
return;
Node<T> node = headerNode;
do
{
Node<T> temp = node.NextNode;
node.NextNode = node.PreviousNode;
node.PreviousNode = temp;
node = temp;
}
while (temp != headerNode)
}
The first lines check for empty or single element list. 第一行检查空列表或单个元素列表。 The main loop iterates through the list swapping the previous & next nodes, stopping when it gets back to the header node.
主循环遍历列表,交换上一个和下一个节点,并在返回到头节点时停止。 The result is a reversed list.
结果是一个反向列表。
Thanks everybody, I have found the solution. 谢谢大家,我找到了解决方案。
public void Reverse()
{
var currNode = headerNode;
do
{
var temp = currNode.NextNode;
currNode.NextNode = currNode.PreviousNode;
currNode.PreviousNode = temp;
currNode = currNode.PreviousNode;
}
while (currNode != headerNode);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.