C# 指针 链表 结构 UNSAFE

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication10
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            Node myNode = new Node();

            myNode.addNode("first");

            myNode.addNode("second");

            myNode.addNode("third");

            myNode.addNode("forst");

            Node node;

            node = myNode;
            while (node != null)
            {
                MessageBox.Show(node.Name);
                node = node.Next;
            }


            myNode.delNode(3);

            node = myNode;
            while (node != null)
            {
                MessageBox.Show(node.Name);
                node = node.Next;
            }

        }

        unsafe int sum(int *i, int *j)
        {
            return *i += *j;
        }

        unsafe int sumsafe(int i,int j)
        {
            return sum(&i,&j);
        }

        private unsafe void button2_Click(object sender, EventArgs e)
        {
            //int j = sumsafe(2, 3);
            int a = 2;            
            int b = 3;
            int j = sum(&a,&b);
            MessageBox.Show(j.ToString());
        }

        private unsafe void button3_Click(object sender, EventArgs e)
        {
            int i = 3;
            ptrNode ptr = new ptrNode(&i);

            int j = 3;
            ptrNode p = new ptrNode(&j);

            ptr.next = &p;

            
        }

    }


    
    public class Node
    {
        private string name;
        private Node head;
        private Node next;
        private int index;

        public string Name
        {
            get { return this.name; }
            set { this.name = value; }
        }

        public Node Head
        {
            get { return this.head; }
        }

        public Node Next
        {
            get { return this.next; }
        }

        public Node()
        {
            name = "NodeHead";
        }


        public void addNode(string name)
        {
            Node x = this;
            Node t = new Node();
            t.name = name;
            

            while (x.next != null)
            {
                x = x.next;
            }
        
            x.next = t;
            t.head = x;

            index = index + 1;
        }


        public void delNode(int index)
        {            
            Node temp = this;

            int i = 0;
            while (i < index)
            {
                temp = temp.next;
                i += 1;
            }

            temp.head.next = temp.next;
        }

    }


    unsafe struct ptrNode
    {
        public int* id;
        public ptrNode* next;

        public ptrNode(int * aid)
        {
            id = aid;
            next = null;
        }
    }




 



}
原文地址:https://www.cnblogs.com/baishahe/p/1147345.html