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:\kingz\CSharpCodes\DataStructs\Program.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
----程序结束----

posted @ 2020-12-31 12:24  倦鸟已归时  阅读(585)  评论(0编辑  收藏  举报