Compared with Arrays:
Advantage: quick access to array elements through indexes (array subscript;
Disadvantage: the array needs to be adjusted to insert/delete elements, which is inefficient;
Linked List:
Advantage: Fast insertion/deletion speed and adjustment of the entire linked list;
Disadvantage: Only sequential access is allowed and random access is allowed (subscript is used for Array samples );
The linked list needs to be quickly inserted/deleted, and used when it is too concerned or requires random access.
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 n Ode {// node value 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) // The passed value is greater than the current value {newnode. next = This; // put the passed value before the current value. If (this. prev! = NULL) // the header is not null {This. prev. next = newnode; newnode. prev = This. prev;} This. prev = newnode; // set the previous node of the current node to return newnode as the new node; // The returned else {If (this. next! = NULL) {// recursion this. next. add (newnode);} else {This. next = newnode; // set the new node as the next node newnode of the current node. 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(); } }}