/** * Single-link list operation * Create by Administrator * 2018/6/14 0014 * pm 2:05 **/public class Link {public int iData; public double dData; Public Link Next; public Link (int ID, double dd) {this.idata = ID; This.ddata = DD; } public void DisplayLink () {System.out.print ("{" + IData + "," + DData + "}"); }}class linklist {private Link first; Public linklist () {this.first = null; public Boolean isEmpty () {//To determine if the list is empty return (first = = null); Public link find (int key) {//Find the specified object Link current = first; Get the first dot while (current.idata! = key) {//Weight first start traversing if (Current.next = = null) {return null ; Results not found}else {current = Current.next;//Get Next}} return current; Find results} public link Delete (int key) {//Delete specified object link current = first; Link previous = first; while (current.idata! = key) {if (Current.next = = null) {return null; }else {previous = current; current = Current.next; }} if (current = = first) {first = First.next; }else {previous.next = Current.next; } return current; } public void Insertfirst (int id,double dd) {link link = new Link (ID, DD); Link.next = First; The reference to the storage object first = link; Assign the current object to the first} public link Deletefrst () {//delete the top list, exactly and add the opposite if (!isempty ()) {Link TEMP = Firs T first = First.next; return temp; } return null; } public void Displaylist () {System.out.print ("List (first-->last):"); Link current = first; while (current = null) {Current.displaylink (); current = Current.next; } System.out.println (""); } public static void Main (string[] args) {linklist thelist = new linklist (); Thelist.insertfirst (22,2.99); Thelist.insertfirst (44,4.99); Thelist.insertfirst (66,6.99); Thelist.insertfirst (88,8.99);
Thelist.displaylist ();//While (!thelist.isempty ()) {// Link ALink = Thelist.deletefrst ();/ / System.out.print ("Deleted");// Alink.displaylink ();// System.out.println ("");// } Link f = Thelist.find (a); if (f! = null) { System.out.print ("Find Link:"); F.displaylink (); System.out.println (""); Link result = Thelist.delete (f.idata); if (result! = null) { System.out.print ("delete Success"); Result.displaylink (); } } System.out.println (""); Thelist.displaylist (); }}
Java Learning-linked list (1)