Give me some example where we use stack and queue both together.

asked 25 Sep '16, 16:09

Apurba's gravatar image

Apurba
517


Stack is a LIFO (last in first out) data structure. The associated link to wikipedia contains detailed description and examples.

Queue is a FIFO (first in first out) data structure. The associated link to wikipedia contains detailed description and examples.

import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Stack;


public class Demo {
    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        System.out.println("Output of stack (before pop): ");
        System.out.println(stack);

        stack.pop();
        stack.pop();

        System.out.println("Output of stack(after pop): ");
        System.out.println(stack);

        Queue<Integer> queue = new ArrayDeque<>();
        queue.add(1);
        queue.add(2);
        queue.add(3);
        queue.add(4);

        System.out.println("Output of queue(before poll): ");
        System.out.println(queue);

        queue.poll();
        queue.poll();

        System.out.println("Output of queue(after poll): ");
        System.out.println(queue);

    }
}

Output:

Output of stack (before pop): 
[1, 2, 3, 4]
Output of stack(after pop): 
[1, 2]
Output of queue(before poll): 
[1, 2, 3, 4, 5]
Output of queue(after poll): 
[3, 4, 5]
permanent link

answered 26 Sep '16, 04:44

bazlur_rahman's gravatar image

bazlur_rahman
1.3k17

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×18

question asked: 25 Sep '16, 16:09

question was seen: 701 times

last updated: 26 Sep '16, 04:44