zoukankan      html  css  js  c++  java
  • 单链表反转

    大王女王大人若是看不懂的话,请把注释去掉。

    using System;
    
    namespace ConsoleApplication10
    {
        class Program
        {
            static void Main(string[] args)
            {
                Node headNode = new Node(0);
                Node preNode = headNode;
                Node currentNode;
                for (int i = 1; i < 10; i++)
                {
                    currentNode = new Node(i);
                    preNode.nextNode = currentNode;
                    preNode = currentNode;
                }
                headNode.outPut();
                Console.WriteLine("---------------------翻转后---------------------");
    
                ////这里不加ref的话,会拷贝一份指针进方法,导致headNode的值不被改变
                reverse(ref headNode);
                headNode.outPut();
    
                Console.Read();
            }
            public static void reverse(ref Node head)
            {
                if (head != null && head.nextNode !=null)
                {
                    Node pre = head;
                    Node current = head.nextNode;
                    Node next;
    
                    //Console.WriteLine("最初:  pre:" + pre.number.ToString() + "  current:" + current.number.ToString());
                    //Console.WriteLine("---------------------");
    
                    pre.nextNode = null;
                    while (current != null)
                    {
                        next = current.nextNode;
                        //Console.WriteLine("翻转指针前:   pre:" + pre.number.ToString() + "  current:" + (current != null ? current.number.ToString() : string.Empty) + "   next:" + (next != null ? next.number.ToString() : string.Empty));
    
                        current.nextNode = pre;
                        pre = current;
                        current = next;
    
                        //Console.WriteLine("翻转指针后:   pre:" + pre.number.ToString() + "  current:" + (current != null ? current.number.ToString() : string.Empty) + "   next:" + (next != null ? next.number.ToString() : string.Empty));
                        //Console.WriteLine("---------------------");
    
                    }
                    head = pre;
                }
            }
        }
        class Node
        {
            public Node(int i)
            {
                this.number = i;
            }
            public int number;
            public Node nextNode;
            public void outPut()
            {
                Console.WriteLine(this.number);
                if (this.nextNode != null)
                {
                    this.nextNode.outPut();
                }
            }
        }
    }
  • 相关阅读:
    shell脚本的常用参数
    Qt中使用Protobuf简单案例(Windows + msvc)
    使用PicGo和Typora写Markdown
    CentOS7安装protobuf(C++)和简单使用
    protobuf编译、安装和简单使用C++ (Windows+VS平台)
    protocol buffers 文档(一)-语法指导
    Base64编码和其在图片的传输的应用
    Qt程序打包发布
    Qt中的Label和PushButton背景图自动缩放设置
    TCP的粘包和拆包问题及解决
  • 原文地址:https://www.cnblogs.com/JingG/p/3101981.html
Copyright © 2011-2022 走看看