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




     



    }
  • 相关阅读:
    执行序列oracle存储过程和序列化写的demo
    快捷键列表myeclipse 设置代码提示快捷键
    鼠标管理解决win8 插上usb/鼠标蓝屏或无效方法
    JQuery实现拼图数字游戏
    Django的admin定制
    Django报:AttributeError: tuple object has no attribute get
    Django的models方法返回值异常,待解决
    主页跳转子页面的时候,模板语句中的数据未返回到页面(子页面空白)
    Django报:builtin_function_or_method' object is not iterable
    Windows Azure SDK 1.5、Windows Azure Tools for Microsoft Visual Studio 2010和新的服务管理功能发布了
  • 原文地址:https://www.cnblogs.com/baishahe/p/1147345.html
Copyright © 2011-2022 走看看