首页 > 代码库 > 单链表

单链表

相对于数组来说:
优点: 通过索引(数组下标)快地访问数组元素;
缺点: 插入/删除元素需要对数组进行调整, 效率低;

而链表:
优点:插入/删除速度快而且用对整链表进行调整;
缺点:只能进行顺序访问能随机访问(像数组样用下标);

所链表些需要快速插入/删除而太关心或者需要随机访问情况下使用.

using System;namespace Singly_Linked_List{    class Program    {        static void Main(string[] args)        {            SinglyLinkedList lst = new SinglyLinkedList();             lst.Add(1);             lst.Add(2);             lst.Add(3);             lst.Add(4);             lst.Add(5);             lst.Add(6);            lst.DeleteNext();            lst.DeleteNext();            lst.DeleteNext();            Console.WriteLine( lst.ToString());            Console.Read();        }    }    class SinglyLinkedList    {        //元素个数        private int count;        //尾指针        private Node next;        private Node Temp;        //增加元素        public void Add(object value)        {            Node newNode = new Node(value);            if (this.next == null)                //链表为空                this.next = newNode;            else            {//链表不为空,把元素增加到链表结尾                if (Temp == null)                {                    this.next.next = newNode;                    Temp = newNode;                }                else                {                    Temp.next = newNode;                    Temp = newNode;                }            }            //也可以用下面这种方法            //if (this.next == null)            //    //链表为空            //    this.next = newNode;            //else            //{             //if (this.next.next == null)            //    this.next.next = newNode;            //else {            //    Node n1 = this.next.next;            //    for (int i = 1; i < count; i++)            //    {            //        if (n1.next == null)            //            n1.next = newNode;            //        else            //            n1 = n1.next;            //    }            //}            //}                count++;                            }                  public override string ToString()        {            if (this.next == null)            {                return string.Empty;            }            string s = "";            Node temp = this.next;            for (int i = 0; i < count; i++)            {                s += temp.ToString() + "    ";                temp = temp.next;            }            return s;        }        //链表长度        public int Count        {            get { return count; }        }       // 删除        public int DeleteNext()        {            if (  this.next == null)                return 0;            if (this.next.next == null)                this.next = null;            else            this.next = this.next.next;            count--;            return 1;        }        private class Node        {            public Node(object value)            {                item = value;            }            //存放数据            public object item;            public SinglyLinkedList.Node next;            public override string ToString()            {                return item.ToString();            }        }    }}

 

运行结果: