diff --git a/Data Structures/Queues/Queues.java b/Data Structures/Queues/Queues.java new file mode 100644 index 00000000..c081a803 --- /dev/null +++ b/Data Structures/Queues/Queues.java @@ -0,0 +1,33 @@ +import java.util.LinkedList; +import java.util.Queue; + +public class QueueExample +{ + public static void main(String[] args) + { + Queue q = new LinkedList<>(); + + // Adds elements {0, 1, 2, 3, 4} to queue + for (int i=0; i<5; i++) + q.add(i); + + // Display contents of the queue. + System.out.println("Elements of queue-"+q); + + // To remove the head of queue. + int removedele = q.remove(); + System.out.println("removed element-" + removedele); + + System.out.println(q); + + // To view the head of queue + int head = q.peek(); + System.out.println("head of queue-" + head); + + // Rest all methods of collection interface, + // Like size and contains can be used with this + // implementation. + int size = q.size(); + System.out.println("Size of queue-" + size); + } +} diff --git a/Data Structures/Stacks/Stack.java b/Data Structures/Stacks/Stack.java new file mode 100644 index 00000000..c1ee055f --- /dev/null +++ b/Data Structures/Stacks/Stack.java @@ -0,0 +1,60 @@ + +// Java code for stack implementation + +import java.io.*; +import java.util.*; + +class Test +{ + // Pushing element on the top of the stack + static void stack_push(Stack stack) + { + for(int i = 0; i < 5; i++) + { + stack.push(i); + } + } + + // Popping element from the top of the stack + static void stack_pop(Stack stack) + { + System.out.println("Pop :"); + + for(int i = 0; i < 5; i++) + { + Integer y = (Integer) stack.pop(); + System.out.println(y); + } + } + + // Displaying element on the top of the stack + static void stack_peek(Stack stack) + { + Integer element = (Integer) stack.peek(); + System.out.println("Element on stack top : " + element); + } + + // Searching element in the stack + static void stack_search(Stack stack, int element) + { + Integer pos = (Integer) stack.search(element); + + if(pos == -1) + System.out.println("Element not found"); + else + System.out.println("Element is found at position " + pos); + } + + + public static void main (String[] args) + { + Stack stack = new Stack(); + + stack_push(stack); + stack_pop(stack); + stack_push(stack); + stack_peek(stack); + stack_search(stack, 2); + stack_search(stack, 6); + } +}