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

首先写一下单链表的程序代码包括节点类和单链表类: 注意此时data是private保护级别,只能通过Data来访问,因此会出现的问题后面给出修改方案using System;using System.Collections.Generic;using System.Lin...

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

    注意此时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 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]);
            }
            Console.WriteLine("是否为空?:{0}", list.IsEmpty());
            int index = 0;
            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("是否为空?:{0}", list.IsEmpty());
            Console.WriteLine("----程序结束----");
            Console.ReadKey();
        }
    }
}

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


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

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

  修改方法也很简单,把Node类里面的private T data;

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

本文标题为:C#打印单链表各节点数据的内存地址

基础教程推荐