To create a linked list in a more concise and self-creating way
1 classListNode {2 //for convenience, both of these variables use Pub1ic,3 //the variable that holds the data, directly to the int type4 Public intdata;5 //The variable that holds the node, which defaults to Nu11 .6 PublicListNode Next;7 //constructs a value that can be assigned to data at construction time8 PublicListNode () {9 //give an empty parameter construction methodTen } One PublicListNode (intdata) { A This. data =data; - } - Public intGetData () { the return This. Data; - } - //Adding nodes - Public voidAddintNewData) { +ListNode NewNode =NewListNode (newdata); - if( This. next==NULL){ + This. Next =NewNode; A}Else{ at This. Next.add (NewData); - } - } - //Output - Public voidprint () { -System.out.print ( This. Data + "-"); in if( This. next!=NULL){ - This. Next.print (); to } +}
ListNode Java Create linked list