Java implementation of a simple one-way list

Source: Internet
Author: User

The implementation of a linked list one is node, and the other is list. node is a list of each of the basic components of the table Operation node. That's probably my idea.

Node Section code:

Class Node{private object Data;private node next;public node (object data) {this.data = data;} public void Setnext (Node next) {This.next = next;} public void SetData (Object data) {this.data = data;} Public Object GetData () {return this.data;} Public Node GetNext () {return this.next;}}

The list implements a series of operations on a list:

Class List{private node root;private node point;private int count;public void Add (node node) {if (root = null) {This.root = node;this.point = node;} Else{this.point.setnext (node); this.point = node;} This.count + +;} public int size () {return this.count;} public Boolean isEmpty () {return this.root = = null? True:false;} Public boolean contain (Object data) {return iscontain (this.root,data);} Public Object getData (int index) {if (index<0| | Index>=this.count) {return null;} Return Getdatabyindex (This.root,0,index);} public void SetData (Object data,int index) {if (index<0| | Index>=this.count) {return;} Setdatabyindex (this.root,0,index,data);} public void DeleteData (Object data) {if (This.contain (data)) {Deletenodebydata (this.root,data);} return;} public void Deletenode (int index) {if (index<0| | Index>=this.count) {return;} Deletenodebyindex (This.root,0,index);} Public object[] ToArray () {object[] array = new object[this.count];toarrayby (this.root,array,0); return array;} public void Clear () {this.root = null;This.count = 0; System.GC ();} public void Deletenodebydata (Node node,object data) {if (Data.equals (This.root.getData ())) {This.root = This.root.getNext (); this.count--;return;} if (Data.equals (Node.getnext (). GetData ())) {Node.setnext (Node.getnext (). GetNext ()); This.count--;return;} Deletenodebydata (Node.getnext (), data);} private void Toarrayby (node node,object[] Array,int point) {if (node = = null) {return;} Array[point] = Node.getdata (); Toarrayby (Node.getnext (), array,++point);} private void Deletenodebyindex (Node node,int point,int index) {if (index = = 0) {this.root = This.root.getNext (); This.count--;return;} if (point = = index-1) {Node.setnext (Node.getnext (). GetNext ()); This.count--;return;} Deletenodebyindex (Node.getnext (), ++point,index);} private void Setdatabyindex (Node node,int point,int index,object data) {if (point = = index) {node.setdata (data); return;} Setdatabyindex (Node.getnext (), ++point,index,data);} Private Object Getdatabyindex (Node node,int point,int index) {System.out.println ("point=" +point); System.out. println ("index=" +index), if (point = = index) {return node.getdata ();} Return Getdatabyindex (Node.getnext (), ++point,index); + + can not use behind Point}private Boolean iscontain (node Node,object data) {if (node = = null) {return false;} if (Data.equals (Node.getdata ())) {return true;} Return Iscontain (Node.getnext (), data);} private void Print (node node) {if (node = = null) {return;} System.out.println (Node.getdata ());p rint (Node.getnext ());}  public void printall () {print (this.root);}}

Test code:

public class Testdemo{public static void Main (string[] args) {List List = new List (); System.out.println (List.isEmpty ()); System.out.println (List.size ()), List.add (New node ("first")), List.add (New node ("second")), List.add ("third"); ")); object[] array = List.toarray (); for (int i = 0;i<array.length;i++) {System.out.println (i+" = "+array[i]);} System.out.println (List.size ()); System.out.println (List.isEmpty ()); List.printall (); System.out.println ("Iscontain First:" +list.contain ("first")); System.out.println ("Iscontain Other:" +list.contain ("other")); System.out.println ("Getdata1:" +list.getdata (1)) List.setdata ("None", 0); List.printall (); List.deletenode (0); List.printall (); List.deletenode (1); List.printall (); System.out.println (List.size ()); List.deletedata ("none"); List.deletedata ("third"); List.printall (); System.out.println (List.size ()); List.deletedata ("second");//list.deletedata ("third"); List.printall (); System.out.println (List.size ());//system.out.println ("Getdata3:" +list.getdata (3));}}

This is a simple implementation, for some of the implementation of some of the algorithms did not do a deep study. Have time to study Java implementation List of the source code.

Java implementation of a simple one-way list

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.