To define an abstract node class node:
Package cn.wzbrilliant.datastructure;/** * Node * @author ice * */public abstract class Node {private node Next;public node ( ) {next=null;} public void Setnext (Node nextnode) {Next=nextnode;} Public Node GetNext () {return next;}}
linked list class, the implementation of the insertion of the end-to-end node, the specified location node, delete nodes, specify the location node, the reverse of the list and empty operation:
Package cn.wzbrilliant.datastructure;/** * Linked list * @author ice * */public class Link {protected node head;protected node tail ;p rotected int Size;public Link () {this.head = Null;this.tail = Null;this.size = 0;} public void Addatfirst (node node) {Node.setnext (head); Head=node;if (size==0) tail=node;size++;} public void Addatlast (node node) {if (size==0) {Head=tail=node;} Else{tail.setnext (node); tail=node;} Node.setnext (null); size++;} public void Removefirst () {if (size==0) throw new RuntimeException ("link size is 0 ..."); Head=head.getnext (); if (size==1) {tail.setnext (null);} size--;} public void Removelast () {if (size==0) throw new RuntimeException ("link size is 0 ..."); if (size==1) {Head=tail=null;size--;return;} Node Node=head;while (Node.getnext ()!=tail) {Node=node.getnext ();} Node.setnext (null); tail=node;size--;} /** * Queue reverse */public void reverse () {node Prenode, node, tempnode;if (size = = 0) Return;prenode = Head;node = Prenode.getnex T ();p renode.setnext (null); tail = prenode;while (node! = null) {Tempnode = Node.getneXT (); Node.setnext (prenode);p Renode = Node;node = Tempnode;} head = Prenode;} /** * Insert NewNode * * @param newNode * @param index */public void Insert (node newNode, int index) {if (index <) after the first index node ; 0 | | Index > Size) throw new RuntimeException ("Index error"), if (index = = 0) {newnode.setnext (head); head = Newnode;size++;return;} if (index = = size) {Newnode.setnext (null); Tail.setnext (newNode); tail = Newnode;size++;return;} Node node = head;for (int i = 1; node! = null; i++, node = Node.getnext ()) {if (i = = index) {newnode.setnext (Node.getnext ( ); Node.setnext (newNode); size++;return;}} /** * Remove node nodes node must override Equals () method * @param node */public void Remove (node node) {if (node = = NULL | | size = = 0) thro W New RuntimeException ("Remove error ..."); for (Node temp = head; Temp! = NULL; temp = Temp.getnext ()) {if (temp = = head) {if (Temp.equals (node)) {head = Head.getnext (); size--;continue;}} if (Temp.getnext (). Equals (node)) {Temp.setnext (Temp.getnext (). GetNext ()); size--;}}} Public Node GetFirst () {if(size = = 0) return Null;return head; Public Node GetLast () {if (size = = 0) return Null;return tail;} public int size () {return size;} public Boolean isEmpty () {if (size = = 0) return True;return false;}}
Stack class, the implementation of the stack, play, get stack top elements and the operation of the empty sentence:
Package cn.wzbrilliant.datastructure;/** * Stack * @author ice * */public class Stack {private int size;private Node Top;publi C Stack () {size = 0;top = null;} public void push (node node) {node.setnext (top); top = node;size++;} Public Node Pop () {if (top = = NULL) return null; Node node = Top;top = Top.getnext (); Size--;return node;} Public Node Top () {return top;} public int size () {return size;} public Boolean isEmpty () {if (size = = 0) return True;return false;}}
Queue class, the implementation of the queued, team, empty operation:
Package cn.wzbrilliant.datastructure;/** * Queue * @author Ice * */public class Queue {Private node head;private node TAIL;PR ivate int Size;public Queue () {this.head = Null;this.tail = Null;this.size = 0;} public void EnQueue (node node) {Tail.setnext (node); tail = node;size++;} Public Node DeQueue () {if (size = = 0) return null; Node node = Head;head = Head.getnext (); Size--;return node;} public int size () {return size;} public Boolean isEmpty () {if (size = = 0) return True;return false;}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Data structure of the link list, stack and queue Java code implementation