Package Linklist;public class Node<t> {public T data;//data domain public node next;//node domain//default constructor method public node () {}//with parameter construction method, Non-head node initialization public node (T Data,node next) {This.data=data;this.next=next;} Header node initialization public node {this.next=next;} Show node value public void display () {System.out.print (data+ "");}}
Gorgeous Split-line ********************************************************* ***************
Package linklist;/** * **************** single-linked list with lead node and its implementation ********************* * * @author WL * */public class SINGLYLINKLIST&L T t> {node<?> head;//head node int size;//list size node<?> current;//current node//Initialize an empty list public singlylinklist () { This.head=new node<object> (null); this.size=0;this.current=head;} Determines whether the linked list is empty public boolean isEmpty () {return size==0;} Print List public void traverse () {if (IsEmpty ()) {System.out.println ("null");} Else{for (node<?> p=head.next;p!=null;p=p.next) {System.out.print (p.data+ ",");} System.out.println ();}} Add node from the node public void Addfromhead (T value) {node<t> node=new node<t> (value,null); node.next=head.next; head.next=node;size++;} Add node from tail node public void Addfromtail (T value) {node<t> node=new node<t> (value,null); this.current=head.next; if (current==null) {head.next=node;size++;} Else{while (current.next!=null) {//The current node is anchored to the tail node Current=current.next;} current.next=node;size++;}} Delete public void Removefromhead () {if (IsEmpty ()) {///To determine if the list is empty throw new RuntimeexceptioN ("The linked list is empty");} current=head.next;//current node head.next=current.next;current.next=null;size--;} Remove public void Removefromtail () {if (IsEmpty ()) {///To determine if the list is empty throw new RuntimeException ("Empty list") from the tail node;} Node<?> prev=null;//the previous node This.current=head.next;while (current.next!=null) {//The current node is positioned to the tail node prev=current; Current=current.next;} prev.next=null;size--;} Insert a node after index public void insert (int index,t value) {if (index<0| | Index>size) {throw new RuntimeException ("parameter index is incorrect");} Node<t> node=new node<t> (value,null); if (index==0) {node.next=head.next;head.next=node;size++;} Else{int count=0;//counter node<?> prev=null;//Previous node Current=head.next;while (current!=null&&count!=index {//To position the current node at index node prev=current;current=current.next;count++;} node.next=current;prev.next=node;size++;}} Delete a node of the index position at any location public void remove (int index) {if (index<0| | Index>size) {throw new RuntimeException ("parameter index is incorrect");} if (index==0) {current=head.next;head.next=current.next;size--;} Else{int count=0;//Counter node<?> prev=null;//the previous node Current=head.next;while (current!=null&&count!=index) {//positions the current node at index node prev=current ; current=current.next;count++;} prev.next=current.next;current=null;size--;}} When you delete a node based on its value, when there are multiple identical value values, only the first public void Removebyvalue (T value) {if (IsEmpty ())} {throw new RuntimeException (" The linked list is empty ");} int count=0;//counter node<?> prev=null;//Previous node Current=head.next;while (current!=null&¤t.data!=value) {// Positions the current node at the first node with a value of prev=current;current=current.next;count++;} if (count>size) {throw new RuntimeException ("The node with value is not present");} prev.next=current.next;current=null;size--;}}
Java data structure System--linked list (1): Single linked list and related common operations