单向非循环列表的简单操作

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

namespace Console_test
{
    /// <summary>
    ///  单向非循环链表
    /// </summary>
    public class LinkList
    {
        /// <summary>
        /// 节点类 
        /// </summary>
        private class Node
        {
            /// <summary>
            /// 数据
            /// </summary>
            public int Data;
            /// <summary>
            /// 下一个节点
            /// </summary>
            public LinkList.Node Next;
            public Node() { }
            public Node(int data)
            {
                Data = data;
            }
        }

        /// <summary>
        /// 链表的长度
        /// </summary>
        public int Count
        {
            get
            {
                return GetLinkCount();
            }
        }
        /// <summary>
        /// 头节点
        /// </summary>
        private Node headNode = new Node();
        /// <summary>
        /// 为节点
        /// </summary>
        private Node tailNode;

        /// <summary>
        /// 给链表添加元素
        /// </summary>
        /// <param name="data"></param>
        public void add(int data)
        {
            Node newNode = new Node(data);
            if (tailNode == null)
            {
                tailNode = headNode;
            }
            tailNode.Next = newNode;
            tailNode = newNode;
        }

        /// <summary>
        /// 显示链表
        /// </summary>
        public void show()
        {
            Node n = headNode.Next;
            while (n != null)
            {
                Console.Write(n.Data + "\t");
                n = n.Next;
            }
            Console.WriteLine();
        }

        /// <summary>
        /// 得到链表的长度
        /// </summary>
        /// <returns></returns>
        private int GetLinkCount()
        {
            Node n = headNode.Next;
            int cut = 0;
            while (n != null)
            {
                cut++;
                n = n.Next;
            }
            return cut;
        }

        /// <summary>
        /// 给链表排序
        /// </summary>
        public void SortList()
        {
            int t;
            //for(循环变量;循环条件;一次循环完之后要做什么事情)
            for (Node i = headNode.Next; i != null; i = i.Next)
            {
                for (Node j = i.Next; j != null; j = j.Next)
                {
                    if (i.Data > j.Data)
                    {
                        t = i.Data;
                        i.Data = j.Data;
                        j.Data = t;
                    }
                }
            }
        }

        /// <summary>
        /// 在index位置添加数据data
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="index">索引:从0开始</param>
        /// <returns></returns>
        public bool InsertList(int data, int index)
        {
            //添加的时候应该先找到那个位置原来的前一个元素然后把它指向的地址赋给新添加元素的指向地址,然后把它的地址指向新添加的元素
            Node n = GetNode(index);
            if (n != null)
            {
                Node newNode = new Node(data);
                newNode.Next = n.Next;
                n.Next = newNode;
                return true;
            }
            return false;
        }

        /// <summary>
        /// 删除节点
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public bool DeleteList(int index)
        {
            try
            {
                Node n = GetNode(index);
                n.Next = n.Next.Next;
                if (n.Next == null)//说明n是最后一个节点
                {
                    tailNode = n;
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 对应位置的节点的前一个节点
        /// </summary>
        /// <param name="index">索引</param>
        /// <returns>对应位置的节点的前一个节点</returns>
        private Node GetNode(int index)
        {
            Node n = headNode;
            bool isOK = false;
            int _index = 0;
            while (n != null)
            {
                if (_index == index)
                {
                    isOK = true;
                    break;
                }
                _index++;
                n = n.Next;
            }
            if (isOK)
            {
                return n;
            }
            return null;
        }
    }
}

 Main函数调用:

 static void Main(string[] args)
        {
          
            LinkList link = new LinkList();
            Console.WriteLine("请输入链表的长度:");
            int len = Convert.ToInt32(Console.ReadLine());
            for (int i = 0; i < len; i++)
            {
                Console.WriteLine("请输入第{0}个数:", i + 1);
                link.add(Convert.ToInt32(Console.ReadLine()));
            }
            Console.WriteLine("链表为:");
            link.show();
            link.SortList();
            Console.WriteLine("排序后的链表为:");
            link.show();
            Console.WriteLine("请输入要插入链表的位置:");
            int index = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("请输入要插入链表的值:");
            int data = Convert.ToInt32(Console.ReadLine());
            if (!link.InsertList(data, index))
            {
                Console.WriteLine("插入元素失败");
            }
            Console.WriteLine("插入元素后的链表为:");
            link.show();
            Console.WriteLine("请输入要删除链表的位置:");
            index = Convert.ToInt32(Console.ReadLine());
            if (!link.DeleteList(index))
            {
                Console.WriteLine("删除元素失败");
            }
            Console.WriteLine("删除元素后的链表为:");
            link.show();

            Console.WriteLine("添加一个节点");
            link.add(Convert.ToInt32(Console.ReadLine()));
            link.show();

            Console.ReadKey();
            Console.ReadLine();

        }

 

posted @ 2013-01-30 11:23  下-个路口  阅读(189)  评论(0编辑  收藏  举报