Learn the list of basic knowledge of Java data structures

Source: Internet
Author: User

public class Link {public int idata;public double ddata;public link next;public link (int iData, double dData) {super (); thi S.idata = Idata;this.ddata = DData;} public void DisplayLink () {System.out.print ("{" +idata+ "," +ddata + "}");}}


public class Linklist {private Link first;public void linklist () {First =null;} Whether the linked list is empty public boolean isEmpty () {return (first ==null);} Insert first header element public void Insertfirst (int id,double dd) {link NewLink =new link (ID, dd); Newlink.next =first;first =newlink; }//Delete the list header element public link deletefirst () {link templink =first;first =first.next;return templink;} Find a specific node public Link find (int key) {link current =first;while (current.idata!=key) {if (Current.next ==null) {return null;} else {current =current.next;}} return current;} Delete node public Link Delete (int key) {link Currentlink =first; Link previous=first;while (Currentlink.idata!=key) {if (Currentlink.next ==null) {return null;} else {previous =currentlink;currentlink =currentlink.next;}} if (Currentlink ==first) {first =first.next;} Else{previous.next =currentlink.next;} return currentlink;} Insert a specific node public Boolean insertlink (int key, int iData, double dData) {int i=0; Link Currentlink =first; Link previous =first; Link NextLink; Link newLink =new link (iData, dDaTA);//Insert Header element if ((I==key) && (currentlink!=null)) {Newlink.next =currentlink;first=newlink;return true;} else if (currentlink!=null) {//i++;while (i!= (key-1)) {if (Currentlink.next ==null) {Currentlink.next=newlink;return true;} else {previous =currentlink;currentlink =currentlink.next;i++;}} if (Currentlink.next!=null) {NextLink =currentlink.next;currentlink.next=newlink;newlink.next=nextlink;return True ;} else {Currentlink.next =newlink;newlink.next =null;return true;}} else if (Currentlink ==null) {Currentlink =newlink;first=newlink;first.next=null;return true;} return false;} Show linked list all data public void Displaylist () {System.out.println ("linklist.displaylist () List (first-->last):"); Link currentlink =first;while (currentlink!=null) {currentlink.displaylink (); Currentlink =currentlink.next;} System.out.println (""); System.out.print ("Linklist.displaylist () Finish");}}

public class Linklistapp {public static void main (string[] args) {linklist thelinklist =new linklist (); Thelinklist.insertlink (9, 999, 999.999); Thelinklist.displaylist (); Thelinklist.insertfirst (22, 22.22); Thelinklist.insertfirst (33.33), Thelinklist.insertfirst (44.44), Thelinklist.insertfirst (55, 55.55); Thelinklist.displaylist (); Thelinklist.insertlink (2, 333, 333.333); Thelinklist.displaylist (); Thelinklist.insertlink (7, 777, 777.777); Thelinklist.displaylist (); System.out.println (""); while (!thelinklist.isempty ()) {Link aLink =thelinklist.deletefirst (); System.out.print ("Deleted"); Alink.displaylink (); System.out.println ("");}    Thelinklist.displaylist ();}}


Learn the list of basic knowledge 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.