C#单链表

关于单链表的知识,请查阅原文,这里就不再赘述。
原文中有一处错误已经指出:获取链表中间值时,如果链表长度为偶数,原文获取的值是正确值的后一组值。
修正思路:在给A结点赋值时,使用临时结点记录结点A的值。最后输出临时结点的值与A结点的值。

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

namespace 链表
{
    class Program
    {
        static void Main(string[] args)
        {
            LinkList<string> link = new LinkList<string>();
            link.Append("A");
            link.Append("B");
            link.Append("C");
            link.Append("D");
            link.Append("E");
            link.Insert(1,"Head");
            Console.WriteLine("单链表内容:");
            link.Display();
            link.Delete(5);
            Console.WriteLine("已完成删除单链表中第5行记录数");
            link.Display();
            Console.WriteLine("查询单链表中第1:{0}.第3:{1}", link.GetNodeData(1), link.GetNodeData(3));
            Console.WriteLine("面试题-->单链表反转");
            link.Reverse();
            Console.WriteLine("面试题-->获得单链表中间值");
            link.GetMiddleValue();
            Console.ReadKey();
        }
    }

    public class Node<T>
    {
        /// <summary>
        /// 属性
        /// </summary>
        public T Data { get; set; }
        public Node<T> Next { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public Node(T value)
        {
            Data = value;
            Next = null;
        }
        public Node()
        {
            Data = default(T);
            Next = null;
        }
    }

    public class LinkList<T>
    {
        /// <summary>
        /// 单链表头
        /// </summary>
        public Node<T> Head { get; set; }
        /// <summary>
        /// 构造
        /// </summary>
        public LinkList()
        {
            Clear();
        }
        /// <summary>
        /// 求单链表长度
        /// </summary>
        /// <returns></returns>
        public int GetLength()
        {
            Node<T> A = Head;
            int length = 0;
            while (A.Next!=null)
            {
                A = A.Next;
                length++;
            }
            return length;
        }
        /// <summary>
        /// 判断单链表是否为空
        /// </summary>
        /// <returns></returns>
        public bool isEmpty()
        {
            return Head == null;
        }
        /// <summary>
        /// 清空单链表
        /// </summary>
        public void Clear()
        {
            Head = null;
        }
        /// <summary>
        /// 获得当前位置单链表中结点的值
        /// </summary>
        /// <param name="i">结点位置</param>
        /// <returns></returns>
        public T GetNodeData(int i)
        {
            if (isEmpty())
            {
                Console.WriteLine("单链表为空");
                return default(T);
            }
            if (i<1||i>GetLength())
            {
                Console.WriteLine("结点位置有误");
                return default(T);
            }
            Node<T> A = Head;
            int index = 1;
            while (index<i&&A.Next!=null)
            {
                A = A.Next;
                index++;
            }
            return A.Data;
        }
        /// <summary>
        /// 增加元素到单链表末尾
        /// </summary>
        /// <param name="item"></param>
        public void Append(T item)
        {
            Node<T> foot = new Node<T>(item);
            Node<T> A = new Node<T>();
            if (Head==null)
            {
                Head = foot;
                return;
            }
            A = Head;
            while (A.Next!=null)
            {
                A = A.Next;
            }
            A.Next = foot;
        }
        public void Insert(int i,T item)
        {
            if (isEmpty())
            {
                Console.WriteLine("单链表为空");
            }
            if (i < 0 || i > GetLength())
            {
                Console.WriteLine("结点位置有误");
            }
            //增加到头部
            if (i == 1)
            {
                Node<T> H = new Node<T>(item);
                H.Next = Head;
                Head = H;
                return;
            }
            Node<T> A = new Node<T>();
            Node<T> B = new Node<T>();
            int j = 1;
            B = Head;
            while (j < i && B.Next != null)
            {
                A = B;
                B = B.Next;
                j++;
            }
            if (j==i)
            {
                Node<T> C = new Node<T>(item);
                A.Next = C;
                C.Next = B;
            }
        }
        /// <summary>
        /// 删除第i个结点
        /// </summary>
        /// <param name="i"></param>
        public void Delete(int i)
        {
            Node<T> A = new Node<T>();
            if (1==i)
            {
                A = Head;
                Head = Head.Next;
                return;
            }
            Node<T> B = new Node<T>();
            B = Head;
            int j = 1;
            while (B.Next!=null&&j<i)
            {
                A = B;
                B = B.Next;
                j++;
            }
            if (j==i)
            {
                A.Next = B.Next;
            }
        }
        /// <summary>
        /// 显示单链表
        /// </summary>
        public void Display()
        {
            if (isEmpty())
            {
                Console.WriteLine("单链表为空");
                return;
            }
            Node<T> H = Head;
            while (H!=null)
            {
                Console.WriteLine(H.Data);
                H = H.Next;
            }
        }

        #region 面试题
        /// <summary>
        /// 反转链表
        /// </summary>
        public void Reverse()
        {
            if (Head==null||GetLength()==1)
            {
                return;
            }

            Node<T> NewNode = null;
            Node<T> CurrentNode = Head;
            Node<T> TempNode = new Node<T>();
            while (CurrentNode!=null)
            {
                TempNode = CurrentNode.Next;
                CurrentNode.Next = NewNode;
                NewNode = CurrentNode;
                CurrentNode = TempNode;
            }
            Head = NewNode;
            Display();
        }

        public void GetMiddleValue()
        {
            Node<T> A = Head;
            Node<T> B = Head;
            Node<T> temp=new Node<T>(); //链表长度为偶数时用到
            while (B!=null&&B.Next!=null)
            {
                temp = A;
                A = A.Next;
                B = B.Next.Next;
            }
            if (B != null)    //奇数
            {
                Console.WriteLine("奇数,中间值为{0}", A.Data);
            }
            else    //偶数
            {
                Console.WriteLine("偶数,中间值为{0}和{1}", temp.Data,A.Data);
            }
        }
        #endregion
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值