Forest Wood Blog Website: http://blog.csdn.net/wolinxuebin
Reference website: http://blog.csdn.net/sunsaigang/article/details/5751780
Descriptive narrative: The function of simple single-linked list with Java implementation
Defines a MyList class
The included functions are:
GetHead () returns the head pointer.
IsEmpty () whether the inference is null;
AddFirst (T Element) adds elements to the head of the linked list.
addlast (T Element) adds a dollar to the tail of the linked list.
The Add (T fix,t Element) inserts a new element after the specified element is fix
Remove (T Element) deletes the specified element
contains (T element) to see if an element is included
printlist() print linked list .
Other:
Using generics
program code such as the following:
public class mylist<t>{//Use generics/* * To define node class nodes */private static class node<t>{T Elemen T Node<t> Next; Node (T element,node<t> Next) {//constructor this.element = element; This.next = Next; } Node (T Element) {//constructor this (element,null); Call above constructor}}//define MyList member private node<t> head; Define HEAD Node/* * constructor */MyList () {head = NULL; }/* * Returns the head pointer */public node<t> GetHead () {return head; }/* * To see if the list is empty */public boolean isEmpty () {return null = = head;//Infer whether it is empty}/* * add element to the list header */public void AddFirst (T element) {if (IsEmpty ()) head = new node<t> (element); else head = new node<t> (element,head); }/* Adds the element to the end of the list */public void AddLast (T element) {if (IsEmpty ()) head = new node<t> (element);//False Set to null else { node<t> node = head; Not empty, use Find, know footer while (node.next! = null) node = Node.next; Node.next = new node<t> (element); }/* * Add a new element after the specified element */public Boolean Add (T fix,t Element) {if (IsEmpty ()) return false; else {node<t> Node = head; Defines the intermediate variable while (node.element! = fix && null! = Node.next) {//The program jumps out of condition 1, to the end of table 2, to find this element node = n Ode.next; Find if there is an element}//This is used to directly use while lookup, while inference is outside the while. Able to raise the speed if (node.element = = fix) {//Here first infer whether the element node.next = new Node<t> (element,node.next) is found; /Insert element. And the next of element to return true; } else return false; }/* * Delete the specified element */public Boolean remove (T Element) {if (IsEmpty ()) return false; node<t> node = head; Define variables pre and node node<t> pre = NULL; WHile (node.element! = element && null! = Node.next) {//program jumps out of condition 1, to footer 2, find this element pre = node; Save the previous variable node = Node.next; Point to next element} if (node.element = = Element) {if (null = = PRE)//assuming that the specified element is the first element head = Head.next; else Pre.next = Node.next; return true; } else return false; }/* * To see if an element is included */public Boolean contains (T element) {if (IsEmpty ()) return false; else {node<t> Node = head; while (node.element! = element && null! = Node.next)//program jumps out of condition 1, to footer 2, find this element node = Node.next; Constantly pointing to the next program if (node.element = = Element) return true; else return false; }/* * Print List */public void printlist () {if (IsEmpty ()) {System.out.println ("null"); } else{ for (node<t> node=head; node!=null;node=node.next) System.out.print (node.element + ""); System.out.println (); Print carriage return}} public static void Main (string[] args) {mylist<integer> list = new Mylist<integ Er> ();//If you do not add <Integer> to the specified parameter type, you will be warned//using an unchecked or unsafe operation. For specific information, use-xlint:unchecked to compile again. for (int i=0; i<10; i++) {list.addfirst (i); } list.printlist (); Print List.remove (0); Delete List.printlist (); Print list.addlast (0); Add list.printlist at the tail (); Print List.add (7,-7); After 7 inserted-7 list.printlist (); Print if (List.contains ( -7)) System.out.println ("is in the list!"); Else System.out.println ("Isn't in the list!"); }}
Java implementation of simple single-linked list function