java 數據結構之棧與隊列
一:對列
隊列是一種先進先出的數據結構
實現代碼:
package Queue;
/*
* 使用java構建隊列,并模擬實現隊列的入隊和出對方法
*/
public class Queue { //隊列類
private int maxSize; //定義隊列的長度
private int[] arrQueue; //隊列
private int rear; //定義隊列的尾指針
private int front; //定義隊列的頭指針
private int empty; //元素的個數
public Queue(int s) //初始化構造函數
{
maxSize = s;
arrQueue = new int[s];
rear = -1;
front=0;
empty = 0;
}
//實現插入方法
public void insert(int m)
{
if(rear == maxSize-1) //處理循環
rear = -1;
arrQueue[++rear] = m; //對尾指針加一,把值放在隊列結尾
empty++; //隊列元素個數加1
System.out.println("隊列入隊元素 為:" + m);
}
//實現出棧的方法,即取得隊列的頭元素
public int remove()
{
int temp = arrQueue[front++]; //將棧頂元素賦值給temp,棧頂指針加1
if(front == maxSize) //處理循環
front = 0;
empty--; //元素個數-1
return temp;
}
//判斷隊列是否為空
public boolean isEmpty()
{
return (empty==0);
}
//判斷對列是否為滿
public boolean isFull()
{
return (empty == maxSize);
}
//返回隊列長度
public int qLong()
{
return empty;
}
public static void main(String[] args) {
Queue q = new Queue(5); //初始化隊列為5個元素
q.insert(1);
q.insert(2);
q.insert(3);
q.insert(4);
q.insert(5);
int t1 = q.remove();
System.out.println("隊列元素出隊:" + t1);
int t2 = q.remove();
System.out.println("隊列元素出隊:" + t2);
System.out.println("隊列是否為空:" + q.isEmpty());
System.out.println("隊列是否為滿:" + q.isFull());
System.out.println("隊列的長度:" + q.qLong());
}
}
二:棧
棧是一種先進后出的數據結構
1:使用數組模擬棧
package Statck;
/*
* 使用java構建棧,并模擬實現棧的入棧和出棧方法
* 使用數組實現
*/
public class Statck1 {
private int maxSize; //棧的最多元素數
private int top; //棧頂指針
private int len; //棧的深度
private int[] arrStack; // 模擬棧
//棧的初始化
public Statck1(int s){
maxSize = s;
len =0;
top= -1;
arrStack = new int[s];
}
//獲取棧的長度
public int getLen(){
return len;
}
//獲取當前棧還能插入多少個f元素
public int getLeaveLen(){
return (maxSize-len);
}
//判斷棧是否滿
public boolean isFull(){
return (len==maxSize);
}
//判斷棧是否為空
public boolean isEmpty(){
return (len ==0);
}
//元素入棧
public void inStack(int s)
{
arrStack[++top] = s; //棧頂指針加1,入棧
System.out.println("元素入棧:" + s);
len ++ ;//棧深度+1
}
//元素出棧
public int outStack()
{
int temp = arrStack[top--];//賦值之后減1
System.out.println("元素出棧:" + temp);
len--; //棧深度-1
return temp;
}
public static void main(String[] args) {
Statck1 s = new Statck1(5);
s.inStack(1);
s.inStack(2);
s.inStack(3);
s.inStack(4);
s.inStack(5);
s.outStack();
s.outStack();
System.out.println("棧的長度:" + s.getLen());
System.out.println("還能入棧元素個數:" + s.getLeaveLen());
System.out.println("棧的是否為空:" + s.isEmpty());
System.out.println("棧的是否為滿:" + s.isFull());
}
}
2:使用鏈表模擬棧
package Statck;
import java.util.ArrayList;
import java.util.EmptyStackException;
import java.util.List;
/*
* 使用java構建棧,并模擬實現棧的入棧和出棧方法
* 使用鏈表實現
*/
public class Statck2<E extends Object> {
private List<E> statck = new ArrayList<E>();
public Statck2(){
//棧的初始化
}
//清空棧
public void clear(){
statck.clear();
System.out.println("清空棧..........");
}
//判斷棧是否為空
public boolean isEmpty(){
return statck.isEmpty();
}
//獲取棧頂元素
public E getTop(){
if(isEmpty())
return null;
return statck.get(0);
}
//彈出棧操作
public E pop(){
if (isEmpty())
throw new EmptyStackException();
System.out.println(statck.size() + "\t 出棧");
return statck.remove(statck.size() - 1);
}
//壓入棧操作
public void push(E e){
statck.add(e);
System.out.println(e + "\t 入棧");
}
//獲取當前棧的深度
public int getStatckSize(){
if(isEmpty())
throw new EmptyStackException();
return statck.size();
}
public static void main(String[] args) {
Statck2 s = new Statck2();
s.clear(); //清空棧
System.out.println("當前棧是否為空:" + s.isEmpty());
s.push(1);
s.push(2);
s.push(3);
s.pop();
System.out.println("當前棧的深度為:" + s.getStatckSize());
System.out.println("當前棧頂元素為:" + s.getTop());
}
}
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持,如有疑問請留言或者到本站社區交流討論,大家共同進步!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。