Java simulation of one-way and two-way linked lists

Source: Internet
Author: User

The implementation of the one-way linked list simulated by Java below

Package COM. jadyer. sort; /*** simulate a one-way linked list * @ author macro Yu * @ editor Jan 23,201 2 7:55:21 pm * @ see ==================== ========================================================== ========================================================== * @ see [Data Structure classification: linear data structure and non-linear data structure] * @ see 1) linear data structure, including: Linear table, stack, queue, String, array, file * @ see 2) nonlinear data structure, including: tree and graph * @ see ====================================================== ========================================================== =======================================* @ see [linear Table Overview: Its data elements are linearly related.] * @ see 1) all data elements in a linear table must be of the same data type in the same linear table * @ see 2) A linear table must have a unique data element called "first" and a unique data element called "last" * @ see 3) except the first element, each element has only one precursor element. Except the last element, each element has only one successor element * @ see 4). The logical structure of a linear table is a finite sequence of n data elements (A1, A2, a3 ,..., an) where N is the length of the linear table (n> = 0 ), A table with n = 0 is called an empty table * @ see ==================================== ========================================================== =========================================== * @ see [linear table classification: according to its storage structure, it can be divided into sequential tables and linked lists. * @ see 1) ordered tables: Linear tables stored in sequential storage structure are called ordered tables. That is, the elements in the memory address are stored sequentially * @ see. You can also say that the data elements in the linear table are stored in a certain storage area in sequence, the resulting table is called a sequence table * @ see one-dimensional array, which is a linear table stored in sequence. Therefore, arraylist can be considered as a sequence table * @ see 2) linked list: A linear table stored in a chained storage structure is called a linked list. That is, the elements in the memory address are not consecutively stored * @ see ==================================== ========================================================== ================================= * @ see [Stack] * @ see Stack (stack) it is also a special linear table. It is a linear table that only needs to be inserted and deleted at the end of the table. * @ see the physical storage of the stack can be structured in sequence, you can also use the chain Storage Structure * @ see Stack is a kind of post-first-in-first-out (LIFO) structure. The end of the stack table is called the top stack ), the header of the stack is called the bottom of the stack) * @ see ============================================== ========================================================== =================================* @ see [queue] * @ see Queue (Queue) yes. All inserts can only be performed at one end of the table, and all deletions are linear tables at the other end of the table. * @ see physical storage of the queue can be structured in sequence, you can also use the chain Storage Structure * @ see queue is a first-in-first-out (FIFO) structure, where the end that can be inserted is called the rear ), the end that is allowed to be deleted is called the front (bus like a bit) * @ see ============================================== ========================================================== ===================================*/class nodeoneway {string data; // store the node data itself nodeoneway next; // store the reference public nodeoneway () {} public nodeoneway (string data) {This. data = data ;}} /*** one-way linked list test class * @ author macro Yu * @ editor Jan 23,201 2 7:56:51 pm */public class nodeonewaytest {public static void main (string [] ARGs) {nodeoneway node11 = new nodeoneway ("node11_data"); nodeoneway node22 = new nodeoneway ("node22_data"); nodeoneway node33 = new nodeoneway ("node33_data"); node11.next = node22; // generate the Successor Relationship node22.next = node33; system. out. println (node11.next. next. data); // get the data attribute value of node33 through node11/*** generate the node44 object, insert it into the middle of node11 and node22 */nodeoneway node44 = new nodeoneway ("node44_data"); node11.next = node44; // modify the Successor Relationship of node11 to node44node44. next = node22; // modify the Successor Relationship of node44 to node22system. out. println (node11.next. next. next. data); // obtain the data attribute value system of node33 through node11. out. println (node11.next. next. data); // obtain the data attribute value of node22 through node11/*** Delete node44 object */node11.next = node22; // that is, the Successor Relationship of node11 points to node22, the Successor Relationship of node44 no longer points to node22node44. next = NULL; system. out. println (node11.next. next. data); // obtain the data attribute value of node33 through node11 }}

The following shows how to simulate a two-way linked list in Java.

Package COM. jadyer. sort;/*** simulate a two-way circular linked list * @ author macro Yu * @ editor Jan 23,201 2 8:16:34 * @ see Java. util. the underlying layer of the arraylist class is implemented using arrays * @ see Java. util. the underlying layer of the sort list class is the implementation of the * @ see bidirectional linked list. Each object in the two-way linked list has two references besides the data itself, it refers to the previous element and the last element * @ see. Therefore, when the Add/Remove operation is performed, the sorted list has better performance, while when the get operation is performed, arraylist has better performance */class nodetwoway {nodetwoway previous; // stores the string data referenced by the previous node; // stores the node data itself nodetwoway next; // store the reference public nodetwoway () {} public nodetwoway (string data) {This. data = data ;}} /*** test class of Bidirectional cyclic linked list * @ author macro Yu * @ editor Jan 23,201 2 8:21:33 */public class nodetwowaytest {public static void main (string [] ARGs) {nodetwoway node11 = new nodetwoway ("node11_data"); nodetwoway node22 = new nodetwoway ("node22_data"); nodetwoway node33 = new nodetwoway ("node33_data"); region = node33; // generate a precursor and a Successor Relationship node11.next = node22; node22.previous = node11; node22.next = node33; node33.previous = node22; node33.next = node11;/*** generate the node44 object, insert it into the middle of node11 and node22 */nodetwoway node44 = new nodetwoway ("node44_data"); node44.previous = node11; node44.next = node22; node11.next = node44; node22.previous = node44; /*** Delete the node44 object */node44.previous = NULL; node44.next = NULL; node11.next = node22; node22.previous = node11 ;}}

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.