• C#打印单链表各节点数据的内存地址


    首先写一下单链表的程序代码包括节点类和单链表类:

        注意此时data是private保护级别,只能通过Data来访问,因此会出现的问题后面给出修改方案

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DataStructs
    {
        public class Node<T>
        {
            private T data;
            private Node<T> next;
    
            public Node<T> Next { get => next; set => next = value; }
            public T Data { get => data; set => data = value; }
    
            public Node(T val)
            {
                Data = val;
                Next = null;
            }
    
            public Node()
            {
                Data = default(T);
                Next = null;
            }
    
    
        }
    }

    然后给出单链表定义:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DataStructs
    {
        class SinglyList<T>
        {
            private Node<T> head;
    
            public Node<T> Head { get => head; set => head = value; }
    
            public SinglyList()
            {
                head = null;
            }
            
            public int GetLength()
            {
                Node<T> p = head;
                int len = 0;
                while (p != null)
                {
                    len++;
                    p = p.Next;
                }
                return len;
            }
            public void Clear()
            {
                head = null;
            }
    
            public bool IsEmpty()
            {
                if (head == null)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            public void Append(T item) {
                Node<T> q = new Node<T>(item);
                Node<T> p = new Node<T>();
                if(head == null)
                {
                    head = q;
                    return;
                }
                p = head;
                while (p.Next != null)
                {
                    p = p.Next;
                }
                p.Next = q;
            }
    
            public Node<T> Pull()
            {
                if (IsEmpty())
                {
                    return null;
                }
                Node<T> pre = head;
    
                Node<T> node = null;
                if (pre.Next == null)
                {
                    Clear();
                    return pre;
                }
                node = pre.Next;
                while (node.Next != null)
                {
                    pre = node;
                    node = node.Next;
                }
                pre.Next = null;
                return node;
            }
        }
    }

    在主程序里,打印每一个节点的内存地址:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DataStructs
    {
        class Program
        {
            static void Main(string[] args)
            {
                int[] array = { 2, 4, 6, 8, 10 };
                SinglyList<int> list = new SinglyList<int>();
                for(int i = 0; i < array.Length; i++)
                {
                    list.Append(array[i]);
                }
                int index = list.GetLength()-1;
                while (!list.IsEmpty())
                {
                    Node<int> var = list.Pull();
                    Console.WriteLine("节点{0}: {1}", index--, var.Data);
                    unsafe
                    {
                         fixed(int *p = &var.data)
                        {
                            Console.WriteLine("地址:{0}", (int)p);
                        }
                    }
                }
                Console.WriteLine("----程序结束----");
                Console.ReadKey();
            }
        }
    }

    这个时候,Visual Studio会给我们报错提示:

      错误:CS0211 无法获取给定表达式的地址 DataStructs D:kingzCSharpCodesDataStructsProgram.cs 28 活动:

      

    也就是说,代码“fixed(int *p = &var.Data)”出现了错误,这一句var.Data是调用了getter函数。

      修改方法也很简单:先修改Node节点中data的保护级别,然后取址符后面的调用修改为直接调用值。

        1. 把Node类里面的private T data; 修改为public T data;

        2. 然后将“fixed(int *p = &var.Data)”修改为“fixed(int *p = &var.data)”即可

    程序打印结果如下:

    节点4: 10
    地址:41493600
    节点3: 8
    地址:41493568
    节点2: 6
    地址:41493536
    节点1: 4
    地址:41493504
    节点0: 2
    地址:41493472
    ----程序结束----

    这是十进制地址,要打印十六进制地址只需要修改一处:

      Console.WriteLine("地址:0x{0:X}", (int)p);

    程序执行如下:

    节点4: 10
    地址:0x2F42468
    节点3: 8
    地址:0x2F42448
    节点2: 6
    地址:0x2F42428
    节点1: 4
    地址:0x2F42408
    节点0: 2
    地址:0x2F423E8
    ----程序结束----

  • 相关阅读:
    netty(八) netty中自带channelhandler
    netty(七) Handler的执行顺序
    netty(六) websocket开发应用
    netty(五) http协议开发应用
    netty(四) 编解码技术
    netty(三) TIP黏包/拆包问题解决之道
    netty(二) 创建一个netty服务端和客户端
    netty(一) netty有哪几部分构成
    使用jsp制作index,可以通过<c:if test==“管理员”>或<c:if test=="客户">来区别展示用户界面
    使用jstl和el表达式来展示request域中存放的user对象的信息
  • 原文地址:https://www.cnblogs.com/zhaoke271828/p/14215870.html
Copyright © 2020-2023  润新知