In the previous two blog posts-Implementation of the linear Table Interface _ Java and Implementation of the node class of the single-chain table in the chain storage structure of the linear table _ Java, we have implemented the linear table interface and the node class of a single-chain table. Today, let's implement the chain storage structure of a linear table-a single-chain table class.
The comment is very detailed. Let's take a look at it:
Package Datastructure. linearlist; import Datastructure. linearlist. node; // import the single-chain table node class public class singly1_list <E> implements lList <E> // single-chain table class, implementing the linear Table Interface {protected node <E> head; // The header pointer pointing to the first node of the single-chain table public singlydomainlist () // construct an empty single-chain table {This. head = NULL;} public singlylinkedlist (node <E> head) // constructs a single-chain table {This. head = head;} public Boolean isempty () // judge whether the single-chain table is empty {return this. head = NULL;} public int length () // returns the length of a single-chain table. Traversal Algorithm {int I = 0; node <E> P = This. Head; while (P! = NULL) {I ++; P = P. next;} return I;} public e get (INT index) // return the object whose serial number is index. If the single-chain table is empty or the serial number is incorrect, null {If (this. head! = NULL & index> = 0) {Int J = 0; node <E> P = This. Head; while (P! = NULL & J <index) {J ++; P = P. Next;} If (P! = NULL) {return (e) p. data ;}} return NULL;} public e set (INT index, e element) // set the object whose serial number is index to element. If the operation is successful, the original object is returned, otherwise, null {If (this. head! = NULL & index> = 0 & element! = NULL) {Int J = 0; node <E> P = This. Head; while (P! = NULL & J <index) {J ++; P = P. Next;} If (P! = NULL) {e old = (e) p. data; p. data = element; return old; // if the operation succeeds, the original object} return NULL; // The operation is not successful} public Boolean add (INT index, e element) // Insert the element object. The number of the inserted object is index. If the operation is successful, true {If (element = NULL) {return false; // null object cannot be added )} if (this. head = NULL | index <= 0) // Insert the {node <E> q = new node <E> (element); q. next = This. head; this. head = Q; // This. head = new node (element, this. head); // Insert the header, equivalent to the above three sentences} else // The Single-chain table is not empty and index> = 1 {Int J = 0; node <E> P = This. Head; while (P. Next! = NULL & J <index-1) // find the insert position {J ++; P = P. next;} node <E> q = new node <E> (element); // insert Q at the center/end. next = P. next; // Q Insert after the P node p. next = Q; // P. next = new node (element, P. next); // Insert at the center/end, equivalent to the above three sentences} return true;} public Boolean add (E element) // Insert the object {return add (integer. max_value, element);} public e remove (INT index) // remove the object whose serial number is index. If the operation succeeds, the object to be removed is returned, otherwise, null {e old = NULL; If (this. head! = NULL & index> = 0) {If (Index = 0) // Delete the header {old = (e) This. head. data; this. head = This. head. next;} else // intermediate/tail deletion {Int J = 0; node <E> P = This. head; while (P. next! = NULL & J <index-1) // locate the precursor node {J ++; P = P. Next;} If (P. Next! = NULL) {old = (e) p. next. data; // if the operation is successful, the object P is returned. next = (P. next ). next; // Delete the successor node of P }}} return old;} public void clear () // clear the single-chain table {This. head = NULL;} Public String tostring () // returns the string {string STR = "("; node <E> P = This. head; while (P! = NULL) {STR + = P. Data. tostring (); P = P. Next; If (P! = NULL) {STR + = "," ;}} return STR + ")";}}
In the next blog, I will discuss the tostring () recursion algorithm of a single-chain table and the reversal of a single-chain table. Coming soon.