stack data structure
import java.util.Stack;
public class Main {
public static void main(String[] args) {
//create a stack
Stack<String> stackofbook = new Stack<>();
//pushing data to the stack
stackofbook.push("madolduwa");
stackofbook.push("rumi");
//to view the stack
System.out.println(stackofbook);
//popping data in the stack
stackofbook.pop();
stackofbook.pop();
//peek
String bookatthetop = stackofbook.peek();
System.out.println(bookatthetop);
//check if stack is empty
System.out.println(stackofbook.empty());
}
}
Queue Data Structure (priority)
import java.util.Collection;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Queue;
public class queue {
public static void main(String[] args) {
//Queue<Double> queue = new PriorityQueue<>();
//if u wont to reverse order
Queue<Double> queue = new PriorityQueue<>(Collections.reverseOrder());
queue.offer(99.8);
queue.offer(94.4);
queue.offer(89.5);
queue.offer(84.33);
while (!queue.isEmpty()){
System.out.println(queue.poll());
}
}
}
Link List Data Structure
0 Comments