标签:style class blog code http ext
相对于数组来说:
优点: 通过索引(数组下标)快地访问数组元素;
缺点: 插入/删除元素需要对数组进行调整, 效率低;
而链表:
优点:插入/删除速度快而且用对整链表进行调整;
缺点:只能进行顺序访问能随机访问(像数组样用下标);
所链表些需要快速插入/删除而太关心或者需要随机访问情况下使用.
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication4 { class DoubleLinkedList { private Node head; public void Add(int data) { if (head==null) { head = new Node(data); } else { head=head.Add(new Node(data)); } } public override string ToString() { if (head==null) { return string.Empty; } else { return this.head.ToString(); } } public class Node { //节点的值 private int data; private Node next; private Node prev; public Node(int data) { this.data = data; } public int Data { get { return this.data; } } public Node Add(Node newNode) { if (data > newNode.data) //传进来的值大于当前值 { newNode.next = this;//把传进来的值放到当前值前面去 if (this.prev!=null)//前面不为空 { this.prev.next = newNode; newNode.prev = this.prev; } this.prev = newNode; //设置当前节点的前一个节点为新节点 return newNode;//只有新节点为头节点时才返回 } else { if (this.next!=null) {//递归 this.next.Add(newNode); } else { this.next = newNode;//设置新节点为当前节点的下一节点 newNode.prev = this; } return this; } } public override string ToString() { string str = data.ToString(); if (next!=null) { str += " " + next.ToString(); } return str; } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication4 { class Program { static void Main(string[] args) { DoubleLinkedList lst = new DoubleLinkedList(); lst.Add(1); lst.Add(3); lst.Add(9); lst.Add(2); lst.Add(6); lst.Add(5); lst.Add(8); Console.WriteLine( (lst.ToString())); Console.ReadLine(); } } }
标签:style class blog code http ext
原文地址:http://www.cnblogs.com/liek/p/3807009.html