數據結構|-用C#實現一個簡單的鏈表


我們知道C#中是沒有鏈表的,我們可以自己實現一個

 

整個單鏈表能實現的功能有:

 

功能 方法 返回值 備注
獲取鏈表長度 GetLength() int 返回值是鏈表長度
清空鏈表 Clear() void  
判斷鏈表是否為空 IsEmpty() bool  
添加元素 Add(T item) void 在鏈表尾添加元素
在指定位置插入元素 Insert(T item,int index) void 插入的元素就是第index位
刪除指定位置元素 Delete(int index) T  
索引器 this[int index] T 類似數組的索引器
獲取指定位置的值 GetEle(int index) T 以位取值
獲取值所在的位置 Locate(T value) int 以值取位

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


首先是結點類Node.CS

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

namespace _001_線性表 {
    /// <summary>
    /// 單鏈表的結點
    /// </summary>
    /// <typeparam name="T"></typeparam>
    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; }
        } 
    }
}

然后是鏈表的具體功能的實現LinkList

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

namespace _001_線性表 {
    class LinkList<T>
    {
        private Node<T> head;//存儲一個頭結點

        public LinkList()
        {
            head = null;
        }


        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 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;
                head = newNode;
            }
            else
            {
                Node<T> temp = head;
                for (int i = 1; i <=index-1; i++)
                {
                    //讓temp向后移動一個位置
                    temp = temp.Next;
                }
                Node<T> preNode = temp;
                Node<T> currentNode = temp.Next;
                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 = 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 T this[int index]
        {
            get
            {
                Node<T> temp = head;
                for (int i = 1; i <= index; i++) {
                    //讓temp向后移動一個位置
                    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;
            }
        }
    }
}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM