您好,登錄后才能下訂單哦!
java 數(shù)據(jù)結(jié)構(gòu)之棧與隊(duì)列
一:對(duì)列
隊(duì)列是一種先進(jìn)先出的數(shù)據(jù)結(jié)構(gòu)
實(shí)現(xiàn)代碼:
package Queue; /* * 使用java構(gòu)建隊(duì)列,并模擬實(shí)現(xiàn)隊(duì)列的入隊(duì)和出對(duì)方法 */ public class Queue { //隊(duì)列類(lèi) private int maxSize; //定義隊(duì)列的長(zhǎng)度 private int[] arrQueue; //隊(duì)列 private int rear; //定義隊(duì)列的尾指針 private int front; //定義隊(duì)列的頭指針 private int empty; //元素的個(gè)數(shù) public Queue(int s) //初始化構(gòu)造函數(shù) { maxSize = s; arrQueue = new int[s]; rear = -1; front=0; empty = 0; } //實(shí)現(xiàn)插入方法 public void insert(int m) { if(rear == maxSize-1) //處理循環(huán) rear = -1; arrQueue[++rear] = m; //對(duì)尾指針加一,把值放在隊(duì)列結(jié)尾 empty++; //隊(duì)列元素個(gè)數(shù)加1 System.out.println("隊(duì)列入隊(duì)元素 為:" + m); } //實(shí)現(xiàn)出棧的方法,即取得隊(duì)列的頭元素 public int remove() { int temp = arrQueue[front++]; //將棧頂元素賦值給temp,棧頂指針加1 if(front == maxSize) //處理循環(huán) front = 0; empty--; //元素個(gè)數(shù)-1 return temp; } //判斷隊(duì)列是否為空 public boolean isEmpty() { return (empty==0); } //判斷對(duì)列是否為滿 public boolean isFull() { return (empty == maxSize); } //返回隊(duì)列長(zhǎng)度 public int qLong() { return empty; } public static void main(String[] args) { Queue q = new Queue(5); //初始化隊(duì)列為5個(gè)元素 q.insert(1); q.insert(2); q.insert(3); q.insert(4); q.insert(5); int t1 = q.remove(); System.out.println("隊(duì)列元素出隊(duì):" + t1); int t2 = q.remove(); System.out.println("隊(duì)列元素出隊(duì):" + t2); System.out.println("隊(duì)列是否為空:" + q.isEmpty()); System.out.println("隊(duì)列是否為滿:" + q.isFull()); System.out.println("隊(duì)列的長(zhǎng)度:" + q.qLong()); } }
二:棧
棧是一種先進(jìn)后出的數(shù)據(jù)結(jié)構(gòu)
1:使用數(shù)組模擬棧
package Statck; /* * 使用java構(gòu)建棧,并模擬實(shí)現(xiàn)棧的入棧和出棧方法 * 使用數(shù)組實(shí)現(xiàn) */ public class Statck1 { private int maxSize; //棧的最多元素?cái)?shù) private int top; //棧頂指針 private int len; //棧的深度 private int[] arrStack; // 模擬棧 //棧的初始化 public Statck1(int s){ maxSize = s; len =0; top= -1; arrStack = new int[s]; } //獲取棧的長(zhǎng)度 public int getLen(){ return len; } //獲取當(dāng)前棧還能插入多少個(gè)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("棧的長(zhǎng)度:" + s.getLen()); System.out.println("還能入棧元素個(gè)數(shù):" + 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構(gòu)建棧,并模擬實(shí)現(xiàn)棧的入棧和出棧方法 * 使用鏈表實(shí)現(xiàn) */ 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 入棧"); } //獲取當(dāng)前棧的深度 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("當(dāng)前棧是否為空:" + s.isEmpty()); s.push(1); s.push(2); s.push(3); s.pop(); System.out.println("當(dāng)前棧的深度為:" + s.getStatckSize()); System.out.println("當(dāng)前棧頂元素為:" + s.getTop()); } }
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持,如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,大家共同進(jìn)步!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。