Packagetest1;//create a linked list structure Public classSinglelist {//defines the head node pointer.Node head =NULL; //node data structure defining a linked list classnode{intVal; Node Next; PublicNode (intval) { This. val =Val; Next=NULL; } } //Add a node to the tail of the list Public voidAddintnum) {Node Node=NewNode (num); if(head==NULL) Head=node; Elsesearch (). Next=node; } //get the node at the tail of the list Publicnode Search () {node Nodelast=Head; while(nodelast.next!=NULL) {Nodelast=Nodelast.next; } returnNodelast; } //implement to find the data for a particular node in the list is num and return the node PublicNode Search (intnum) {Node Nodelast=Head; while(nodelast.next!=NULL){ if(nodelast.val==num)returnNodelast; Nodelast=Nodelast.next; } return NULL; } //implement to find a particular node in a linked list the data for the next node is NUM and returns that node PublicNode searchprevious (intnum) {Node Nodelast=Head; while(nodelast.next!=NULL){ if(nodelast.next.val==num)returnNodelast; Nodelast=Nodelast.next; } return NULL; } //change actions in a linked list Public voidChangeintNumintnumnew) { if(num)! =NULL) search (num). val=numnew; } //Delete a node of a linked list Public voidDeleteintnum) { if(num)! =NULL) searchprevious (num). Next=search (num). Next; } //Print linked list Public voidprintlist () {Node cur=Head; while(cur!=NULL) {System.out.println (cur.val); Cur=Cur.next; } } Public Static voidMain (string[] args) {singlelist singlelist=Newsinglelist (); Singlelist.add (1); Singlelist.add (2); Singlelist.add (3); Singlelist.add (4); Singlelist.change (3,8); Singlelist.delete (8); Singlelist.printlist ();//While (singlelist.head!=null) {//System.out.println (singlelist.head.val);//Singlelist.head=singlelist.head.next;// } }}
Note 1 Define a head node in Singlelist 2 note how to implement a list of 3 queries for a list delete operation you can use node node = new node (num) in any non-main function.
It's not in the main function. I know this is an internal category.
Own a 20-minute Java implementation of the one-way list (including additions and deletions to change the operation)