zoukankan      html  css  js  c++  java
  • 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
    ----程序结束----

  • 相关阅读:
    laravel 的passport Oauth 认证登录请求 的 oauth_token 重置
    多个php版本的composer使用
    MySQL查询语句练习题(面试时可能会遇到哦!)
    tp5 url 线上访问 在nginx 上 出现404错误,解决办法(1.80nginx 配置 pathInfo)
    源码编译安装lnmp环境(nginx-1.14.2 + mysql-5.6.43 + php-5.6.30 )------踩了无数坑,重装了十几次服务器才会的,不容易啊!
    Mysql错误处理: /usr/bin/mysqld_safe: line xxx: xxxx Killed ... (mysql自动停止 Plugin FEDERATED is disabled 的完美解决方法)
    Plugin 'FEDERATED' is disabled. /usr/sbin/mysqld: Table 'mysql.plugin' doesn't exist
    thinkphp5的mkdir() Permission denied问题
    微信小程序 Unexpected end of JSON input/Unexpected token o in JSON at position 1
    服务器 apache配置https,http强制跳转https(搭建http与https共存)
  • 原文地址:https://www.cnblogs.com/zhaoke271828/p/14215870.html
Copyright © 2011-2022 走看看