1. 程式人生 > >c# 單鏈表實現程式碼

c# 單鏈表實現程式碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 單鏈表
{

    class SNode<T>  //結點資料
    {
        private T data; //資料域,結點資料
        private SNode<T> next; //引用域,下一個結點
        public SNode(T val, SNode<T> p)  //普通結點  資料域,結點域
        {
            data = val;
            next = p;
        }
        public SNode(SNode<T> p)    //頭結點
        {
            next = p;
        }
        public SNode(T val) //尾結點
        {
            data = val;
            next = null;
        }
        public SNode()  //無引數
        {
            data = default(T);
            next = null;
        }
        //屬性
        public T Data
        {
            get { return data; }
            set { data = value; }
        }
        public SNode<T> Next
        {
            get { return next; }
            set { next = value; }
        }
    }
    class SLinkList<T> : SNode<T>
    {
        private SNode<T> start; //頭結點
        int length;     //長度
        public SLinkList()  //構造器
        {
            start = null;
        }
        //在單鏈表的末尾追加資料元素
        public void InsertNode(T a)
        {
            if (start == null)
            {
                start = new SNode<T>(a);
                length++;
                return;
            }
            SNode<T> current = start;
            while (current.Next != null)
            {
                current = current.Next;
            }
            current.Next = new SNode<T>(a);
            length++;
        }
        //在單鏈表的第i個元素的位置前插入一個元素
        public void InsertNode(T a, int i)
        {
            SNode<T> current;   //當前結點
            SNode<T> previous;  //上一個結點
            if (i < 1 || i > length + 1)
            {
                Console.WriteLine("位置錯誤!");
                return;
            }
            SNode<T> newnode = new SNode<T>(a);
            //在空連結串列或第一個元素前插入第一個元素
            if (i == 1)
            {
                newnode.Next = start;
                start = newnode;
                length++;
                return;
            }
            //單鏈表的兩個元素間插入一個元素
            current = start;
            previous = null;
            int j = 1;
            while (current != null && j < i)
            {
                previous = current;
                current = current.Next;
                j++;
            }
            if (j == i)
            {
                newnode.Next = current;
                previous.Next = newnode;
                length++;
            }
        }
        //刪除單鏈表的第i個元素
        public void DeleteNode(int i)
        {
            if (IsEmpty() || i < 1)
            {
                Console.WriteLine("連結串列為空或者位置錯誤!");
                return;
            }
            SNode<T> current = start;
            if (i == 1)
            {
                start = current.Next;
                length--;
                return;
            }
            SNode<T> previous = null;
            int j = 1;
            while (current.Next != null && j < i)
            {
                previous = current;
                current = current.Next;
                j++;
            }
            if (j == i)
            {
                previous.Next = current.Next;
                current = null;
                length--;
            }
            else
            {
                Console.WriteLine("結點不存在!");
            }
        }
        //獲得單鏈表的第i個元素
        public T SearchNode(int i)
        {
            if (IsEmpty())
            {
                Console.WriteLine("連結串列為空!");
                return default(T);
            }
            SNode<T> current = start;
            int j = 1;
            while (current.Next != null && j < i)
            {
                current = current.Next;
                j++;
            }
            if (j == i)
            {
                return current.Data;
            }
            else
            {
                Console.WriteLine("結點不存在!");
                return default(T);
            }
        }
        //在單鏈表中查詢值為value的元素
        public T SearchNode(T value)
        {
            if (IsEmpty())
            {
                Console.WriteLine("連結串列為空!");
                return default(T);
            }
            SNode<T> current = start;
            while (!current.Data.ToString().Contains(value.ToString()) && current != null)
            {
                current = current.Next;
            }
            if (current != null)
                return current.Data;
            else
                return default(T);
        }
        //列印連結串列的資料
        public void DisPlayData()
        {
            SNode<T> current = start;
            while (current!=null)
            {
                Console.WriteLine(current.Data+" ");
                current = current.Next;
            }

        }
        //求連結串列的長度
        public int GetLength()
        {
            return length;
        }
        //清空連結串列
        public void Clear()
        {
            start = null;
        }
        //判斷單鏈表是否為空
        public bool IsEmpty()
        {
            if (start == null)
            {
                return true;
            }
            return false;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            SLinkList<string> s = new SLinkList<string>();
            s.InsertNode("a");

            s.DisPlayData();
            Console.ReadKey();

        }
    }
}