• 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;
            }
        }




     



    }
  • 相关阅读:
    Sass--扩展继承
    Sass--混合宏的不足
    学习笔记47—PhotoShop技巧
    学习笔记46—如何使Word和EndNote关联
    学习笔记45—Linux压缩集
    学习笔记44—Linux下安装freesurfer
    学习笔记43—Linux安装集
    学习笔记42—Win7下安装Linux双系统
    学习笔记41—ttest误区
    学习笔记40—endnote点点滴滴
  • 原文地址:https://www.cnblogs.com/baishahe/p/1147345.html
Copyright © 2020-2023  润新知