C#数据结构之单链表

链表是用一组任意的存储单元来存储线性表中的数据元素(这组存储单元可以是连续的,也可以是不连续的)。
单链表节点定义
//定义线性表的行为,可供顺序表类和单链表类继承
public interface IListDS
{
int GetLength();
void Insert(T item,int i);
void Add(T item);
bool IsEmpty();
T GetElement(int i);
void Delete(int i);
void Clear();
int LocateElement(T item);
void Reverse();
}
单链表操作实现


public class Node
{
    private T data;//数据域
    private Node next;//引用域

    //构造器:数据域和引用域,普通结点
    public Node(T val, Node p)
    {
        data = val;
        next = p; 
    }
    //构造器:引用域,头结点
    public Node(Node p)
    {
        next = p;
    }
    //构造器:数据域,尾结点
    public Node(T val)
    {
        data = val;
        next = null;
    }
    //构造器:无参数
    public Node()
    {
        data = default(T);
        next = null;
    }
    //数据域属性
    public T Data
    {
        get
        {
            return data;
        }
        set
        {
            data = value;
        }
    }
    //引用域属性
    public Node Next
    {
        get
        {
            return next;
        }
        set
        {
            next = value;
        }
    }

}

//链表类,包含链表定义及基本操作方法
public class MyLinkList
{
    private Node head; //单链表的头结点
    
    //头结点属性
    public Node Head
    {
        get { retrun head; }
        set { head = value; }
    }
    //构造器
    public MyLinkList()
    {
        head = null;
    }
    //求单链表的长度
    public int GetLength()
    {
        Node p = head;
        int len = 0;
        while(p!=null)
        {
            ++len;
            p = p.Next;
        }
        return len;
    }
    //清空单链表
    public void Clear()
    {
        head = null;
    }
    //判断单链表是否为空
    public bool IsEmpty()
    {
        if (head == null) { return true; }
        else { return false; }
    }
    //在单链表的末尾添加新元素
    public void Append(T item)
    {
        Node q = new Node(item);
        Node p = new Node();
        if(head==null)
        {
            head = q;
            return;
        }
        p = head;
        while(p.Next!=null)
        {
            p = p.Next;
        }
        p.Next = q;
    }
    //在单链表的第i个结点的位置前插入一个值为item的结点  
    public void Insert(T item, int i)
    {
        if (IsEmpty() || i < 1 || i > GetLength())
        {
            Console.WriteLine("LinkList is empty or Position is error!");
            return;
        }
        if (i == 1)
        {
            Node q = new Node(item);
            q.Next = head;
            head = q;
            return;
        }
        Node p = head;
        Node r = new Node();
        int j = 1;
        while (p.Next != null && j < i)
        {
            r = p;
            p = p.Next;
            ++j;
        }
        if (j == i)
        {
            Node q = new Node(item);
            q.Next = p;
            r.Next = q;
        }
    }
    //在单链表的第i个结点的位置后插入一个值为item的结点  
    public void InsertPost(T item, int i)
    {
        if (IsEmpty() || i < 1 || i > GetLength())
        {
            Console.WriteLine("LinkList is empty or Position is error!");
            return;
        }
        if (i == 1)
        {
            Node q = new Node(item);
            q.Next = head.Next;
            head.Next = q;
            return;
        }
        Node p = head;
        int j = 1;
        while (p != null && j < i)
        {
            p = p.Next;
            ++j;
        }
        if (j == i)
        {
            Node q = new Node(item);
            q.Next = p.Next;
            p.Next = q;
        }
    }
    //删除单链表的第i个结点  
    public T Delete(int i)
    {
        if (IsEmpty() || i < 0 || i > GetLength())
        {
            Console.WriteLine("LinkList is empty or Position is error!");
            return default(T);
        }
        Node q = new Node();
        if (i == 1)
        {
            q = head;
            head = head.Next;
            return q.Data;
        }
        Node p = head;
        int j = 1;
        while (p.Next != null && j < i)
        {
            ++j;
            q = p;
            p = p.Next;
        }
        if (j == i)
        {
            q.Next = p.Next;
            return p.Data;
        }
        else
        {
            Console.WriteLine("The " + i + "th node is not exist!");
            return default(T);
        }
    }
    //获得单链表的第i个数据元素  
    public T GetElem(int i)
    {
        if (IsEmpty() || i < 0)
        {
            Console.WriteLine("LinkList is empty or position is error! ");
            return default(T);
        }
        Node p = new Node();
        p = head;
        int j = 1;
        while (p.Next != null && j < i)
        {
            ++j;
            p = p.Next;
        }
        if (j == i)
        {
            return p.Data;
        }
        else
        {
            Console.WriteLine("The " + i + "th node is not exist!");
            return default(T);
        }
    }
    //在单链表中查找值为value的结点  
    public int Locate(T value)
    {
        if (IsEmpty())
        {
            Console.WriteLine("LinkList is Empty!");
            return -1;
        }
        Node p = new Node();
        p = head;
        int i = 1;
        while (!p.Data.Equals(value) && p.Next != null)
        {
            p = p.Next;
            ++i;
        }
        return i;
    }
    //显示链表  
    public void Display()
    {
        Node p = new Node();
        p = this.head;
        while (p != null)
        {
            Console.Write(p.Data + " ");
            p = p.Next;
        }
    }
}

public class Program
{
    static void Main(string[] args)
    {
        MyLinkList myLinkList = new MyLinkList(); //实例化一个单链表  
        Console.WriteLine(myLinkList.GetLength());   //获取长度  
                                                     //添加元素  
        myLinkList.Append("good");
        myLinkList.Append("monring");
        myLinkList.Append("lwk");
        myLinkList.Insert("!", 5);  //在i结点前插元素,位置错误测试  
        myLinkList.InsertPost("!", 5);  //在i结点后插元素,位置错误测试  
        myLinkList.InsertPost("!", 3);  //后插元素  
        myLinkList.Insert(",", 3);  //前插元素  
        myLinkList.Display();  //显示链表元素  
        Console.WriteLine(myLinkList.GetElem(4));//获取结点,并显示  
        myLinkList.Delete(1);  //删除结点  
        myLinkList.Display();
        Console.WriteLine(myLinkList.GetLength()); //显示链表长度  
        Console.Read();
    }
}

你可能感兴趣的:(C#数据结构之单链表)