Single-chain table

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; namespace singly_linked_list {class program {static void main (string [] ARGs) {singly1_list lst = new singly1_list (); 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 singly+list {// number of elements Private int count; // The end pointer private node next; private node temp; // Add the public void add (object Value) Element) {node newnode = new node (value); If (this. next = NULL) // The linked list is empty. This. next = newnode; else {// The linked list is not empty. Add the element to the end of the linked list if (temp = NULL) {This. next. next = newnode; temp = newnode;} else {temp. next = newnode; temp = newnode;} // You can also use the following method // If (this. next = NULL) // The linked list is empty // 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;} // chain table length public int count {get {return count ;}}// Delete 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;} // stores public object item; Public singly1_list. node next; Public override string tostring () {return item. tostring ();}}}}

 

Running result:

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.