c# 单链表实现代码

版权声明:本人菜鸟一只,如文章有错误或您有高见,请不吝赐教 https://blog.csdn.net/qq_41138935/article/details/82744826
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();

        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41138935/article/details/82744826
今日推荐