Introduction of Linear table
A linear table is simply a sequence of data elements, a one-to-one relationship;
Second, ArrayList simple realization
READ: O (1)
Insert, Delete: O (n)
Code implementation:
Package org.xiazdong.list;
public class Myarraylist<t> {private static final int default_length = 10;
Private t[]t;
private int length;
Public myarraylist () {t = (t[]) new object[default_length];
length = 0;
} public myarraylist (int length) {t = (t[]) new object[length];
length = 0;
} public myArrayList (T[]arr) {this (arr.length*2);
for (int i=0;i<arr.length;i++) {T[i] = Arr[i];
}} public Boolean isEmpty () {return length = = 0;
} public void Makeempty () {t = (t[]) new object[t.length];
length = 0; } public T get (int i) throws arrayindexoutofboundsexception{if (i<0| |
I>=length) {throw new ArrayIndexOutOfBoundsException ("Array Out of Bounds");
} else{return t[i];
}} public Boolean contains (T-e) {for (T elem:t) {if (Elem.equals (e)) {return true;
}} return false;
public boolean insert (T e) {if (E==null) {return false;
}//If the capacity is insufficient, expand if (length>=t.length) {larger (length*2);
} t[length++] = e; Return true;
} public boolean remove (T e) {int pos =-1;
for (int i=0;i<length;i++) {if (T[i].equals (e)) {pos = i;
}} if (pos = =-1) {return false;
} else{for (int j=pos;j<length-1;j++) {T[j] = t[j+1];
T[J+1] = null;
} length--;
return true; }} public boolean remove (int i) {if (i<0| |
I>=length) {throw new arrayindexoutofboundsexception ();
} else{for (int j=i;j<length-1;j++) {t[j]= t[j+1];
T[J+1] = null;
} length--;
return true; }} public boolean insert (int i,t e) {if (i<0| |
I>length) {throw new arrayindexoutofboundsexception ();
} if (Length>=t.length) {larger (length*2);
} for (int j=t.length-1;j>=i;j--) {t[j+1]= t[j];
} T[i] = e;
length++;
return true;
} private void larger (int len) {t[]tmp = (t[]) new Object[len];
for (int i=0;i<t.length;i++) {Tmp[i] = T[i];
} t = tmp;
The public String toString () {StringBuilder buf = new StringBuilder (); for (int i=0;i<length;i++) {buf.append (T[i]). Append ("");
} return buf.tostring ();
} public int Find (T e) {for (int i=0;i<length;i++) {if (T[i].equals (e)) {return i;
}} return-1;
} public int GetSize () {return length;
}
}
Three, LinkedList simple realization
READ: O (n)
Insert, Delete: O (1)
Head pointer: The storage address of the first node;
Head node: Add a head node before the first Data node to store the length of the list, in order to facilitate the list operation, because for example:
Code implementation:
Package org.xiazdong.list; /** * * * * @author xzdong * * @param <T> * * public class Mylinkedlist<t> {/** * first.elem storage chain list length
degree */private beginnode first;
Public Mylinkedlist () {first = new Beginnode (0,null);
} public void Insert (T elem) {node n = new node (elem,null);
Node current = first;
while (current.next!=null) {current = Current.next;
} Current.next = n;
first.elem++; } public void Insert (int i,t elem) {if (i<0| |
I>first.elem) {throw new arrayindexoutofboundsexception ();
} Node current = first;
node n = new node (elem,null);
int count = 0;
while (current.next!=null&&count<i) {current = Current.next;
count++;
} n.next = Current.next;
Current.next = n;
first.elem++;
} public boolean remove (int i,t elem) {if (first.elem==0) {return false; } if (i<0| |
I>first.elem) {throw new arrayindexoutofboundsexception ();
}//1. Find location Node current = first;
int count = 0; while (cUrrent.next!=null&&count<i) {current = Current.next;
count++;
}//2. template code current.next = Current.next.next;
first.elem--;
return true;
} public boolean IsEmpty () {return first.elem==0;
} public void Makeempty () {first.next=null;
} public int GetSize () {return first.elem;
} public int Find (T elem) {Node current = first;
int index = 0;
while (current.next!=null) {current = Current.next;
if (Current.elem.equals (Elem)) {return index;
} else{index++;
}} return-1;
public boolean remove (T elem) {if (first.elem==0) {return false;
} Node current = First.next;
Node beforecurrent = First;
while (!current.elem.equals (elem)) {if (Current.next==null) {return false;
} beforecurrent = current;
current = Current.next;
} if (Current.elem.equals (Elem)) {beforecurrent.next = Current.next;
first.elem--;
return true;
} else{return false; }} public T get (int i) {if (i<0| |I>first.elem) {throw new arrayindexoutofboundsexception ();
} int count = 0;
Node current = first;
while (current.next!=null&&count<i) {current = Current.next;
count++;
} return Current.next.elem;
The public String toString () {StringBuilder buf = new StringBuilder ();
if (first.elem==0) {return "";
} Node current = First.next;
while (Current!=null) {buf.append (Current.elem). Append ("");
current = Current.next;
} return buf.tostring ();
} Class Node{Public Node () {elem = null;
next = null;
} public Node (T Elem,node next) {This.elem = Elem;
This.next = Next;
} private T Elem;
Node Next;
} class Beginnode extends node{int elem;
Public Beginnode (int elem,node next) {This.elem = Elem;
This.next = Next;
}
}
}
the comparison of two kinds of implementations
|
Array implementations |
Linked list implementation |
| Advantages |
Read fast |
No need to pre-allocate, insert delete convenient |
| Disadvantages |
Pre-allocate size, insert delete trouble |
Read slow |
Circular link List implementation
The circular link list is the tail pointer pointing to the head pointer, forming a loop;
Code implementation:
Package org.xiazdong.list; /** * * * * @author xzdong * * @param <T> * * public class Mycircularlinkedlist<t> {/** * FIRST.E
Lem Storage Chain List length */private Beginnode first;
Public Mycircularlinkedlist () {first = new Beginnode (0,null);
First.next = First;
} public void Insert (T elem) {node n = new node (elem,first);
Node current = first;
while (current.next!=first) {current = Current.next;
} Current.next = n;
first.elem++; } public void Insert (int i,t elem) {if (i<0| |
I>first.elem) {throw new arrayindexoutofboundsexception ();
} Node current = first;
node n = new node (elem,null);
int count = 0;
while (current.next!=null&&count<i) {current = Current.next;
count++;
} n.next = Current.next;
Current.next = n;
first.elem++;
} public boolean remove (int i,t elem) {if (first.elem==0) {return false; } if (i<0| |
I>first.elem) {throw new arrayindexoutofboundsexception (); }//1. Locate the location Node CurreNT = First;
int count = 0;
while (current.next!=first&&count<i) {current = Current.next;
count++;
}//2. template code current.next = Current.next.next;
first.elem--;
return true;
} public boolean IsEmpty () {return first.elem==0;
} public void Makeempty () {first.next=first;
} public int GetSize () {return first.elem;
} public int Find (T elem) {Node current = first;
int index = 0;
while (current.next!=first) {current = Current.next;
if (Current.elem.equals (Elem)) {return index;
} else{index++;
}} return-1;
public boolean remove (T elem) {if (first.elem==0) {return false;
} Node current = First.next;
Node beforecurrent = First;
while (!current.elem.equals (elem)) {if (Current.next==first) {return false;
} beforecurrent = current;
current = Current.next;
} if (Current.elem.equals (Elem)) {beforecurrent.next = Current.next;
first.elem--;
return true;
} else{return false; }} public T get (int i) {if (i<0| |
I>first.elem) {throw new arrayindexoutofboundsexception ();
} int count = 0;
Node current = first;
while (current.next!=first&&count<i) {current = Current.next;
count++;
} return Current.next.elem;
The public String toString () {StringBuilder buf = new StringBuilder ();
if (first.elem==0) {return "";
} Node current = First.next;
while (Current!=first) {buf.append (Current.elem). Append ("");
current = Current.next;
} return buf.tostring ();
} Class Node{Public Node () {elem = null;
next = null;
} public Node (T Elem,node next) {This.elem = Elem;
This.next = Next;
} private T Elem;
Node Next;
} class Beginnode extends node{int elem;
Public Beginnode (int elem,node next) {This.elem = Elem;
This.next = Next;
}
}
}
Introduction to the doubly linked list
On the basis of a single linked list, each node increments the pre pointer, pointing to the precursor;