Written by Bruth_Lee in Southwest University of Science And Technology.
Learn in order to practice.(学以致用)
_____________________________________________________________________________
Single chain_list order operation.(Default from small to large)(单链表排序(默认从小到大))
_____________________________________________________________________________
Analysis:(分析)
First step->We should set the chain_list to empty.(第一步->将单链表置为空)
Last step->Fill numbers into list from small to large.(第二步->从小到大填入数字)
List interface:
package Second_chapter1;
public interface List {
public void clear();//Initialization
public Object gete(int i);//Return the i element
public int leng();//Seek length
public int loct(Object el);//Find, If el has been found, return the sequence of element,else return null;
public boolean inst(int loc, Object el);//Insert el into loc;
public Object dele(int loct);//Delete the element in the loc position;
public boolean full();//Judge whether the sequence_list is full;
public boolean empt();//Judge whether the sequence_list is empt;
}
LinkList class:
package Second_chapter2;
import Second_chapter1.List;
public class LinkList implements List{
Node head;
int size;
public LinkList() {head = new Node();size=0;}
public LinkList(Object[] a) {
Node p;
int i,n=a.length;
head = new Node();
for(i=n-1; i>=0; i--) {
p = new Node(a[i], head.next);
head.setnext(p);
};
size = n;
};
public void print() {
Node p;
p = head.next;
while(p!=null)
{
System.out.print(p.data.toString()+" ");
p = p.next;
}
System.out.println();
}
public void sort() {
Node p,s;
p = head.next;
head.next = null;//Sigle chain_list empty
while(p!=null) {
s = p;
p = p.next;
orderinst(s);
}
}
private void orderinst(Node ip) {
// TODO Auto-generated method stub
Node p,q;
Object data;
data = ip.data;
if(head.next == null || ((Integer)ip.data).intValue() <= ((Integer)head.next.data).intValue())
{
ip.next = head.next;
head.next = ip;
}
else
{
p = head.next;
q =null;//q is a precursor node of p;
while(p!=null && ((Integer)p.data).intValue() < ((Integer)ip.data).intValue())
{
q=p;
p=p.next;
}
// ip.next = q.next;
ip.next = p;
q.next = ip;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Object[] c = new Object[10];
for(int i=0; i<10;i++) {
c[i] = i;
}
LinkList a = new LinkList(c);
}
public Node index(int i) {
Node p;
int j;
if(i<0 || i>size) {
p=null;
}
else if(i==0) p = head;
else {
p = head.next;
j=1;
while(i!=j){//???
p = p.next;
j++;
}
}
return p;
}
@Override
public void clear() {
// TODO Auto-generated method stub
head = new Node();
size=0;
}
@Override
public Object gete(int i) {
// TODO Auto-generated method stub
if(i<1 || i>size) return null;
Node p=index(i);
return p.getdata();
}
@Override
public int leng() {
// TODO Auto-generated method stub
return size;
}
@Override
public int loct(Object el) {
// TODO Auto-generated method stub
Node p;
int j;
p = head.next;
j=1;
// while(p!=null) {
// if(p.data.equals(el)) break;
// p=p.next;
// j++;
// }
while(p!=null && !p.data.equals(el) ) {
p=p.next;j++;
}
if(p!=null) return j;
else return -1;
};
@Override
public boolean inst(int loc, Object el) {
// TODO Auto-generated method stub
if(loc<1 || loc>size+1) return false;
Node p = index(loc-1);
p.setnext(new Node(el,p.next));
size++;
return true;
}
@Override
public Object dele(int loct) {
// TODO Auto-generated method stub
if(size==0 || loct<1 || loct>size) return null;
Node p = index(loct-1);
Object el = p.next.data;
p.setnext(p.next.next);
size--;
return el;
}
@Override
public boolean full() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean empt() {
// TODO Auto-generated method stub
return head.next == null;
}
}
Node class:
package Second_chapter2;
public class Node {
Object data;
Node next;
public Node() {
this(null,null);
}
public Node(Object d, Node n) {
data = d;
next = n;
}
public Object getdata() {return data;}
public Node getnext() {return next;}
public void setdata(Object el) {data = el;}
public void setnext(Node p) {next = p;}
};
Exam3:
package Second_chapter2;
public class Exam3 {
public static void main(String[] args) {
LinkList ll1 = new LinkList();
for(int i=0; i<10; i++) {
// ll1.inst(i+1, i+1);
ll1.inst(i+1, new Integer(i+1));
}
System.out.print("The original chain list: ");
ll1.print();
ll1.inst(5, new Integer(99));
System.out.print("The chain list which an element is inserted: ");
ll1.print();
ll1.sort();
System.out.print("The chain has been sorted: ");
ll1.print();
}
}
The result as follow: