1. 程式人生 > >資料結構_線性表_棧_佇列的(面向介面)實現_Unit_1;

資料結構_線性表_棧_佇列的(面向介面)實現_Unit_1;

Topic 1:線性表

package lanqiao;
/**
 * 線性表的介面
 * 我們的這個介面為順序儲存的線性表服務
 * 我們進行面向介面的程式設計
 * @author wangtong
 *
 */

public interface List {
	//指定的下標(索引)位置插入資料元素(增)
	public void insert(int index,Object obj) throws Exception;
	//刪除指定下標(索引)位置的資料(刪) 
	//注意我們的這個刪除方法的操作一般情況下 都要把我們刪除的那個資料給返回出來顯示一下的
 	public Object delect(int index) throws Exception;
	//替換指定下標(索引)位置的資料(改)
	public void replace (int index,Object obj) throws Exception;
	//獲取指定下標位置的元素(查)
	public Object getData(int index) throws Exception;
	//獲取線性表中元素的個數
	public int getSize();
	//判斷線性表是否為空
	public boolean isEmpty();
}
package lanqiao;
/**
 * 
 * 實現List介面 完成順序儲存的線性表
 * 底層實現是陣列
 * 我們要時刻注意  size指的是元素的個數  他等於下標減一
 * @author wangtong
 *
 */
public class LinearList implements List {
	
	//線性表的預設長度
	private final int defaultSize = 10;
	//線性表的總長度
	private int totalSize ;
	//線性表中元素個數
	private int size;
	//順序儲存的線性表
	private Object[] linearList;
	//無參建構函式  這個無參建構函式應當構造一個預設長度的線性表
	//所以我們先寫一個實現構造預設函式
	private void init (int sz) {
		totalSize = sz;
		size = 0;
		linearList = new Object[sz];
	}
	//無參構造
	public LinearList () {
		init(defaultSize);
	}
	//有參建構函式
	public LinearList (int size) {
		init(size);
	}
	
	
	@Override
	public void insert(int index, Object obj) throws Exception {
		//先判斷線性表是否有空餘的位置
		if (size == totalSize) {
			throw new Exception("線性表已滿,無法插入!!!");
		}
		//再判斷索引是否越界  
		/*
		 * 只允許線上性表元素的之前後之後插入  不能在一個空位置的之後插入
		 * 我們這裡的判斷條件設定為判斷 index > size 要是成立的話那麼就會
		 * 在一個空位置的之後插入  不符合  ;index = size是允許的這個時候
		 * 就是在最後一個元素的後面插入
		 */
		if (index > size) {
			throw new Exception("插入的位置不正確!!!");
		}
		//索引滿足條件之後  我們要把插入位置的元素及其以後的元素都向後移動
		//我們這裡要注意移動的順序  要從後往前執行才能保證資料不會被覆蓋
		for (int i = size - 1; i >= index; i--) {
			linearList[i+1] = linearList[i];
		}
		//插入元素
		linearList[index] = obj;
		//記錄元素的插入
		size ++;
	}

	@Override
	public Object delect(int index) throws Exception {
		//判斷線性表是否為空
		if (size == 0) {
			throw new Exception ("線性表已空,無法刪除!!!");
		}
		//判斷索引是否有錯
		if (index > size-1) {
			throw new Exception ("刪除的元素不存在!!!");
		}
		//當索引符合要求之後開始刪除
		//先獲得我們要刪除的元素  不然一會給覆蓋了
		Object temp = linearList[index];
		//從index的後一位開始往前移動
		for (int i = index; i < size - 1; i++) {
			linearList[i] = linearList[i+1];
		}
		//刪除減一
		size --;
		return temp;
	}

	@Override
	public void replace(int index, Object obj) throws Exception {
		//判斷線性表為空
		if (size == 0) {
			throw new Exception ("線性表已空,無法替換!!!");
		}
		//判斷索引是否越界
		if (index > size-1) {
			throw new Exception ("替換的元素的下標錯誤(下標指向的那個元素為null)!!!");
		}
		//替換
		linearList[index] = obj;
	}

	@Override
	public Object getData(int index) throws Exception {
		//判斷線性表為空
		if (size == 0) {
			throw new Exception ("線性表已空,無法查詢元素!!!");
		}
		//判斷索引是否越界
		if (index > size-1) {
			throw new Exception ("查詢的那個元素為null(下標指向的那個元素為null)!!!");
		}
		return linearList[index];
	}

	@Override
	public int getSize() {
		return size;
	}

	@Override
	public boolean isEmpty() {
//		if (size == 0) {
//			return true;
//		} else {
//			return false;
//		}
		//看簡單的程式碼
		return size == 0;
		
	}
	
	//增加方法   print 
	public void print () {
		//  我們這裡使用增強for會出現問題
		for (int i = 0; i < size; i++) {
			System.out.println(linearList[i]);
		}
	}
	
	//增加方法追加  表示線上性表的後面加一個元素
	public void append(Object obj) throws Exception {
		//先判斷線性表是否有空餘的位置
		if (size == totalSize) {
			throw new Exception("線性表已滿,無法插入!!!");
		}
		size ++;//為了解決第一個元素的加入
		linearList[size - 1] = obj;
	}

}
package test;

import lanqiao.LinearList;

/**
 * 測試我們寫的順序儲存的線性表
 * @author wangtong
 *
 */

public class TestLinearList {
	public static void main (String[] args) {
		//無參構造
		LinearList ll = new LinearList() ;
		//有參構造
		//LinearList ll2 = new LinearList(20);
		
		try {
			for(int i = 0; i < 10; i++)
				ll.append(i);
			System.out.println(ll.getSize());
			System.out.println("------------------------------------------");
			System.out.println("刪除" + ll.delect(5));
			System.out.println("------------------------------------------");
			System.out.println(ll.getSize());
			System.out.println("------------------------------------------");
			System.out.println(ll.getData(5));
			System.out.println(ll.getData(6));
			ll.replace(1,3);
			System.out.println("------------------------------------------");
			ll.print();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
}

Topic 2 : 棧

package lanqiao;
/**
 * 實現棧的面向介面程式設計
 * 先進後出  棧的刪除(出棧)並不是真正的刪除  
 * 他只是把指標變數挪了一位
 * @author wangtong
 *
 */

public interface StackInterface {
	//進棧(壓棧)
	public void push (Object obj) throws Exception;
	//出棧   跟以前一樣刪除一個元素要  返回這個元素看看刪除的是什麼
	public Object pop () throws Exception;
	//檢視棧頂元素但不刪除
	public Object getTop () throws Exception;
	//判斷是否為空棧
	public boolean isEmpty();
}
package lanqiao;
/**
 * 棧類的實現
 * @author wangtong
 *
 */
public class Stack implements StackInterface{

	//棧頂指標  也表示棧內的元素個數 
	private int top;
	//棧的總空間
	private int totalSize;
	//預設棧的空間
	private final int defaultSize = 10;
	//棧的容器
	private Object[] stack;
	//初始化方法
	private void init( int sz) {
		top = 0;
		totalSize = sz;
		stack = new Object[sz];
	}
	//無參構造器
	public Stack () {
		init(defaultSize);
	}
	//有參構造器
	public Stack (int size) {
		init(size);
	}
	
	@Override
	public void push(Object obj) throws Exception {
		//檢測棧是否已滿
		if (top == totalSize) {
			throw new Exception("棧已滿!!!");
		}
		stack[top] = obj;
		top ++;
		
	}

	@Override
	public Object pop() throws Exception {
		//檢測棧是否為空
		if (top == 0) {
			throw new Exception("棧為空!!!");
		}
		top --;
		return stack[top];
	}

	@Override
	public Object getTop() throws Exception {
		//檢測棧是否為空
		if (top == 0) {
			throw new Exception("棧為空!!!");
		}
		return stack[top - 1];
	}

	@Override
	public boolean isEmpty() {
		return top == 0;
	}
	
}
package lanqiao;
/**
 * 測試自己棧的實現
 * 基於陣列的順序棧
 * @author wangtong
 *
 */
public class TestStack {
	public static void main (String[] args) {
		Stack s = new Stack();
		
		try {
			for (int i = 0; i < 10; i++) {
				s.push(i);
				System.out.println(s.getTop());
			}
			// 逐個出棧
			while (!s.isEmpty()) {
				System.out.println(s.pop());
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}

Topic 3:佇列

package lanqiao;
/**
 * 佇列的特點是隊頭刪除  隊尾插入
 * 實現佇列的面向介面程式設計
 * @author wangtong
 *
 */

public interface QueueInterface {
	//入隊
	public void enter (Object obj) throws Exception;
	//出隊
	public Object out () throws Exception;
	//獲取但不刪除隊首元素
	public Object lookFront () throws Exception;
	//判空
	public boolean isEmpty () ;
	
}
package lanqiao;
/**
 * 佇列的實現類
 * @author wangtong
 *
 */
public class Queue implements QueueInterface{
	private Object[] queue;
	private int front;
	private int rear;
	private final int defaultSize = 10;
	private int totalSize;
	
	private void init (int size) {
		front = 0;
		rear = 0;
		totalSize = size;
		queue = new Object[size];
	}
	
	public Queue () {
		init(defaultSize);
	}
	public Queue (int size) {
		init(size);
	}

	@Override
	public void enter(Object obj) throws Exception {
		//判滿
		if (front == totalSize) {
			throw new Exception ("你的佇列已滿!!!");
		}
		queue[rear++] = obj;//先賦值  後加加
		
	}

	@Override
	public Object out() throws Exception {
		//判空
		if (isEmpty()) {
			throw new Exception ("棧為空!!!");
		}
		Object temp = queue[front++]; 
		return temp;
	}

	@Override
	public Object lookFront() throws Exception {
		//判空
		if (isEmpty()) {
			throw new Exception ("棧為空!!!");
		}
		return queue[front];
	}

	@Override
	public boolean isEmpty() {
		return front == rear;
	}
	
	//增加方法  自身的長度
	public int length () {
		return  rear - front;
	}
	
}
package lanqiao;
/**
 * 測試位元組寫的順序儲存結構的佇列
 * @author wangtong
 *
 */
public class TestQueue {
	public static void main (String[] args) throws Exception  {
		Queue q = new Queue();
		for (int i = 0; i < 9; i++) {
			q.enter(i);
		}
		
		System.out.println(q.out());
		q.enter(1);
		System.out.println(q.length());
	}
}