1. 程式人生 > >java資料結構(一)----------順序表操作例項

java資料結構(一)----------順序表操作例項

import java.util.Scanner;

class DATA{//資料類
	
	String key; // 節點的關鍵字
	String name;
	String age;
}
class SLType{// 定義順序表的結構陣列	
	static final int MAXLEN = 100;// 順序表的最大長度
	DATA[] ListData = new DATA[MAXLEN+1];// 儲存順序表的順序結構
	int ListLen;                         // 順序表已存節點的的數量
	
	public void SLinit(SLType SL) {// 初始化順序表
		SL.ListLen = 0; // 初始化為空表
	}
	
	public int SLLength(SLType SL) {// 返回順序表中元素的數量
		return SL.ListLen;
	}
	
	public int SLInsert(SLType SL,int n,DATA data) { // 向順序表中插入元素
		
		int i;
		
		if(SL.ListLen >= MAXLEN) {            // 順序表中的數量已經超過了最大數量
			System.out.println("順序表已滿,不能插入節點\n");
			return 0;                         // 返回0 代表不能插入成功
		}
		
		if(n<1 || n >SL.ListLen-1) {                               // 插入節點的序號不正確
			System.out.println("插入元素序號錯誤,不能插入元素!\n");
			return 0;
		}
		
		for(i=SL.ListLen;i>=n;i--) {          // 將順序表中資料向後移動
			SL.ListData[i+1] = SL.ListData[i]; 
		}
			SL.ListData[n] = data; // 插入節點
			SL.ListLen ++;         // 順序表節點數量+1
			return 1;              // 成功 返回1
	}
	
	
	public int SLADD(SLType SL,DATA data) { // 增加元素到陣列底部
		
		if(SL.ListLen >= MAXLEN) {            // 順序表中的數量已經超過了最大數量
			System.out.println("順序表已滿,不能插入節點\n");
			return 0;                         // 返回0 代表不能插入成功
		}
		
		SL.ListData[++SL.ListLen] = data;     // 陣列元素新增,並且陣列長度+1
		return 1;
	}
	
	
	public int SLDelete(SLType SL,int n) {    // 刪除指定位置的元素
		
		int i;
		if(n<1 || n >SL.ListLen-1) {                               // 插入節點的序號不正確
			System.out.println("插入元素序號錯誤,不能插入元素!\n");
			return 0;
		}
		for(i=n;i<SL.ListLen;i++) {
			SL.ListData[i] = SL.ListData[i+1];  // 所有元素向前移動
		}
		 SL.ListLen--;// 順序表的數量-1
		return 1;
	}
	
	public DATA SLFindByNum(SLType SL,int n) { // 找到指定位置的元素通過序號
		if(n<1 || n >SL.ListLen-1) {                               // 插入節點的序號不正確
			System.out.println("插入元素序號錯誤,不能插入元素!\n");
			return null;
		}
		return SL.ListData[n];
	}
	
	public int SLFindCount(SLType SL,String key) { // 按照關鍵字查詢節點
		int i;
		for(i=1;i<SL.ListLen;i++) {
			if(SL.ListData[i].key.compareTo(key) == 0) { // 查詢key是否相同
			     return i; 
			}
		}
		return 0;   // 查詢整個順序表的資料都沒有,返回0
	}
	
	public int SLAll(SLType SL) { // 顯示順序表中的所有節點
		int i;
		for(i=1;i<=SL.ListLen;i++) {
			System.out.println("這個元素的key是"+SL.ListData[i].key+"這個元素的name是"+SL.ListData[i].name+"這個元素的age是"+SL.ListData[i].age);
		}
		return 0;
	}
		
}

public class ShunXuBiao {
	
	public static void main(String[] args) {
			test();
	}
	
	public static void test() {
		int i; 
		SLType SL = new SLType(); // 定義順序表變數
		DATA pdata;               // 定義儲存節點的引用變數
		String key ;              // 定義儲存關鍵字
		
		System.out.println("順序表演示操作!\n");
		SL.SLinit(SL);;// 初始化順序表
		System.out.println("初始化順序表完成\n");
		Scanner input = new Scanner(System.in);
		do {
			System.out.println("輸入新增節點的(學號   姓名  年齡)");
			
			DATA data= new DATA();
			data.key = input.next();
			data.name = input.next();
			data.age  = input.next();
			if(!"0".equals(data.age)) { 				
				// 如果年齡不為零
				if(SL.SLADD(SL, data) == 0) {   // 如果元素新增失敗
					break;
				}
			}else {
				break;
			}
			
		}while(true);
		System.out.println("\n順序表的節點順序為: \n");
		SL.SLAll(SL); // 顯示所有節點
		
		System.out.println("\n 請輸入要選擇的節點序號  \n");
		 i = input.nextInt(); //輸入查詢的序號
		 pdata = SL.SLFindByNum(SL, i); // 查詢
		 if(pdata != null) {
			 System.out.println("找到了該節點");
		 }
		 
		 System.out.println("/n 請輸入要查詢的關鍵字 \n");
		 key = input.next();
		 i = SL.SLFindCount(SL, key); // 該關鍵字的序號
		 pdata = SL.SLFindByNum(SL, i); // 查詢
		 if(pdata != null) {
			 System.out.println("找到了該節點");
		 }
		
	}

}

執行結果:
順序表演示操作!

初始化順序表完成

輸入新增節點的(學號   姓名  年齡)
11 11 11
輸入新增節點的(學號   姓名  年齡)
22 22 22
輸入新增節點的(學號   姓名  年齡)
33 33 33
輸入新增節點的(學號   姓名  年齡)
0 0 0

順序表的節點順序為: 

這個元素的key是11這個元素的name是11這個元素的age是11
這個元素的key是22這個元素的name是22這個元素的age是22
這個元素的key是33這個元素的name是33這個元素的age是33

 請輸入要選擇的節點序號  

1
找到了該節點
/n 請輸入要查詢的關鍵字 

11
找到了該節點