JAVA順序表的簡單實現

來源:互聯網
上載者:User
import java.util.Scanner;class DATA{//類比一個班級的學生記錄String key;String name;int age;}class SLType{static final int MAXLEN = 100;DATA[] ListData = new DATA[MAXLEN+1];int ListLen;//順序表已存結點的數量void SLInit(SLType sl){sl.ListLen = 0;}int SLLength(SLType sl){return (sl.ListLen);}//插入節點int SLInsert(SLType SL,int n , DATA data){int i ;if(SL.ListLen>=MAXLEN){System.out.println("順序表已滿,不能插入節點");return 0;}if(n<1 || n>SL.ListLen-1){System.out.println("插入序號有誤,不能插入節點");return 0;}//將順序表中的資料向後移動for(i = SL.ListLen; i >=n ; i--){SL.ListData[i+1] = SL.ListData[i];}SL.ListData[n] = data;SL.ListLen++;return 1;}//追加節點int SLAdd(SLType SL,DATA data){if(SL.ListLen>=MAXLEN){System.out.println("順序表已滿,不能插入節點");return 0;}SL.ListData[++SL.ListLen]=data;return 1;}//刪除節點int SLDelete(SLType SL,int n ){int i;if(n<1||n>SL.ListLen+1){System.out.println("序號輸入有誤,不能插入節點");return 0;}//往前挪for(i = n ; i<SL.ListLen;i++){SL.ListData[i] = SL.ListData[i+1];}SL.ListLen--;return 1;}//尋找節點DATA SLFindByNum(SLType SL,int n){if(n<1||n>SL.ListLen+1){System.out.println("序號輸入有誤,不能插入節點");return null;}return SL.ListData[n];}//按照關鍵字尋找節點int SLFindByCont(SLType SL,String key){int i;for(i = 1; i <= SL.ListLen ; i++){if(SL.ListData[i].key.compareTo(key)==0){return i;}}return 0;}//顯示所有節點int SLAll(SLType SL){int i;for(i = 1; i <=SL.ListLen ; i++){System.out.println(SL.ListData[i].key+"#"+SL.ListData[i].name+"#"+SL.ListData[i].age);}return 0;}}public class SequentialList {public static void main(String[] args) {int i;    SLType SL=new SLType();         //定義順序表變數 //    DATA data=new DATA();       //定義結點儲存資料類型變數DATA pdata;//定義結點儲存指標變數     String key;           //儲存關鍵字System.out.print("順序表操作示範!\n");     SL.SLInit(SL);       //初始化順序表 System.out.print("初始化順序表完成!\n");Scanner input=new Scanner(System.in);    do {                   //迴圈添加結點資料         System.out.print("輸入添加的結點(學號 姓名 年齡):");         DATA data=new DATA();          data.key=input.next();        data.name=input.next();        data.age=input.nextInt();                if(data.age!=0)               //若年齡不為0         {            if(SL.SLAdd(SL,data)==0)   //若添加結點失敗 {                break;            //退出死迴圈 }        }   else   //若年齡為0    {            break;          //退出死迴圈   }    }while(true);    System.out.print("\n順序表中的結點順序為:\n");    SL.SLAll(SL);                  //顯示所有結點資料             System.out.print("\n要取出結點的序號:");    i=input.nextInt();               //輸入結占點序號        pdata=SL.SLFindByNum(SL,i);  //按序號尋找結點     if(pdata!=null)        //若返回的結點指標不為NULL{         System.out.printf("第%d個結點為:(%s,%s,%d)\n",i,pdata.key,pdata.name,pdata.age);}           System.out.print("\n要尋找結點的關鍵字:");    key=input.next();  //輸入關鍵字         i=SL.SLFindByCont(SL,key);     //按關鍵字尋找 ,返回結點序號     pdata=SL.SLFindByNum(SL,i);   //按序號查詢,返回結點指標     if(pdata!=null)                     //若結點指標不為NULL {        System.out.printf("第%d個結點為:(%s,%s,%d)\n",i,pdata.key,pdata.name,pdata.age);  }  }}

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.