利用C#的指针编写都一个简单链表

using System;
 
 namespace UnsafeTest
{
    unsafe struct link
    {
        public int x;
        public link* next;
    }
    class Program
    {
        static unsafe void Main(string[] args)
        {
            int val;
            link* head = stackalloc link[sizeof(link)];
            
            link*q =head;
            for(int i=0;i<=10;i++)
            {
                val = i+100;
                link* temp = stackalloc link[sizeof(link)];
                q->x = val;
                q->next = temp;
                q = temp;
            }
             
            link* t = head;
            while(t->next!=null)
            {
 
                Console.WriteLine(t->x );
                t = t->next;
            }
        }
    }
}

 

posted @ 2015-07-06 11:38  如.若  阅读(483)  评论(0编辑  收藏  举报