带头节点链表的反转 循环和递归 C#实现

来源:互联网 发布:java hello word 编辑:程序博客网 时间:2024/04/29 18:09

练练脑子,呵呵. 要熟悉和理解算法,最好还是亲自动手写一下,发现不管是笔试面试,大家都爱问链表相关的问题, 下午动手调了一下, 实现了带头节点的链表描述,链表建立,链表遍历,链表反转(循环和递归)

 

 

Node.cs  节点类

-----------------

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace List
{
    public class ListNodes
    {
        public Node headNode;

        public ListNodes()
        {
            this.headNode = new Node();
           
        }

        public ListNodes(int length): this()
        {
            Node nodePointer = headNode;

            for (int i = 1; i <= length; i++)
            {
                Console.WriteLine("Please input the node data");
                int inpuData;
                try
                {
                    inpuData = Convert.ToInt32 ( Console.ReadLine());
                    Node newNode = new Node (inpuData );

                    nodePointer.next = newNode;
                    nodePointer = newNode;                   
                     
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    i--;
                    continue;
 
                }
            }
       
        }

        public void DisplayListNodes()
        {
            Node nodePointer = this.headNode;

            Console .WriteLine ("Print the List Node");

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

        }


        public void ReverseListNodes()
        {
            Node nodePointer = this.headNode.next;


            if (nodePointer != null)
            {
                Node nextNode = nodePointer.next ;
                Node nextNodeNext = nodePointer;
              
                while (nextNode != null )
                {
                            nextNodeNext = nextNode.next;

                            nextNode.next = nodePointer;
                            if( nodePointer == this.headNode .next )
                            {
                                 nodePointer.next = null;
                            }

                            nodePointer = nextNode;
                            nextNode = nextNodeNext;
                        
                 }

                this.headNode .next = nodePointer ;
                       

              }

                   
                   
 
                }
                  
               
     }

           
    
 }

  
 

链表集合类 ListNode.cs

------------------------------------

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace List
{
    public class ListNodes
    {
        public Node headNode;

        public ListNodes()
        {
            this.headNode = new Node();
           
        }

        public ListNodes(int length): this()
        {
            Node nodePointer = headNode;

            for (int i = 1; i <= length; i++)
            {
                Console.WriteLine("Please input the node data");
                int inpuData;
                try
                {
                    inpuData = Convert.ToInt32 ( Console.ReadLine());
                    Node newNode = new Node (inpuData );

                    nodePointer.next = newNode;
                    nodePointer = newNode;                   
                     
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    i--;
                    continue;
 
                }
            }
       
        }

        public void DisplayListNodes()
        {
            Node nodePointer = this.headNode;

            Console .WriteLine ("Print the List Node");

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

        }


        public void ReverseListNodes()
        {
            Node nodePointer = this.headNode.next;


            if (nodePointer != null)
            {
                Node nextNode = nodePointer.next ;
                Node nextNodeNext = nodePointer;
              
                while (nextNode != null )
                {
                            nextNodeNext = nextNode.next;

                            nextNode.next = nodePointer;
                            if( nodePointer == this.headNode .next )
                            {
                                 nodePointer.next = null;
                            }

                            nodePointer = nextNode;
                            nextNode = nextNodeNext;
                        
                 }

                this.headNode .next = nodePointer ;
                       

              }

                   
                   
 
                }


        public void ReverserListNodeRecur()
        {
            Node nodePointer = this.headNode .next ;
            if( nodePointer !=null )
            {
                this.headNode .next = ReverseListNoHead(nodePointer);

            }
 
        }

        static public Node  ReverseListNoHead(Node lNode)
        {
            if (lNode != null)
            {
                if (lNode.next != null)
                {
                    Node n = ReverseListNoHead(lNode.next);
                    lNode.next.next = lNode;
                    lNode.next = null;
                    return n;

                }

                else
                {
                    return lNode;
                }

            }
            else
                return null;


        }
                  
               
     }

           
    
 }

  
 

 

 

Program.cs  测试桩

-------------------------

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace List
{
    class Program
    {
        static void Main(string[] args)
        {

            int length=0;
            bool continueFlag= true ;

            while (continueFlag)
            {
                try
                {
                    Console.WriteLine("Please input the length of the List");
                    length = Convert.ToInt32(Console.ReadLine());
                    if (length < 0)
                    {
                        Console.WriteLine("Please input an non negtive number");
                        continue;
                    }
                    break;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }

            Console.WriteLine("Start inputing the datas now");
           
            ListNodes newList = new ListNodes(length );
            newList.DisplayListNodes();

            newList.ReverseListNodes();

            Console.WriteLine("Reverse the List");

            newList.DisplayListNodes();

            Console.WriteLine("Reverse the List, Recursion");

            newList.ReverserListNodeRecur();

            Console.WriteLine("Reverse the List");

            newList.DisplayListNodes();


        }
    }
}

 

 

原创粉丝点击