Stack là kiểu chèn dữ liệu như khi trông sách. Quyển thứ nhất rồi chồng quyển thứ 2 nên rồi trồng quyển thứ 3 vv… Và khi muốn lấy thì phải lấy thì phải lấy thứ tự từ trên xuống lấy quyển 3 rồi lấy quyển 2 rồi mới lấy được quyển 1.
Còn Queues thì theo thứ tự xếp hàng ai đến trước thì được lấy trước.+
Hàm sự dùng:
add hoặc offer(E item): đẩy phần tử mới vào.
poll(): là lấy phần từ ra
package test.lession2;
import java.util.PriorityQueue;
import java.util.Stack;
public class StackAndQueues {
public static void main(String[] args) {
// Queues
PriorityQueue<String> names = new PriorityQueue<String>();
names.offer("A1");
names.offer("C1");
names.offer("A2");
names.offer("B3");
names.add("A");
names.add("B");
while (true) {
String name = names.poll();
if (name == null) {
break;
} else {
// System.out.println("Name: " + name);
}
}
// Stack
Stack<String> books = new Stack<String>();
books.push("VeuTo");
books.push("ChanDai");
books.push("MongTo");
books.push("MatXinh");
// book.forEach((list) -> {
// System.out.println(list);
// });
for (int i = 0; i < books.size(); i++) {
System.out.println(books.pop());
System.out.println(books.pop());
}
}
}
Leave a comment