做新媒体每天必看的网站,郴州网络推广公司排名,alt网站标签怎么做,wordpress自适应站点双端队列#xff08;deque#xff0c;double-ended queue#xff09;是一种具有队列和栈特性的数据结构#xff0c;允许在其两端进行插入和删除操作。在Java中#xff0c;java.util.Deque接口就是双端队列的实现#xff0c;而ArrayDeque和LinkedList是其中的具体实现类。…双端队列dequedouble-ended queue是一种具有队列和栈特性的数据结构允许在其两端进行插入和删除操作。在Java中java.util.Deque接口就是双端队列的实现而ArrayDeque和LinkedList是其中的具体实现类。
下面是一个使用ArrayDeque实现的双端队列的例子我们将使用它来管理一个在线购物车系统中的商品列表。用户可以从任何一端前面或后面添加或移除商品。
import java.util.ArrayDeque;
import java.util.Deque;// 商品类
class Product {String name;double price;public Product(String name, double price) {this.name name;this.price price;}Overridepublic String toString() {return Product{ name name \ , price price };}
}public class ShoppingCart {private DequeProduct cart;public ShoppingCart() {cart new ArrayDeque();}// 向购物车尾部添加商品public void addProduct(Product product) {cart.addLast(product);}// 向购物车头部添加商品public void addFirstProduct(Product product) {cart.addFirst(product);}// 从购物车尾部移除商品public Product removeProduct() {return cart.pollLast();}// 从购物车头部移除商品public Product removeFirstProduct() {return cart.pollFirst();}// 显示购物车中的所有商品public void displayCart() {while (!cart.isEmpty()) {System.out.println(cart.pollFirst());}}public static void main(String[] args) {ShoppingCart cart new ShoppingCart();// 添加商品cart.addProduct(new Product(Apple iPhone 13, 999.99));cart.addFirstProduct(new Product(Google Pixel 6, 599.99));cart.addProduct(new Product(Samsung Galaxy S21, 799.99));// 显示购物车内容System.out.println(Shopping Cart Contents:);cart.displayCart();// 移除商品System.out.println(\nAfter removing the last item:);cart.removeProduct();cart.displayCart();// 再次移除商品System.out.println(\nAfter removing the first item:);cart.removeFirstProduct();cart.displayCart();}
}在这个例子中
Product 类表示商品包含名称和价格属性。ShoppingCart 类使用ArrayDeque作为内部数据结构来存储商品。它提供了添加商品到队列尾部或头部的方法以及从队列尾部或头部移除商品的方法。main 方法创建了一个ShoppingCart对象添加了一些商品显示了购物车的内容然后移除了商品并再次显示购物车的内容。
这个例子展示了如何使用Java中的双端队列来管理一个购物车系统中的商品列表允许用户从任意一端添加或移除商品这在许多实际应用场景中都非常有用。
让我们通过一个详细的案例和表格来进一步说明如何使用双端队列Deque在Java中管理一个购物车系统。我们将使用ArrayDeque作为我们的双端队列实现并跟踪购物车中的商品添加和移除操作。
示例代码
import java.util.ArrayDeque;
import java.util.Deque;// 商品类
class Product {String name;double price;public Product(String name, double price) {this.name name;this.price price;}Overridepublic String toString() {return Product{ name name \ , price price };}
}public class ShoppingCart {private DequeProduct cart;public ShoppingCart() {cart new ArrayDeque();}// 向购物车尾部添加商品public void addProduct(Product product) {cart.addLast(product);}// 向购物车头部添加商品public void addFirstProduct(Product product) {cart.addFirst(product);}// 从购物车尾部移除商品public Product removeProduct() {return cart.pollLast();}// 从购物车头部移除商品public Product removeFirstProduct() {return cart.pollFirst();}// 显示购物车中的所有商品public void displayCart() {System.out.println(Current Cart Contents:);for (Product product : cart) {System.out.println(product);}}public static void main(String[] args) {ShoppingCart cart new ShoppingCart();// 添加商品到购物车cart.addProduct(new Product(Apple iPhone 13, 999.99));cart.addFirstProduct(new Product(Google Pixel 6, 599.99));cart.addProduct(new Product(Samsung Galaxy S21, 799.99));// 显示购物车内容cart.displayCart();// 移除商品System.out.println(\nAfter removing the last item:);cart.removeProduct();cart.displayCart();// 再次移除商品System.out.println(\nAfter removing the first item:);cart.removeFirstProduct();cart.displayCart();}
}表格说明
购物车操作记录
操作描述购物车状态从左至右为队首至队尾初始化创建一个空的购物车[]添加到队尾addLast添加“Apple iPhone 13”[“Apple iPhone 13”]添加到队首addFirst添加“Google Pixel 6”[“Google Pixel 6”, “Apple iPhone 13”]添加到队尾addLast添加“Samsung Galaxy S21”[“Google Pixel 6”, “Apple iPhone 13”, “Samsung Galaxy S21”]移除队尾removeLast移除“Samsung Galaxy S21”[“Google Pixel 6”, “Apple iPhone 13”]移除队首removeFirst移除“Google Pixel 6”[“Apple iPhone 13”]
通过这个案例和表格我们可以清楚地看到双端队列在购物车管理中的应用商品可以被添加到队列的任一端也可以从任一端移除。这种灵活性使得双端队列成为处理双向数据流或需要快速访问两端数据的场景的理想选择。
让我们继续深入探讨双端队列Deque的应用这一次我们将使用它来解决一个常见的编程问题回文检测。回文是指正读反读都一样的字符串例如“racecar”。我们将使用双端队列的特性来高效地检查一个给定的字符串是否是回文。
示例代码
import java.util.ArrayDeque;
import java.util.Deque;public class PalindromeChecker {public static boolean isPalindrome(String str) {DequeCharacter deque new ArrayDeque();// 将字符串中的字符添加到双端队列中for (char c : str.toLowerCase().toCharArray()) {if (Character.isLetter(c)) {deque.addLast(c);}}char first, last;while (deque.size() 1) {first deque.removeFirst();last deque.removeLast();if (first ! last) {return false;}}return true;}public static void main(String[] args) {String testString A man a plan a canal Panama;if (isPalindrome(testString)) {System.out.println(testString is a palindrome.);} else {System.out.println(testString is not a palindrome.);}}
}代码解释
在这段代码中我们定义了一个名为PalindromeChecker的类其中包含一个静态方法isPalindrome该方法接受一个字符串参数并返回一个布尔值指示该字符串是否为回文。
初始化双端队列我们使用ArrayDeque作为双端队列的实现并将其实例化。添加字符遍历输入字符串的每个字符只将字母字符转换为小写并添加到队列的尾部。比较字符从双端队列的头部和尾部同时移除字符并比较它们是否相同。如果任何时候两个字符不匹配那么字符串就不是回文方法立即返回false。结束条件如果队列中只剩下一个字符或没有字符则说明字符串是回文方法返回true。
运行结果
当你运行main方法时它会检查字符串“A man a plan a canal Panama”是否为回文。由于忽略空格和大小写这个字符串实际上是一个回文因此控制台将输出“A man a plan a canal Panama is a palindrome.”
这个例子展示了如何利用双端队列的特性来简化和优化一些算法如回文检测这在实际编程中是非常有用的技巧。
接下来我们将使用双端队列Deque来解决一个有趣的问题浏览器的历史记录管理。当我们浏览网页时通常会使用浏览器的前进和后退按钮来在历史记录之间导航。双端队列非常适合模拟这一行为因为它允许我们在队列的两端高效地添加和移除元素。
示例代码
import java.util.ArrayDeque;
import java.util.Deque;public class BrowserHistory {private DequeString history;private DequeString forwardHistory;public BrowserHistory() {history new ArrayDeque();forwardHistory new ArrayDeque();}// 访问新的网址public void visit(String url) {history.addLast(url);forwardHistory.clear(); // 清空前进历史}// 返回上一个页面public String back() {if (history.size() 1) {String currentUrl history.removeLast();forwardHistory.addFirst(currentUrl);return history.getLast();} else {return No more pages to go back.;}}// 前往下一个页面public String forward() {if (!forwardHistory.isEmpty()) {String nextUrl forwardHistory.removeFirst();history.addLast(nextUrl);return nextUrl;} else {return No more pages to go forward.;}}public static void main(String[] args) {BrowserHistory browser new BrowserHistory();// 访问一些网址browser.visit(google.com);browser.visit(youtube.com);browser.visit(facebook.com);// 后退到上一个页面System.out.println(Back: browser.back());// 再次后退System.out.println(Back again: browser.back());// 前进到下一个页面System.out.println(Forward: browser.forward());}
}表格说明
浏览器历史记录操作记录
操作描述当前页面历史记录从左至右为最新至最旧前进历史从左至右为最近至最远初始化创建一个新的浏览器历史管理器N/A[][]访问visit访问“google.com”google.com[“google.com”][]访问visit访问“youtube.com”youtube.com[“google.com”, “youtube.com”][]访问visit访问“facebook.com”facebook.com[“google.com”, “youtube.com”, “facebook.com”][]后退back后退到上一个页面youtube.com[“google.com”, “youtube.com”][“facebook.com”]后退back再次后退google.com[“google.com”][“youtube.com”, “facebook.com”]前进forward前进到下一个页面youtube.com[“google.com”, “youtube.com”][“facebook.com”]
通过这个案例和表格我们可以看到双端队列如何有效地模拟浏览器的历史记录管理。每当访问一个新的网址时它会被添加到历史记录的尾部并清空前进历史。后退操作会从历史记录的尾部移除当前页面并将其添加到前进历史的头部而前进操作则相反。这种方法保证了高效且直观的网页导航体验。