Java uses linked lists to implement linear tables

Source: Internet
Author: User
 
 
Package COM. jzm; public class myllist <t> {private node firstnode; private int length; // linear table length private class node {// Private internal class private t data; private node next; private node (T dataportion) {DATA = dataportion; next = NULL;} // end constructor/* Private node (T dataportion, node nextnode) {DATA = dataportion; next = nextnode;} // end constructor */} // end-node public myllist () {clear ();} public boolea N isempty () {return length <1? True: false;} public final void clear () {firstnode = NULL; length = 0;} private node getnodeat (INT givenposition) {assert (! Isempty () & (1 <= givenposition) & (givenposition <= length); node currentnode = firstnode; For (INT I = 1; I <givenposition; I ++) {currentnode = currentnode. next;} assert currentnode! = NULL; return currentnode;} public Boolean add (T newentry) {// Add the Element Node newnode = new node (newentry) at the end; If (isempty ()) {firstnode = newnode;} else {node lastnode = getnodeat (length); lastnode. next = newnode; // point the last node reference to the new node} // end-If length ++; return true;} // end add public Boolean add (INT newposition, t newentry) {If (newposition> = 1) & (newposition <= Length + 1) {node newnode = new node (newentry ); If (isempty () | newposition = 1) {// scenario 1 newnode. next = firstnode; firstnode = newnode;} else {node nodebefore = getnodeat (newPosition-1); node nodeafter = nodebefore. next; newnode. next = nodeafter; nodebefore. next = newnode;} // end else length ++; return true;} else return false;} // end add (,) Public t getentry (INT givenposition) {T result = NULL; If (! Isempty () & (givenposition> = 1) & givenposition <= length) {result = getnodeat (givenposition ). data;} return result;} public object remove (INT givenposition) {object result = NULL; If (givenposition <1 | givenposition> length | isempty () {system. out. println ("the deleted location does not exist or the linked list is empty"); return result;} else if (givenposition! = 1) {node deletenode = getnodeat (givenposition); node nodebefore = getnodeat (givenPosition-1); nodebefore. next = deletenode. next; Length --; return deletenode;} else {result = firstnode. data; firstnode = firstnode. next; Length --; return result ;}} public void display () {If (length <1) {system. out. println ("no data in the linked list! ");} Else {node currentnode = firstnode; For (INT I = 1; I <= length; I ++) {system. out. println ("Number" + I + "data:" + currentnode. data); currentnode = currentnode. next; }}// for} // end display}

 

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.