Java learning notes-custom implementation of collections list

Source: Internet
Author: User
/** Custom implementation of the javaslist collection structure */public class my‑list {private node firstnode; // always points to the first element private node lastnode; // always point to the last element private int size; // set length // Add public Boolean add (node) {If (null = node) {Throw new illegalargumentexception ("Node element with null not allowed");} If (null = firstnode) {firstnode = node; // No element exists in the set, assign this element to the first element} else {node lastnode = getlast (); // two-way linked list lastnode. setnext (node); // point to the next element Surnode. setprev (lastnode); // point to the previous element} lastnode = node; // assign the newly added element to the last element size ++; // Add 1 return true for each length added;} // Add the public void addfirst (node) {If (node = NULL) element to the header of the List) {Throw new illegalargumentexception ("Do Not Allow null node elements");} If (firstnode = NULL) {lastnode = node;} else {node n = getfirst (); N. setprev (node); node. setnext (n) ;}firstnode = node; size ++ ;}// Add the public void addlast (node) {If (node = NULL) element at the end of the list) {Throw n EW illegalargumentexception ("Do Not Allow null node elements");} If (lastnode = NULL) {firstnode = node;} else {node n = getlast (); N. setnext (node); node. setprev (n) ;}lastnode = node; size ++ ;}// Delete the first public void removefirst () {node = firstnode in the list. getnext (); node. setprev (null); firstnode = node; size --;} // Delete the last element in the list public void removelast () {node = lastnode. getprev (); node. setnext (null); lastnode = node; size -- ;}// Delete the specified public v element in the list Oid remove (INT index) {If (index <0 | index> = size) {Throw new arrayindexoutofboundsexception ();} If (null = firstnode) {Throw new illegalstateexception ("no element in set");} int I = 0; node = firstnode; If (Index = 0) {node = node. getnext (); node. setprev (null); firstnode = node; size --; return;} If (Index = (size-1) {node = lastnode. getprev (); node. setnext (null); lastnode = node; size --; return;} If (index <size/2) {While (I! = Index) {node = node. getnext (); I ++ ;}} else {I = size-1; node = lastnode; while (I! = Index) {node = node. getprev (); I -- ;}} node next = node. getnext (); node Prev = node. getprev (); next. setprev (prev); Prev. setnext (next); size --;}/** return the elements at the specified position in this list (bipartite) */Public node get (INT index) {If (index <0 | index> = size) {Throw new arrayindexoutofboundsexception ();} If (null = firstnode) {Throw new illegalstateexception ("no element in set");} int I = 0; node; If (index <size/2) {node = firstnode; while (I! = Index) {node = node. getnext (); I ++ ;}} else {I = size-1; node = lastnode; while (I! = Index) {node = node. getprev (); I --;} return node;}/** return the element (common method) at the specified position in this list */Public node myget (INT index) {If (index <0 | index> = size) {Throw new arrayindexoutofboundsexception ();} If (null = firstnode) {Throw new illegalstateexception ("no element in set");} int I = 0; node = firstnode; while (I! = Index) {node = node. getnext (); I ++;} return node;}/** get the last element */Public node getlast () {If (null = firstnode) {Throw new illegalstateexception ("no element in set");} return lastnode;}/** get the last element */Public node getfirst () {If (null = firstnode) {Throw new illegalstateexception ("no element in set");} return firstnode;}/** get set length */Public int size () {return size ;}}

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.