zoukankan      html  css  js  c++  java
  • 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;
            }
        }




     



    }
  • 相关阅读:
    hibernate_0100_HelloWorld
    MYSQL子查询的五种形式
    JSF是什么?它与Struts是什么关系?
    nop指令的作用
    htmlparser实现从网页上抓取数据(收集)
    The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the
    FCKeditor 在JSP上的完全安装
    Java遍历文件夹的2种方法
    充电电池和充电时间说明
    吃知了有什么好处
  • 原文地址:https://www.cnblogs.com/baishahe/p/1147345.html
Copyright © 2011-2022 走看看