单链表
public class Node {
public Object data;
public Node next;
}
import java. util. Scanner ;
public class LinkedList {
public Node head;
public LinkedList ( ) {
head = new Node ( ) ;
}
public void listHeadInsert ( ) {
Scanner sc = new Scanner ( System . in) ;
int x = sc. nextInt ( ) ;
while ( x != 9999 ) {
Node s = new Node ( ) ;
s. data = x;
s. next = head. next;
head. next = s;
x = sc. nextInt ( ) ;
}
}
public void listTailInsert ( ) {
Scanner sc = new Scanner ( System . in) ;
int x = sc. nextInt ( ) ;
Node r = head;
while ( x != 9999 ) {
Node s = new Node ( ) ;
s. data = x;
s. next = r. next;
r. next = s;
r = s;
x = sc. nextInt ( ) ;
}
}
public int length ( ) {
int len = 0 ;
Node p = head;
while ( p. next != null ) {
p = p. next;
len++ ;
}
return len;
}
public Node locataElem ( Object e) {
Node p = head. next;
while ( p != null && p. data != e) {
p = p. next;
}
return p;
}
public Node getElem ( int i) {
if ( i < 0 ) {
return null ;
}
int j = 0 ;
Node p = head;
while ( p != null && j < i) {
p = p. next;
j++ ;
}
return p;
}
public boolean deleteNode ( Node p) {
if ( p == null ) {
return false ;
}
Node q = p. next;
p. data = q. data;
p. next = q. next;
return true ;
}
public boolean delete ( int i) {
if ( i < 1 ) {
return false ;
}
Node p = getElem ( i - 1 ) ;
if ( p == null ) {
return false ;
}
if ( p. next == null ) {
return false ;
}
Node q = p. next;
p. next = q. next;
return true ;
}
public boolean insertPriorNode ( Node p, Object e) {
if ( p == null ) {
return false ;
}
Node s = new Node ( ) ;
s. next = p. next;
p. next = s;
s. data = p. data;
p. data = e;
return true ;
}
public boolean insertNextNode ( Node p, Object e) {
if ( p == null ) {
return false ;
}
Node s = new Node ( ) ;
s. data = e;
s. next = p. next;
p. next = s;
return true ;
}
public boolean insert ( int i, Object e) {
if ( i < 1 ) {
return false ;
}
Node p = getElem ( i - 1 ) ;
return insertNextNode ( p, e) ;
}
public void insertHead ( Object e) {
Node node = new Node ( ) ;
node. data = e;
node. next = head. next;
head. next = node;
}
@Override
public String toString ( ) {
StringBuilder res = new StringBuilder ( ) ;
Node current = head. next;
while ( current != null ) {
res. append ( current. data + "->" ) ;
current = current. next;
}
res. append ( "null" ) ;
return res. toString ( ) ;
}
}
双向链表
public class DNode {
public Object data;
public DNode prior;
public DNode next;
}
import java. util. Scanner ;
public class DLinkedList {
public DNode head;
public DLinkedList ( ) {
head = new DNode ( ) ;
}
public DNode getElem ( int i) {
if ( i < 0 ) {
return null ;
}
int j = 0 ;
DNode p = head;
while ( p != null && j < i) {
p = p. next;
j++ ;
}
return p;
}
public void listInsertHead ( ) {
Scanner sc = new Scanner ( System . in) ;
int x = sc. nextInt ( ) ;
while ( x != 9999 ) {
DNode s = new DNode ( ) ;
s. data = x;
InsertNextDNode ( head, s) ;
x = sc. nextInt ( ) ;
}
}
public boolean insert ( int i, Object e) {
if ( i < 1 ) {
return false ;
}
DNode p = head;
int j = 0 ;
while ( p != null && j < i - 1 ) {
p = p. next;
j++ ;
}
if ( p == null ) {
return false ;
}
DNode s = new DNode ( ) ;
s. data = e;
s. next = p. next;
if ( p. next != null ) {
p. next. prior = s;
}
s. prior = p;
p. next = s;
return true ;
}
public boolean InsertNextDNode ( DNode p, DNode s) {
if ( p == null || s == null ) {
return false ;
}
s. next = p. next;
if ( p. next != null ) {
p. next. prior = s;
}
s. prior = p;
p. next = s;
return true ;
}
@Override
public String toString ( ) {
StringBuilder res = new StringBuilder ( ) ;
DNode current = head. next;
while ( current != null ) {
res. append ( current. data + "->" ) ;
current = current. next;
}
res. append ( "null" ) ;
return res. toString ( ) ;
}
}