Realization of C # one-way linked list

Source: Internet
Author: User

Using System; The public class LinkedList {//nested class represents a single node; Private class Node {public node (object values) {item=values;} public Object it Em Data fields, public linkedlist.node next;//pointer fields, public override string ToString () {return item. ToString (); A private int count;//the number of record elements; public int count {get {return this.count;}} private Node head;//header pointer; public object [int index]//indexer; {get {return Getbyindex (index). Item;} set{getbyindex (Index). Item=value}}//① add element; public void Add ( Object values) {node newnode=new node (values); if (head ==null)//If the header pointer is empty; {Heads =newnode} else {Getbyindex (count-1 ). Next=newnode; Plug into the end of the list;} count + +; List length +1;//② inserts an element at the specified index; public void Insert (int index,object values) {Node tempnode; if (index ==0) {if (head ==null) { Tempnode =new Node (values); Tempnode.next =head; Head =tempnode; } else {node prenode=getbyindex (index-1);//Find the predecessor of the insertion node, node Nextnode=prenode.next;//Find the successor node of the insertion point; Tempnode =new node (VA Lues); Prenode.next =tempnode; TempnodE.next =nextnode; Count + +; //③ deletes the specified index element; the public void RemoveAt (int index) {if (index ==0)//Delete node is the head pointer; {heads =head.next;} else {node Prenode=getb Yindex (index-1); if (Prenode.next ==null) {throw new ArgumentOutOfRangeException ("index"), the index is out of range. "); } Prenode.next =prenode.next.next; Count--; public override string ToString () {string s= ' "; for (Node temp=head; temp!=null; temp=temp.next) {s+=temp. ToString () + "";} return s; Private Node getbyindex (int index) {if ((index <0) | | (index >= This.count)) {throw new ArgumentOutOfRangeException (' index ', ' index is out of range. "); } Node Tempnode=this.head; for (int i=0;i<index; i++) {tempnode=tempnode.next;} return tempnode; } class App {static void Main () {LinkedList lst=new linkedlist (); Console. WriteLine ("① add element:"); Lst. ADD (0); Lst. ADD (1); Lst. ADD (2); Lst. ADD (3); Console. WriteLine (LST. ToString ()); Console. WriteLine ("② in position 2nd, add Element 50:"); Lst. Insert (2,50); Console. WriteLine (LST. ToString ()); Console. WriteLine ("③ Remove number 1thelement: "); Lst. RemoveAt (1); Console. WriteLine (LST. ToString ()); Console. WriteLine ("④ 2nd number of elements assigned to 9:"); LST [2]=9; Console. WriteLine (LST. ToString ()); } }

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.