1. 程式人生 > >C#算法與數據結構之線性結構

C#算法與數據結構之線性結構

data tno 數據結構 image 存滿 sta als bre adk

線性結構是什麽?

  線性結構是一種數據結構,它有一對一的關系,就像一個長對,一個接一個,特點是,除第一個元素和最後一個元素外,其它元素前後只有一個元素。

  簡單示例1:

static void Main(string[] args)
{
//使用BCL中的List線性表
List<string> strList = new List<string>();
strList.Add("123");
strList.Add("456");
strList.Add("789");
Console.WriteLine(strList[1]);
Console.WriteLine(strList.Count);
strList.Remove("123"); //去除某一元素
Console.WriteLine(strList[1]);
Console.WriteLine(strList.Count);

}

  輸出為:456

      3  

      789

      2

線性表實現方式

    順序表:連續排放

     單鏈表:

      雙向鏈表

      循環鏈表

自設一個List<>:

首先創建一個接口,然後實現接口得所有方法,完程自建List功能:

  class SeqList<T>:IListDS<T>
    {
        private T[] data;     //存儲數據
        private int count = 0;

        public SeqList(int size)         
        {
            data 
= new T[size]; count = 0; } public SeqList():this(10) //默認構造函數10 { } public void Add(T item) //添加物件 { if(count == data.Length) { Console.WriteLine("空間已存滿"); } else { data[count]
= item; count++; } } //取得數據個數 public int GetLength() //得到長度 { return count; } public T GetEle(int index) //返回索引 { if(index>=0&&index<=count-1) { return data[index]; } else { Console.WriteLine("索引不存在"); return default(T); } } public void Clear() { count = 0; } public bool IsEmpty() { return count == 0; } public void Insert(T item,int index) { for(int i=count-1;i>=count;i--) { data[i + 1] = data[i]; } data[index] = item; count++; } public T Delete(int index) { T temp = data[index]; for(int i=index+1;i<count;i++) { data[i - 1] = data[i]; } count--; return temp; } public T this[int index] { get { return GetEle(index); } } public int Locate(T value) { for(int i=0;i<count;i++) { if(data[i].Equals(value)) { return i; } } return -1; } }

測試:

  static void Main(string[] args)
        {
            SeqList<string> seqList = new SeqList<string>();
            seqList.Add("123");
            seqList.Add("456");
            seqList.Add("789");
            Console.WriteLine(seqList.GetEle(1));
            Console.WriteLine(seqList[1]);
            seqList.Insert("666",1);
            for(int i=0;i<seqList.GetLength();i++)
            {
                Console.WriteLine(seqList[i] + ".");
            }
            seqList.Clear();
            Console.WriteLine(seqList.GetLength());
            Console.ReadKey();



        }

結果:

456
456
123.
666.
456.
789.
0 結果OK,功能實現

單鏈表: data next 示圖: 技術分享

 class Node<T>
    {
        private T data;           //存儲數據
        private Node<T> next;    //指針

        public Node()
        {
            data = default(T);
            next = null;
        }
        public Node(T value)
        {
            data=value;
            next = null;
        }
        public Node(T value,Node<T> next)
        {
            this.data = value;
            this.next = next;
        }
        public Node(Node<T> next)
        {
            this.next = next;
        }

        public T Data
        {
            get { return data; }
            set { data = value; }

        }
        public Node<T> Next
        {
            get { return next; }
            set { next = value; }
        }
        
        
    }
class LinkList<T>:IListDS<T>
    {
        private Node<T> head;
        public LinkList()
        {
            head = null;
        }
        public void Add(T item)
        {
            Node<T> newNode = new Node<T>(item);
            //頭節點為空,那麽新節點就是頭節點
            if(head==null)
            {
                head = newNode;
            }
            else
            {//把新來結點放鏈表尾部
                Node<T> temp = head;
                while(true)
                {
                    if(temp.Next!=null)
                    {
                        temp = temp.Next;
                    }
                    else
                    {
                        break;
                    }
                }
                temp.Next = newNode;
            }
            
        }
        public void Insert(T item, int index)
        {
            Node<T> newNode = new Node<T>(item);
            if(index==0)
            {
                newNode.Next = head;
            }
            else
            {
                Node<T> temp = head;
                for(int i=1;i<index-1;i++)
                {
                    temp = temp.Next;
                }
                Node<T> preNode = temp;
                Node<T> currentNode = temp;
                preNode.Next = newNode;
                newNode.Next = currentNode;
            }
        }
        public T Delete(int index)
        {
            T data = default(T);
            if(index == 0)
            {
                data = head.Data;
                head = head.Next;
            }
            else
            {
                Node<T> temp = head;
                for (int i = 1; i < index - 1; i++)
                {
                    temp = temp.Next;
                }
                Node<T> preNode = temp;
                Node<T> currentNode = temp.Next;
                data = currentNode.Data;
                Node<T> nextNode = temp.Next.Next;
                preNode.Next = nextNode;
                
            }
            return data;
        }
        public int GetLength()
        {
            if (head == null)
                return 0;
            Node<T> temp = head;
            int count = 1;
            while(true)
            {
                if(temp.Next!=null)
                {
                    count++;
                    temp = temp.Next;
                }
                else
                {
                    break;
                }
            }
            return count;
        }
        public void Clear()
        {
            head = null;
        }
        public bool IsEmpty()
        {
            return head == null;
        }
        public T this[int index]
        {
            get
            {
                Node<T> temp = head;
                for(int i=1;i<=index-1;i++)
                {
                    temp = temp.Next;
                }
                return temp.Data;
            }
        }
        public T GetEle(int index)
        {
            return this[index];
        }
        public int Locate(T value)
        {
            Node<T> temp = head;
            if(temp==null)
            {
                return -1;
            }
            else
            {
                int index = 0;
                while(true)
                {
                    if(temp.Data.Equals(value))
                    {
                        return index;
                    }
                    else
                    {
                        if(temp.Next!=null)
                        {
                            temp = temp.Next;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                return -1;
            }
        }
    }

C#算法與數據結構之線性結構