C#中实现链表(本人觉得有用)

来源:互联网 发布:unity3d培训周末班 编辑:程序博客网 时间:2024/05/13 05:51

     由于C#是面向对象的,但比之C++而言,缺少了指针这一功能,虽然没了繁琐的指针,但有得必有失,本人总结了在C#中通过类来实现C++中用指针实现的链表的功能。个人觉得比较有用。

public class Link
    {
        int k=1;
        public Node head = new Node();        //表头
        public Node end = new Node();                 //表尾
        public Node point = null;                         //指针
        public Link() //构造函数
        {
            head.rear = null;
            end.next = null;
            head.next = end;
            end.rear = head;
            point = head;

            //以下为类中的成员变量,以初始化,可以自己定义成员变量
            head.address = "";
            head.first_name = "";
            head.second_name = "";
            end.address = "";
            end.first_name = "";
            end.second_name = "";
        }

        //关于此函数的说明,Classmates,Teacher,Friend均是node的基类,特此说明,每次创建节点都大同小异

        public void CreateNode(string str1,string str2,string str3,string str4, int i)
        {
            point = head;
            while (point.next.next != null)
            {
                point = point.next;
            }
            switch (i)
            {
                case 0:
                    Classmates cl = new Classmates(str1, str2, str3, str4, i);
                    cl.rear=point;
                    cl.next=point.next;
                    point.next.rear = cl;
                    point.next = cl;
                    point =cl;
                    cl.j = k;
                    k++;
                    break;
                case 1:
                    Teacher tc = new Teacher(str1, str2, str3, str4,i);
                    tc.rear = point;
                    tc.next = point.next;
                    point.next.rear = tc;
                    point.next = tc;
                    point = tc;
                    tc.j = k;
                    k++;
                    break;
                case 2:
                    Friend frd = new Friend(str1, str2, str3, str4,i);
                    frd.rear = point;
                    frd.next = point.next;
                    point.next.rear = frd;
                    point.next = frd;
                    point = frd;
                    frd.j = k;
                    k++;
                    break;

            }
        }
        public void DelNode(Node p)
        {
            p.rear.next = p.next;
            p.next.rear = p.rear;
        }

       //此方法中的to_string()方法是自己定义的,用于转化类中要输出的成员变量
        public string print(Node node,int i)
        {
            string str;
            switch (i)
            {
                case 0:
                    str = ((Classmates)node).to_string();
                    break;
                case 1:
                    str = ((Teacher)node).to_string();
                    break;
                case 2:
                    str = ((Friend)node).to_string();
                    break;
                default:
                    str = "Null";
                    break;
            }
            return str;
        }
    }

 

public class Node
    {
        public Node next = null;
        public Node rear = null;
        public string first_name;
        public string second_name;
        public string address;
        public int i;
        public int j;
    }