The document contains multiple Java programs demonstrating the implementation and operations of Stack and PriorityQueue data structures. It includes methods for pushing, popping, peeking, and searching elements in a stack, as well as adding, removing, and accessing elements in a priority queue. Each program is structured with a main method and showcases various functionalities of the respective data structures.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
3 views4 pages
Lab 2
The document contains multiple Java programs demonstrating the implementation and operations of Stack and PriorityQueue data structures. It includes methods for pushing, popping, peeking, and searching elements in a stack, as well as adding, removing, and accessing elements in a priority queue. Each program is structured with a main method and showcases various functionalities of the respective data structures.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4
{
1. Java code for stack Integer pos = (Integer)
implementation stack.search(element); import java.util.Stack; if(pos == -1) class Main System.out.println("Element not { found"); // Pushing element on the top of the stack else static void stack_push(Stack<Integer> System.out.println("Element is found stack) at position: " + pos); { } for(int i = 0; i < 5; i++) public static void main (String[] args) { { stack.push(i); Stack<Integer> stack = new } Stack<Integer>(); } stack_push(stack); // Popping element from the top of the stack_pop(stack); stack stack_push(stack); static void stack_pop(Stack<Integer> stack_peek(stack); stack) stack_search(stack, 2); { stack_search(stack, 6); System.out.println("Pop Operation:"); } for(int i = 0; i < 5; i++) } { Integer y = (Integer) stack.pop(); 2. Java program to add the elements System.out.println(y); in the stack } import java.io.*; } import java.util.*; // Displaying element on the top of the class StackDemo { stack // Main Method static void stack_peek(Stack<Integer> public static void main(String[] args) stack) { { // Default initialization of Stack Integer element = (Integer) Stack stack1 = new Stack(); stack.peek(); System.out.println("Element on stack // Initialization of Stack top: " + element); // using Generics } Stack<String> stack2 = new // Searching element in the stack Stack<String>(); static void stack_search(Stack<Integer> // pushing the elements stack, int element) stack1.push("4"); stack1.push("All"); System.out.println("Stack after pop stack1.push("Geeks"); operation " stack2.push("Geeks"); + stack); stack2.push("For"); stack2.push("Geeks"); System.out.println("Is stack empty? " + // Printing the Stack Elements stack.empty()); // Should print false System.out.println(stack1); // Pop remaining elements System.out.println(stack2); stack.pop(); } stack.pop(); } stack.pop(); // Check if the stack is empty 3. Java program to demonstrate the System.out.println("Is stack empty? " + removing of the elements from the stack.empty()); // Should print true stack } import java.util.*; } import java.io.*; public class StackDemo { 4. Java program to demonstrate the public static void main(String args[]) accessing of the elements from the { stack // Creating an empty Stack import java.util.*; Stack<Integer> stack = new import java.io.*; Stack<Integer>(); public class StackDemo { // Use add() method to add elements // Main Method stack.push(10); public static void main(String args[]) stack.push(15); { stack.push(30); // Creating an empty Stack stack.push(20); Stack<String> stack = new stack.push(5); Stack<String>(); // Displaying the Stack // Use push() to add elements into the System.out.println("Initial Stack: " + Stack stack); stack.push("Welcome"); // Removing elements using pop() stack.push("To"); method stack.push("Geeks"); System.out.println("Popped element: " stack.push("For"); + stack.pop()); stack.push("Geeks"); System.out.println("Popped element: " // Displaying the Stack + stack.pop()); System.out.println("Initial Stack: " + // Displaying the Stack after pop stack); operation // Fetching the element at the head of the Stack System.out.println("The element at the 6. Java Program Implementing top of the" adding of elements to + " stack is: " + PriorityQueue stack.peek()); import java.util.*; // Displaying the Stack after the public class PriorityQueueDemo Operation { System.out.println("Final Stack: " + public static void main(String args[]) stack); { } // Creating PriorityQueue } PriorityQueue<Integer> pq = new PriorityQueue<>(); 5. Java program to demonstrate the for(int i=0;i<3;i++){ working of Priority Queue pq.add(i); import java.util.*; pq.add(1); class PriorityQueueDemo } { System.out.println(pq); public static void main(String args[]) } { } // Creating empty priority queue PriorityQueue<Integer> p = new 7. Java program to remove elements PriorityQueue<Integer>(); from a PriorityQueue // Adding items to the pQueue using add() import java.util.*; p.add(10); import java.io.*; p.add(20); public class PriorityQueueDemo { p.add(15); public static void main(String args[]) // Printing the top element of { PriorityQueue PriorityQueue<String> pq = new System.out.println(p.peek()); PriorityQueue<>(); // Printing the top element and pq.add("Geeks"); removing it pq.add("For"); // from the PriorityQueue container pq.add("Geeks"); System.out.println(p.poll()); System.out.println("Initial // Printing the top element again PriorityQueue " + pq); System.out.println(p.peek()); // using the method } pq.remove("Geeks"); } System.out.println("After Remove - " + pq); System.out.println("Poll Method - " + // Creating a priority queue pq.poll()); PriorityQueue<String> pq = new System.out.println("Final PriorityQueue<>(); PriorityQueue - " + pq); pq.add("Geeks"); } pq.add("For"); } pq.add("Geeks"); System.out.println("PriorityQueue: " + 8. Java program to access elements pq); from a PriorityQueue // Using the peek() method import java.util.*; String element = pq.peek(); class PriorityQueueDemo { System.out.println("Accessed Element: " + element); // Main Method } public static void main(String[] args) } { 9. Java program to iterate elements to a PriorityQueue
import java.util.*; public class PriorityQueueDemo { // Main Method public static void main(String args[]) { PriorityQueue<String> pq = new PriorityQueue<>(); pq.add("Geeks"); pq.add("For"); pq.add("Geeks"); Iterator iterator = pq.iterator(); while (iterator.hasNext()) { System.out.print(iterator.next() + " "); } } }