简体   繁体   English

在递归函数C#中反向链接列表

[英]reverse a linked list in a recursive function c#

I'm having problems trying to write a reverse recursive method for a LinkedList class I created in C#. 我在尝试为在C#中创建的LinkedList类编写反向递归方法时遇到问题。 The LinkedList has 2 pointers in it one for the head and the other for the tail: LinkedList中有2个指针,一个指向头部,另一个指向尾部:

public class Node
{
    public object data;
    public Node next;
    public Node(object Data)
    {
        this.data = Data;
    }
}

public class LinkedList
{
    Node head;
    Node tail;
    public void Add(Node n)
    {
        if (head == null)
        {
            head = n;
            tail = head;
        }
        else
        {
            tail.next = n;
            tail = tail.next;
        }
    }

Now, the recursive reverse function goes like this: 现在,递归反向函数如下所示:

    public void reverse_recursive()
    {
        Node temp_head = head;
        if (temp_head == tail)
        {

            return;
        }

        while (temp_head != null)
        {
            if (temp_head.next == tail)
            {
                tail.next = temp_head;
                tail = temp_head;
                reverse_recursive();
            }
            temp_head = temp_head.next;
        }
    }

I'm having 2 troubles with it: first, a logic problem, I know that head doesn't point to the first node after the reverse. 我遇到了2个麻烦:首先是逻辑问题,我知道头并没有指向反向之后的第一个节点。 The second problem is that i probably do something wrong with the null pointer so the program crashes. 第二个问题是我可能会对null指针做些错误,因此程序崩溃了。

I also give you the main program: 我也给你主程序:

class Program
{
    static void Main(string[] args)
    {
        LinkedList L = new LinkedList();
        L.Add(new Node("first"));
        L.Add(new Node("second"));
        L.Add(new Node("third"));
        L.Add(new Node("forth"));
        L.PrintNodes();
        L.reverse_recursive();
        L.PrintNodes();
        Console.ReadLine();
    }
}

Thank you for helping!! 感谢您的帮助!!

public void Reverse()
{
    this.Reverse(this.head);
}

private void Reverse(Node node)
{
    if (node != null && node.next != null)
    {
        // Create temporary references to the nodes, 
        // because we will be overwriting the lists references.
        Node next = node.next;
        Node afterNext = node.next.next;
        Node currentHead = this.head;

        // Set the head to whatever node is next from the current node.
        this.head = next;

        // Reset the next node for the new head to be the previous head.
        this.head.next = currentHead;

        // Set the current nodes next node to be the previous next nodes next node :)
        node.next = afterNext;

        // Keep on trucking.
        this.Reverse(node);
    }
    else
    {
        this.tail = node;
    }
}
        public void reverse()
        {
            reverse_recursive(tail);

            Node tmp = tail;
            tail = head;
            head = tmp;
        }

        public void reverse_recursive(Node endNode)
        {
            Node temp_head = head;
            if (temp_head == endNode)
            {
                return;
            }

            while (temp_head != null)
            {
                if (temp_head.next == endNode)
                {
                    break;
                }
                temp_head = temp_head.next;
            }

            endNode.next = temp_head;
            temp_head.next = null;
            reverse_recursive(temp_head);
        }

See also this 另请参见

Another option over here. 这里的另一个选择。

class Program{
    static void Main(string[] args)
    {
        LinkedList L = new LinkedList();
        L.Add(new Node("first"));
        L.Add(new Node("second"));
        L.Add(new Node("third"));
        L.Add(new Node("forth"));
        L.PrintNodes();
        L.reverse_recursive();
        Console.WriteLine("---------------------");
        L.PrintNodes();
        Console.ReadLine();
    }
}

public class Node
{
    public object data;
    public Node next;
    public Node(object Data)
    {
        this.data = Data;
    }
}

public class LinkedList
{
    Node head;
    Node tail;
    public void Add(Node n)
    {
        if (head == null)
        {
            head = n;
            tail = head;
        }
        else
        {
            tail.next = n;
            tail = tail.next;
        }

    }

    public void PrintNodes()
    {
        Node temp = head;

        while (temp != null)
        {
            Console.WriteLine(temp.data);
            temp = temp.next;
        }
    }


    private LinkedList p_reverse_recursive(Node first)
    {
        LinkedList ret;
        if (first.next == null)
        {
            Node aux = createNode(first.data);
            ret = new LinkedList();
            ret.Add(aux);
            return ret;
        }
        else
        {
            ret = p_reverse_recursive(first.next);
            ret.Add(createNode(first.data));
            return ret;
        }

    }

    private Node createNode(Object data)
    {
        Node node = new Node(data);
        return node;
    }

    public void reverse_recursive()
    {

        if (head != null)
        {
            LinkedList aux = p_reverse_recursive(head);
            head = aux.head;
            tail = aux.tail;
        }


    }
}

Hope it helps 希望能帮助到你

A second variant 第二种变体

private void p_reverse_recursive2(Node node)
    {
        if (node != null)
        {
            Node aux = node.next;
            node.next = null;
            p_reverse_recursive2(aux);
            if (aux != null)
                aux.next = node;
        }

    }

    public void reverse_recursive()
    {

        if (head != null)
        {               
            Node aux = head;
            head = tail;
            tail = aux;
            p_reverse_recursive2(tail);
        }


    }

Variation on a theme... 主题变化...

public Node Reverse(Node head)
{
    if(head == null)
    {
        return null;
    }

    Node reversedHead = null;
    ReverseHelper(head, out reversedHead);
    return reversedHead;
}

public Node ReverseHelper(Node n, out Node reversedHead)
{
    if(n.Next == null)
    {
        reversedHead = n;
        return n;
    }

    var reversedTail = ReverseHelper(n.Next, out reversedHead);
    reversedTail.Next = n;
    n.Next = null;
    return n;       
    }   
}

I was just playing with similar brain teaser with the only difference that LinkedList class only has a definition for head and all the rest of the nodes are linked there. 我只是在玩类似的脑筋急转弯,唯一的区别是LinkedList类仅具有head定义,所有其余节点都链接在那里。 So here is my quick and dirty recursive solution: 所以这是我快速又肮脏的递归解决方案:

public Node ReverseRecursive(Node root)
        {
            Node temp = root;
            if (root.next == null)
                return root;
            else
                root = ReverseRecursive(root.next);
            temp.next = null;
            Node tail = root.next;
            if (tail == null)
                root.next = temp;
            else
                while (tail != null)
                {
                    if (tail.next == null)
                    {
                        tail.next = temp;
                        break;
                    }
                    else
                        tail = tail.next;
                }
            return root;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM