Single linked list of Java data structures

Source: Internet
Author: User

List of Links: Linked list header + node

The chain header typically stores only the reference of the next node

?? Node: Save Data + reference to next node

    1. List Header code:

Package?com.xingej.algorithm.datastructure.linkedlist.singlelinkedlist;/**?*? Declare the head of a linked list??? * From the use of the linked list head, Lenovo to Hadoop,spark,netty in the context of the context?*?? *[email protected]?erjun?2017 December 8? Morning 8:45:08?*/public?class? Linklist? {???? Private? Node?first;???? Public? Linklist ()? {???????? When you start a linked list, the pointer reference is empty???????? First?=?null,????}???? Public?void?insertfirst (int?idata,?double?ddata)? {???????? ? Create a new chain node???????? Node?newlink?=?new? Node (idata,?ddata);???????? Update the newly created chain node next reference, which is to assign a header reference to it???????? Newlink.next?=?first;???????? Update the reference to the list header, which is the reference to the newly created chain node, assigned to the header reference???????? First?=?newlink,????}???? Public?boolean?isempty ()? {???????? Return?null?==?first,????}???? Public? Node?deletefirst ()? {???????? ? The first element to be deleted???????? Node?temp?=?first;???????? A linked list, in this data structure, removes the element and only needs to update the pointer reference to???????? Update the reference to the header element???????? First?=?first.next;???????? Return?temp,????}???? Public?void?displaylist ()? {???????? System.out.println ("List?" ( First--->last):???????? Node?current?=?first;???????? While? (null?! =?current)? {???????????? Current.dispLaylink ();???????????? ? Gets the reference to the next element, in C, called the pointer???????????? Current?=?current.next,????????}???????? System.out.println ();????}}
    1. Node Code:

Package?com.xingej.algorithm.datastructure.linkedlist.singlelinkedlist;/**?*? Declare a node? *?? *[email protected]?erjun?2017 December 8? Morning 8:41:50?*/public?class? Node? {???? Private?int?idata;???? Private?double?ddata;???? ? Oh, otherwise the object cannot access the???? Public? Node?next;???? Public? Node (int?idata,?double?ddata)? {???????? This.ddata?=?ddata;???????? This.idata?=?idata,????}???? Public?void?displaylink ()? {???????? System.out.println ("{"? +?idata?+? ",?"?) +?ddata?+ "}");????}}



Simple test Cases:

Package?com.xingej.algorithm.datastructure.linkedlist.singlelinkedlist;import?org.junit.test;/**?*? single-linked list test? *[email protected]?erjun?2017 December 8? Morning 9:00:54?*/public?class? Linklisttest? {[email protected]???? Public?void?test ()? {???????? Linklist?list?=?new? Linklist ();???????? ? It is clear that each time the first element of the linked list is inserted???????? List.insertfirst (32,?8);???????? List.insertfirst (3,?3);???????? List.insertfirst (2,?6);???????? List.insertfirst (42,?8);???????? List.insertfirst (26,?9);???????? List.displaylist ();????}}













Single linked list of Java data structures

Related Article

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.