package Structure.List;
/*Author: CPlusPlus小码农
*If any question,
*Please contact:
* http://daixiecplusplus.blog.163.com/
* QQ:1926742804
*/
public interface ListInterface<E extends Comparable<E>> {
public boolean IsFull();
public boolean IsEmpty();
public int Length();
public boolean Insert(int i,E value);
public boolean Delete(E value);
public int Find(E value);
public boolean Clear();
public E Get(int index);
public void Print();
}
package Structure.List;
/*Author: CPlusPlus小码农
*If any question,
*Please contact:
* http://daixiecplusplus.blog.163.com/
* QQ:1926742804
*/
public class SeqList<E extends Comparable<E> > implements ListInterface<E> {
private Object data_[];
private int length_;
private int maxSize_;
private int initSize_;
private void DoubleSize()
{
initSize_ = 2 * maxSize_ + 1;
Object temp[] = new Object[initSize_];
copy(temp);
data_ = temp;
maxSize_ = initSize_ - 1;
}
private void copy(Object other[])
{
for(int i = 1 ; i <= length_ ; ++i)
{
other[i] = data_[i];
}
}
public SeqList(int initSize)
{
initSize_ = initSize;
maxSize_ = initSize_ - 1;
length_ = 0;
data_ = new Object[initSize_];
}
@Override
public boolean IsFull() {
// TODO Auto-generated method stub
return (length_ == maxSize_);
}
@Override
public boolean IsEmpty() {
// TODO Auto-generated method stub
return (length_ == 0 );
}
@Override
public int Length() {
// TODO Auto-generated method stub
return length_;
}
@Override
public boolean Insert(int i, E value) {
// TODO Auto-generated method stub
if( i < 1 || i > maxSize_)
throw new IllegalArgumentException();
if(maxSize_ == length_)
{
DoubleSize();
}
++length_;
for(int j = length_ ; j > i ; --j)
{
data_[j] = data_[j-1];
}
data_[i] = (Object)value;
return true;
}
@Override
public boolean Delete(E value) {
// TODO Auto-generated method stub
int i = Find(value);
if(i!=0)
{
for(int j = i ; j < length_ ; ++j)
{
data_[j] = data_[j+1];
}
--length_;
}
return true;
}
@Override
public int Find(E value) {
// TODO Auto-generated method stub
if(data_ == null)
throw new NullPointerException();
data_[0] = value;
int i = length_;
while(i >= 0)
{
if(((E)data_[i]).compareTo(value) == 0 ) break;
else --i;
}
return i;
}
@Override
public boolean Clear() {
// TODO Auto-generated method stub
length_ = 0;
return true;
}
@Override
public E Get(int i) {
// TODO Auto-generated method stub
if( i < 1 || i > length_)
throw new IllegalArgumentException();
return (E)data_[i];
}
@Override
public void Print() {
// TODO Auto-generated method stub
for(int i = 1 ; i <= length_ ; ++i)
{
System.out.print((E)data_[i]+ " ,");
}
System.out.println();
}
}
package Structure.List;
/*Author: CPlusPlus小码农
*If any question,
*Please contact:
* http://daixiecplusplus.blog.163.com/
* QQ:1926742804
*/
class ListNode<E extends Comparable<E>>
{
public ListNode<E> next_;
public E data_;
public ListNode(E data, ListNode<E> next){data_ = data ; next_ = next;}
}
public class SingleList<E extends Comparable<E>> {
private ListNode<E> head_;
private ListNode<E> current_;
public SingleList(){head_ = null; current_ = head_;}
public boolean IsEmpty(){return head_ == null;}
public void Insert(E value)
{
if(head_ == null)
{
head_ = new ListNode<E>(value,null);
current_ = head_;
}
else
{
current_.next_ = new ListNode<E>(value,current_.next_);
current_ = current_.next_;
}
}
public boolean Delete(E value)
{
ListNode<E> first = head_;
if(first.data_.compareTo(value) == 0)
{
head_ = first.next_;
current_ = head_;
return true;
}
else
{
while(first.next_ != null && first.next_.data_.compareTo(value) != 0)
{
first = first.next_;
}
if(first.next_ == null) return false;
else {
first.next_ = first.next_.next_;
current_ = head_;
return true;
}
}
}
public ListNode<E> Find(E value)
{
if(head_ == null) return null;
else{
ListNode<E> temp = head_;
while( temp != null)
{
if( temp.data_.compareTo(value) == 0 ) return temp;
else temp = temp.next_;
}
}
return null;
}
public void Print() {
// TODO Auto-generated method stub
ListNode<E> temp = head_;
while(temp != null)
{
System.out.print(temp.data_+", ");
temp = temp.next_;
}
System.out.println();
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SingleList<Double> sList = new SingleList<Double>();
sList.Insert(1.0);
sList.Insert(5.0);
sList.Insert(2.0);
sList.Insert(4.0);
sList.Insert(2.0);
sList.Insert(10.0);
sList.Insert(12.0);
sList.Print();
ListNode<Double> e = sList.Find(2.0);
if(e != null) System.out.println(e.data_);
sList.Delete(1.0);
sList.Print();
sList.Delete(10.0);
sList.Print();
sList.Insert(20.0);
sList.Print();
}
}
package Structure.List;
/*Author: CPlusPlus小码农
*If any question,
*Please contact:
* http://daixiecplusplus.blog.163.com/
* QQ:1926742804
*/
class NodeT<E extends Comparable<E> >
{
NodeT<E> prev_;
NodeT<E> next_;
E data_;
public NodeT(E data,NodeT<E> prev,NodeT<E> next)
{
data_ = data;
prev_ = prev;
next_ = next;
}
};
public class LinkedList<E extends Comparable<E> > {
private NodeT<E> head_;
private NodeT<E> tail_;
private NodeT<E> cur_;
public LinkedList()
{
head_ = new NodeT<E>(null,null,null);
tail_ = new NodeT<E>(null,null,null);
head_.next_ = tail_;
tail_.prev_ = head_;
cur_ = null;
}
public void Insert(E value)
{
if(cur_ == null)
{
NodeT<E> n = new NodeT<E>(value,null,null);
n.prev_ = head_;
n.next_ = head_.next_;
head_.next_ = n;
n.next_.prev_ = n;
cur_ = n;
}
else
{
NodeT<E> n = new NodeT<E>(value,cur_,cur_.next_);
cur_.next_ = n;
n.next_.prev_ = n;
cur_ = n;
}
}
public void Delete(E value)
{
NodeT<E> n = Find(value);
if(n != null){
n.prev_.next_ = n.next_;
n.next_.prev_ = n.prev_;
cur_ = null;
}
}
public NodeT<E> Find(E value)
{
NodeT<E> n = null;
for(n = head_.next_ ; n != tail_ ; n = n.next_)
{
if(n.data_.compareTo(value) ==0 )return n;
}
return null;
}
public void Print()
{
NodeT<E> n = null;
for(n = head_.next_ ; n != tail_ ; n = n.next_)
{
System.out.print(n.data_+",");
}
System.out.println();
}
public static void main(String[] args)
{
LinkedList<Integer> list = new LinkedList<Integer>();
list.Insert(1);
list.Insert(5);
list.Insert(6);
list.Print();
list.Delete(7);
list.Print();
list.Delete(5);
list.Print();
list.Delete(1);
list.Print();
list.Insert(8);
list.Insert(10);
list.Print();
list.Delete(10);
list.Print();
}
}
package Structure.List;
/*Author: CPlusPlus小码农
*If any question,
*Please contact:
* http://daixiecplusplus.blog.163.com/
* QQ:1926742804
*/
class Node<E extends Comparable<E>>
{
E data_;
Node<E> prev_;
Node<E> next_;
public Node(E data,Node<E> prev,Node<E> next)
{
data_ = data;
prev_ = prev;
next_ = next;
}
}
public class LinkedCircleList<E extends Comparable<E> > {
public static void main(String[] args)
{
LinkedCircleList<Integer> list = new LinkedCircleList<Integer>();
list.Insert(1);
list.Insert(2);
list.Insert(3);
list.Print();
list.Delete(4);
list.Print();
list.Delete(1);
list.Print();
list.Delete(2);
list.Print();
list.Insert(4);
list.Insert(5);
list.Print();
list.Insert(7);
list.Print();
list.Delete(5);
list.Print();
list.Delete(7);
list.Print();
list.Delete(3);
list.Print();
list.Insert(10);
list.Insert(15);
list.Print();
}
private Node<E> head_;
private Node<E> current_;
private int size_;
public LinkedCircleList(){head_ = null; current_ = null; size_ = 0;}
public void Insert(E value)
{
if(head_ == null)
{
head_ = new Node<E>(value,null,null);
head_.prev_ = head_;
head_.next_ = head_;
current_ = head_;
++size_;
}
else
{
Node<E> newNode = new Node<E>(value,current_,current_.next_);
current_.next_ = newNode;
newNode.next_.prev_ = newNode;
current_ = newNode;
++size_;
}
}
public void Delete(E value)
{
Node<E> n = Find(value);
if(n == null)
{
return;
}
else
{
if( n == head_)
{
n.prev_.next_ = n.next_;
n.next_.prev_ = n.prev_;
head_ = n.next_;
current_ = head_;
}
else
{
n.next_.prev_ = n.prev_;
n.prev_.next_ = n.next_;
current_ = head_;
}
--size_;
}
}
public void Print()
{
Node<E> n = head_;
for(int i = 0 ;i < size_ ; ++i)
{
System.out.print(n.data_+" ,");
n = n.next_;
}
System.out.println();
}
public Node<E> Find(E value)
{
Node<E> n = head_;
for(int temp = 0 ; temp < size_ ; ++temp)
{
if(n.data_.compareTo(value) == 0){return n;}
else
{
n = n.next_;
}
}
return null;
}
}
package Structure.List;
/*Author: CPlusPlus小码农
*If any question,
*Please contact:
* http://daixiecplusplus.blog.163.com/
* QQ:1926742804
*/
public class ListTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ListInterface<Integer> list = new SeqList<Integer>(5);
list.Insert(1, 1);
list.Insert(1, 4);
list.Insert(2, 3);
list.Insert(1, 2);
list.Insert(1, 5);
list.Print();
list.Delete(0);
list.Print();
list.Delete(3);
list.Print();
list.Insert(1, 10);
list.Insert(1, 7);
list.Insert(1, 6);
list.Insert(1, 4);
list.Insert(1, 3);
list.Insert(1, 2);
list.Insert(1, 12);
list.Insert(1, 14);
list.Print();
System.out.println(list.Length());
Integer i = list.Get(5);
System.out.println(i);
int k = list.Find(5);
System.out.println(k);
}
}