Dubug上顯示,
<terminated>SLinkedList [Java Application]
<terminated, exit value: 0>C:\Program Files\Java\jdk1.7.0_07\bin\javaw.exe (2013-5-7 下午8:46:10)
系統變量的Path為C:\Program Files\Java\jdk1.7.0_07\bin;
classpath.;%JAVA_HOME%\lib;
以下為代碼:
@author Administrator
*
*/
//java ADT
import java.awt.*;
class SLinkedList{//插入和刪除操作分為頭尾和中間,對於頭,前一個為空,對於尾,後一個結點為空,需要分別對待.
public class book{
protected int number;
protected String title;
protected String writer;
protected double pricing;
protected String publishinghouse;
book next; //這便是一個指針,只不過它是引用.
public book(int number,double pricing,String title,String writer,String publishinghouse)
{
this.number=number;
this.pricing=pricing;
this.title=title;
this.writer=writer;
this.publishinghouse=publishinghouse;
this.next=null;
}
}
protected book head,tail;//鏈表類的保護數據成員
public SLinkedList() {
head = tail = null;
} //創建空鏈表
public boolean isEmpty() {
return head == null;
} //判斷鏈表是否為空
public void addToHead(int number,double pricing,String title,String writer,String publishinghouse)
{
head=new book(number,pricing,title,writer,publishinghouse); //創建頭結點
if(tail==null) tail=head;
}
public void addToTail(int number,double pricing,String title,String writer,String publishinghouse)
{
if(!isEmpty()) {//若鏈表非空那麼將為指針的next初始化為新元素
tail.next=new book(number,pricing,title, writer, publishinghouse);
tail=tail.next; //以書號number作為標識符
}else { //如果為空則創建新的,並將頭尾指向它
head=tail=new book(number,pricing,title,writer,publishinghouse);
}
}
public void addFirst(int number,double pricing,String title,String writer,String publishinghouse) {
book newNode=new book(number,pricing,title,writer,publishinghouse);
newNode.next = head;
head = newNode;
} //表頭插入節點,高效率,這是插入操作之一
public void addLast(int number,double pricing,String title,String writer,String publishinghouse) {
book newNode = new book(number,pricing,title,writer,publishinghouse);
book p = head;
while (p.next != null) p = p.next;//一直到next到表尾
p.next = newNode;
newNode.next = null;
} //表尾插入結點,在表尾接一個
public void removeFirst() {
if (!isEmpty()) head = head.next;
else System.out.println("The list have been emptied!");
} //表頭刪除結點,只需讓表頭指向下一個結點
public void removeLast() { //prev為前一個結點的指針,curr為當前的指針
book prev = null, curr = head; //刪除表尾的結點
while(curr.next != null) {
prev = curr;
curr = curr.next;
if(curr.next == null)
prev.next=null; //使前一個結點指向空,則原本的表尾結點便斷開了
}
}//removeLast
public boolean insert(int appointednumber,int number,double pricing,String title,String writer,String publishinghouse) {//結點插入到鏈表某一位置
book prev = head, curr = head.next;
book newNode;
newNode = new book(number, pricing, title, writer, publishinghouse);
if(!isEmpty()) {
while( (curr!= null) && (appointednumber==curr.number) )
{ //兩個判斷條件不能換 當前不為空,且指定num一致.
prev = curr;
curr = curr.next; //appointednumber為指定插入的位置
}
newNode.next = curr;//新結點的next指針指向當前cur,即要插入的位置的下一個結點
prev.next = newNode; //cur指向了要插入的結點
return true;
}
return false;
}
public void remove(int number){//刪除指定位置的結點
book curr=head;
book prev=null;
boolean found=false;
while((curr!=null)&&(!found))
{
if(number==curr.number )
{
if(prev==null)
removeFirst();
else
prev.next=curr.next;
found=true;
}
else{
prev=curr;
curr=curr.next ;//未找到要刪除的位置則繼續找,指針後移
}
}//while
}//remove
public book index(int number)//查詢操作
{ //用index確定要找的number
book p;
for(p=head;p!=null;p=p.next ){
if(p.number==number)
return p;
number++;
}
System.out.print("Error!");
return null;//找不到,返回-1
}
public int Length(SLinkedList L){//返回鏈表長度
int l=0;
book p=head;
while(p.next!=null)
{ p=p.next ; l=l+1;}
return l;
}
public void Insertsort(SLinkedList L)
{ //按關鍵字即number非遞減排序
//對鏈表L作直接插入排序(不需要移動元素)
book p=L.head.next;
book q=L.head;//q為前面的,p為後面的
book ptr=null;
if(!isEmpty()){
for(int j=2;j<=Length(L);j++)
{
if(p.number>q.number)
p=p.next;
else
{ ptr=L.head ;
if(q==L.head)
{ book ptr0=head;
while(ptr0.next!=q)
{ptr0=ptr0.next ;}
ptr0.next=p.next;
p.next=q; //找到p結點的後一個結點,將p結點的前一個結點的指針指向p之後的那個結點
}//q為頭結點,第一趟
while(ptr.next!=q)
{ptr=ptr.next ;}//while
book ptr1=q;
while(ptr1.next !=p)
{ptr1=ptr1.next; }
ptr.next =p;
ptr1.next=p.next;
p.next=q; //換了指針,結點的next域沒變,故結點位置也沒變
book m=q;
q=p;
p=m;
p=p.next;q=q.next;
}//else
}//for
}//if
else
System.out.print("排序失敗,記錄為空");//-1表示為空,錯誤,不能排序
}//insert
public book SelectMinKey(SLinkedList L,int i){//為了簡單選擇排序,返回最小的number
book pt=L.head;
book p1;
while(i>1){ ; pt=pt.next; i--; }
p1=pt.next;
while(p1.next!=null){
if(pt.number>p1.number)
{book s=p1;p1=pt;pt=s; }
p1=p1.next;
}//比pt所指number小的則交換,p1與ptr一直比較下去直到鏈表全部都已經比較過了
return pt;
}//SelectMinKey
public void Selectionsort(SLinkedList L)
{//簡單選擇排序,堆排序是一維數組存儲才比較方便
if(L.head==null)
System.out.print("Error,linkedlist is empty!");
if(L.head.next==null)
System.out.print("Okay!There is only one book");
//以上需要修飾,給出友好互動界面
for(int i=1;i<=Length(L);i++){
//仍以number作為關鍵字進行排序
book j=null;book p=L.head ;
while(i>1){ ; p=p.next; i--; }
j=SelectMinKey(L,i);
if(p!=j) //鏈表結點的交換,只需改變指針,改變next域
{ book j1=head;
book j2=head;
while(j1.next!=p){j1=j1.next ;}//利用兩個指針j1j2儲存指向p和j的next域
while(j2.next!=j){j2=j2.next ;}
j1.next=p.next ;j2.next=j.next ;p.next=j2.next;j.next=j1.next ; }
}
}
public static void main(String[] args)
{
Frame frm=new Frame("SlinkedList!");
frm.setSize(300,200);
frm.setLocation(500, 400);
Panel pan=new Panel();
pan.setSize(150,100);
pan.setLocation(50,50);
pan.setBackground(Color.gray );
Button bun=new Button("yes");
bun.setSize(80, 20);
bun.setLocation(50, 50);
bun.setBackground(Color.yellow );
frm.setLayout(null);
pan.setLayout(null);
pan.add(bun);
frm.add(pan);
SLinkedList L=new SLinkedList();
L.addToHead(00,80.2,"AB", "Steven", "xx publishinghouse");
L.index(00);
}
}//class SLinkedList
在main
方法最後添加
frm.pack();
frm.show();