久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

Java怎么用兩個棧實現隊列和用兩個隊列實現一個棧

152次閱讀
沒有評論

共計 2869 個字符,預計需要花費 8 分鐘才能閱讀完成。

這篇文章主要講解了“Java 怎么用兩個棧實現隊列和用兩個隊列實現一個棧”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著丸趣 TV 小編的思路慢慢深入,一起來研究和學習“Java 怎么用兩個棧實現隊列和用兩個隊列實現一個棧”吧!

import java.util.ArrayList;
import java.util.List;
import java.util.Stack; 
 
 /* 
 * Q 57  用兩個棧實現隊列  
 */
public class QueueImplementByTwoStacks {
 private Stack Integer  stack1;
 private Stack Integer  stack2;
 QueueImplementByTwoStacks(){
 stack1=new Stack Integer 
 stack2=new Stack Integer 
 }
 public Integer poll(){
 Integer re=null;
 if(!stack2.empty()){// 如果 stack2 不為空則彈出棧頂元素
 re=stack2.pop();
 }else{// 如果 stack2 為空,將 stack1 中的元素彈出,依次壓棧到 stack2 中,那么 stack1 棧底的元素就會成為 stack2 棧頂的元素,從而達到 stack1 先進先出的隊列順序
 while(!stack1.empty()){ re=stack1.pop();
 stack2.push(re);
 }
 if(!stack2.empty()){ re=stack2.pop();
 }
 }
 return re;
 }
 public Integer offer(int o){// 每次只從 stack1 壓棧
 stack1.push(o);
 return o;
 }
 public static void main(String[] args) { QueueImplementByTwoStacks queue=new QueueImplementByTwoStacks();
 List Integer  re=new ArrayList Integer 
 queue.offer(1);
 queue.offer(2);
 queue.offer(3);
 re.add(queue.poll());
 queue.offer(4);
 re.add(queue.poll());
 queue.offer(5);
 re.add(queue.poll());
 re.add(queue.poll());
 re.add(queue.poll());
 System.out.println(re.toString());
 }
}
import java.util.LinkedList;
/* 
 * Q 57  用兩個隊列實現一個棧  
 */
public class StackImplementByTwoQueues {
 //use  queue1  and  queue2  as a queue.That means only use the method  addLast  and  removeFirst . 
 private LinkedList Integer  queue1;
 private LinkedList Integer  queue2;
 StackImplementByTwoQueues(){
 queue1=new LinkedList Integer 
 queue2=new LinkedList Integer 
 }
 public Integer pop(){// 隊列 1 不為空,隊列 2 為空,將隊列順序讀到隊列 2 中,則最后一個元素就是需要彈出的值
 Integer re=null;
 if(queue1.size()==0 queue2.size()==0){
 return null;
 }
 if(queue2.size()==0){ while(queue1.size() 0){ re=queue1.removeFirst();// 等同 re=queue1.remove();
 if(queue1.size()!=0){//do not add the last element of queue1 to queue2 
 queue2.addLast(re);
 }
 }
 }else if (queue1.size()==0){ while(queue2.size() 0){ re=queue2.removeFirst();
 if(queue2.size()!=0){//do not add the last element of queue2 to queue1 
 queue1.addLast(re);
 }
 }
 }
 return re;
 }
 public Integer push(Integer o){ if(queue1.size()==0 queue2.size()==0){ queue1.addLast(o);//queue2.addLast(o); is also ok 
 }
 if(queue1.size()!=0){ queue1.addLast(o);// 等同 queue1.=add(0)
 }else if(queue2.size()!=0){ queue2.addLast(o);
 }
 return o;
 }
 public static void main(String[] args) { StackImplementByTwoQueues stack=new StackImplementByTwoQueues();
 int tmp=0;
 stack.push(1);
 stack.push(2);
 stack.push(3);
 tmp=stack.pop();
 System.out.println(tmp);//3 
 stack.push(4);
 tmp=stack.pop();
 System.out.println(tmp);//4 
 tmp=stack.pop();
 System.out.println(tmp);//2 
 stack.push(5);
 stack.push(6);
 tmp=stack.pop();
 System.out.println(tmp);//6 
 tmp=stack.pop();
 System.out.println(tmp);//5 
 tmp=stack.pop();
 System.out.println(tmp);//1 
 }
}

感謝各位的閱讀,以上就是“Java 怎么用兩個棧實現隊列和用兩個隊列實現一個棧”的內容了,經過本文的學習后,相信大家對 Java 怎么用兩個棧實現隊列和用兩個隊列實現一個棧這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是丸趣 TV,丸趣 TV 小編將為大家推送更多相關知識點的文章,歡迎關注!

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2023-08-16發表,共計2869字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 皮山县| 邓州市| 安阳县| 高陵县| 安福县| 阜新| 张家界市| 龙门县| 新乡市| 腾冲县| 莫力| 东海县| 航空| 德州市| 德阳市| 广德县| 中江县| 澎湖县| 龙门县| 东乡族自治县| 北安市| 鹿邑县| 上思县| 九龙县| 海晏县| 松潘县| 静海县| 瑞金市| 云安县| 恩施市| 金乡县| 林甸县| 昔阳县| 浑源县| 张家港市| 久治县| 志丹县| 南雄市| 柏乡县| 汉中市| 靖安县|