标题:c#如何实现像C中链表的功能?
只看楼主
ramos
Rank: 1
来 自:九江
等 级:新手上路
帖 子:7
专家分:0
注 册:2008-2-29
 问题点数:0 回复次数:3 
c#如何实现像C中链表的功能?
各位大侠们好!
   鄙人最近刚刚自学C#一段时间,但是还不知道像C中的链表能在C#中实现不?
   如果能,该如何实现?
   如果不能,那像往有序序列中插入数据的效率岂不很低?
  谢谢!!
搜索更多相关主题的帖子: 链表 功能 序列 鄙人 大侠 
2008-04-11 23:09
ioriliao
Rank: 7Rank: 7Rank: 7
来 自:广东
等 级:贵宾
威 望:32
帖 子:2829
专家分:647
注 册:2006-11-30
得分:0 
程序代码:
namespace ConsoleApplication15
{
    class Program
    {
        static void Main(string[] args)
        {
            unsafe
            {
                int* x;
                int y = 10;
                x = &y;
                Console.Write(*x);
                Console.ReadLine();
            }
        }
    }
}

我只能在这告诉你可以在C#中用指针


[[it] 本帖最后由 ioriliao 于 2008-4-12 08:50 编辑 [/it]]

/images/2011/147787/2011051411021524.jpg" border="0" />
2008-04-12 08:48
pangxiaoliang
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-4-12
得分:0 
我写的代码
可以啊
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication
{
    class Node<T>
    {
        public T data;
        public Node<T> next;
        public Node()
        {
            this.data = default(T);
            this.next = null;
        }
        public Node(T t)
        {
            this.data = t;
            this.next = null;
        }
    }
}
--------------------
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication
{
    class LinkList<T>
    {
        private Node<T> hNode;
        private Node<T> tmpNode;
        public LinkList()
        {
            this.hNode = new Node<T>();
        }
        public void AddItem(T t)
        {

                this.tmpNode = this.hNode;
                while (this.tmpNode.next != null)
                {
                    this.tmpNode = this.tmpNode.next;
                }
                this.tmpNode.next = new Node<T>(t);
        }
        public void LinkItem()
        {
            this.tmpNode = this.hNode;
            while (this.tmpNode.next != null)
            {
                 this.tmpNode = this.tmpNode.next;
            }
            this.tmpNode.next = this.hNode.next;
        }
        public bool IsCycle()
        {
            Node<T> n1;
            Node<T> n2;
            n1 = hNode;
            n2 = hNode;
            do
            {
                n2 = n2.next;
                n1 = n1.next.next;
                if (n1 == null)
                {
                    return false;
                }
               
            } while (!n1.Equals(n2));
            return true;
        }
        public void Show()
        {
            Node<T> n;
            n = hNode;
            while (n.next != null)
            {
                Console.WriteLine(n.data);
                n = n.next;
            }
        }
        public T this[int index]
        {
            get
            {
                this.tmpNode = this.hNode;
                while((index--)!=0)
                {
                    this.tmpNode = this.tmpNode.next;
                }
                return this.tmpNode.data;
            }
        }
    }
}
2008-04-12 09:12
ramos
Rank: 1
来 自:九江
等 级:新手上路
帖 子:7
专家分:0
注 册:2008-2-29
得分:0 
知道吗?我感动的鼻涕都快流出来了!!
   真是太感谢了!!
2008-04-12 11:17



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-208523-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.344108 second(s), 9 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved