數據結構-順序表基礎實現(java)

貼源碼了,就補上傳到git了


import java.util.Scanner;

/**

* 順序表操作實例

* 1.按照順序存儲方式存儲的線性表

* 2.結點按照邏輯次序依次存放在計算機的一組連續存儲空間中

*/

public class SequenceTable {

public static void main(String[] arg){

int i ;

//定義順序表變量

SLType SL = new SLType();

//定義結點保存引用變量

Data pdata ;

//保存關鍵字

String key ;

System.out.println("順序表-演示:");

//初始化順序表

SL.SLInit(SL);

System.out.println("初始化順序表完成");

Scanner input = new Scanner(System.in);

do{

//循環添加結點數據

System.out.println("key ,name ,age");

Data data = new Data() ;

data.key = input.next() ;

data.name = input.next() ;

data.age = input.nextInt();

//若年齡不為 0

if(data.age != 0){

//若添加結點失敗

if(SL.SLAdd(SL ,data) == 0){

break; //退出死循環

}

} else { //若年齡為 0

break; //退出死循環

}

}while(true);

System.out.println("順序表中的結點順序為:");

SL.SLAll(SL); //顯示所有結點順序

System.out.println("要取出結點的序號:");

i = input.nextInt() ; //輸入結點佔點序號

pdata = SL.SLFindByNum(SL ,i) ; //按序號查找結點

//若返回的結點引用不為null

if (pdata != null){

System.out.printf("第%d個結點為:(%s, %s ,%d)\n" ,i , pdata.key ,pdata.name ,pdata.age);

}

System.out.println("輸入要查找結點的關鍵字:");

key = input.next() ; //獲取輸入的關鍵字

i = SL.SLFindByCont(SL ,key) ; //按關鍵字查找 ,返回結點序號

pdata = SL.SLFindByNum(SL ,i) ; //按序號查找 ,返回結點引用

//若結點引用不為null

if(pdata != null){

System.out.printf("第%d個結點為:(%s ,%s ,%d)\n" ,i ,pdata.key ,pdata.name ,pdata.age);

}

}

}

/**

* 定義結點類型

*/

class Data{

String key ;

String name ;

int age ;

}

/**

* 定義順序表結構

*/

class SLType{

static final int MAXLEN = 100 ;

Data[] ListData = new Data[MAXLEN + 1] ;//保存順序表的結構數組

int ListLen ; //順序表已存結點的數量

/**

* 初始化順序表

* @param SL

*/

void SLInit(SLType SL){

SL.ListLen = 0 ; //初始化空表

}

/**

* 返回順序表的元素個數

* @param SL

* @return

*/

int SLLength(SLType SL){

return SL.ListLen ;

}

int SLInsert(SLType SL ,int n ,Data data){

int i ;

//順序表結點數量已超過最大數量

if(SL.ListLen >= MAXLEN){

System.out.print("順序表已滿,不能插入結點!\n");

return 0 ; //返回 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 ;

//順序表結點數量 +1

SL.ListLen ++ ;

//成功插入,返回 1

return 1 ;

}

/**

* 增加元素到順序表尾部

* @param SL

* @param data

* @return

*/

int SLAdd(SLType SL ,Data data){

//順序表已滿

if(SL.ListLen >= MAXLEN){

System.out.println("順序表已滿,不能再增加結點!");

return 0 ;

}

SL.ListData[++ SL.ListLen] = data ;

return 1 ;

}

/**

* 刪除順序表中的數據元素

* @param SL

* @param n

* @return

*/

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] ;

}

//順序表元素 -1

SL.ListLen -- ;

return 1 ;

}

/**

* 根據序號返回數據元素

* @param SL

* @param n

* @return

*/

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 ;

}

}


分享到:


相關文章: