Package com. ds. link; /***** <p> * <strong> A Java single-chain table of Data Structure </strong> * </p> * <p> * a single-chain table provides an efficient list header. insert and delete operations, however, insert operations at the end of a single-chain table are inefficient. * </p> * <p> * the single-chain table pointer field stores references of the next node, the pointer field of the End Node is null * </p> ** @ author zhb 2013 */public class SingleLink <T> {/*** Node class */private class Node <T> {private T data; // data domain private Node <T> next; // the pointer domain stores the reference public Node (T data, Node <T> next) {this. data = data; this. next = next;} public Node (T data) {this (data, null) ;}// the following are the data members and methods of the SingleLinkedList class private Node <T> header, tail; private int size; public SingleLink () {this. header = null; this. tail = null; this. size = 0;}/*** determines whether the linked list is empty */public boolean isEmpty () {return header = null;}/*** creates a header pointer, this method is only used once! */Public void addToHeader (T item) {header = new Node <T> (item); size + = 1; if (tail = null) tail = header ;} /*** Add the tail pointer. This method is used multiple times */public void addToTail (T item) {if (! IsEmpty () {// If the linked list is not empty, convert the next of the tail pointer to a new element "tail. next = new Node <T> (item); // then point the tail pointer to its own next element, tail = tail. next; size + = 1;} else {// if it is null, create a new one! At the same time, point the header and tail to its header = tail = new Node <T> (item); size + = 1 ;}/ *** print list */public void print () {if (isEmpty () {System. out. println ("null");} else {Node <T> current = header; while (current! = Null) {System. out. println ("sigleLink size:" + this. size + "\ t data:" + current. data); current = current. next ;}}/ *** insert a node into the header, which is very efficient */public void addFirst (T item) {Node <T> newNode = new Node <T> (item); newNode. next = header; header = newNode; size + = 1;}/*** insert a node at the end of the table, inefficient */public void addLast (T item) {Node <T> newNode = new Node <T> (item); while (header. next! = Null) header = header. next; header. next = newNode; newNode. next = null; size + = 1;}/*** Delete the node in the header, which is very efficient */public void deleteFirst () {if (! IsEmpty () {header = header. next; size-= 1;} else System. out. println ("The list have been emptied! ");}/*** Delete the Node at the end of the table, which is very inefficient */public void deleteLast () {Node <T> current = header; Node <T> pre = null; while (current! = Null) {pre = current; current = current. next; if (current. next = null) {pre. next = null; size-= 1 ;}}/ *** insert at any position in the linked list, that is, insert */public void insert (int n, T item) after n) {if (n <0 | n> size) {System. out. println ("insert error! ") ;}Else {Node <T> current = header; Node <T> newNode = new Node <T> (item); for (int index = 0; index <n-1; index ++) {current = current. next;} newNode. next = current. next; current. next = newNode; this. size + = 1 ;}/ *** test code */public static void main (String [] args) {SingleLink <String> sl = new SingleLink <String> (); sl. addToHeader ("A"); sl. addToTail ("B"); sl. addFirst ("D"); sl. insert (2, "C"); sl. insert (1, "E"); sl. deleteFirst (); sl. print ();}}
If you have any questions, please feel free to contact us!
This article from the "CEO Road" blog, please be sure to keep this source http://zhaohaibo.blog.51cto.com/7808533/1288654