Two-way linked list

Source: Internet
Author: User

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();        }    }}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.