English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Astack is a subclass ofvector class and it represents anAST Last In First Out (LIFO)The object stack. The last element added to the top of the stack (In) can be the first element to be deleted from the stack (Out).
Aqueue class extensioncollection interface and its supportedinsert andremove The operations used are First In First Out (FIFO) . We can also implement a Stack using Queue in the following program.
import java.util.*; public class StackFromQueueTest { Queue queue = new LinkedList(); public void push(int value) { int queueSize = queue.size(); queue.add(value); for (int i = 0; i < queueSize;++) { queue.add(queue.remove()); } } public void pop() { System.out.println("An element removed from a stack is: " + queue.remove()); } public static void main(String[] args) { StackFromQueueTest test = new StackFromQueueTest(); test.push(10); test.push(20); test.push(30); test.push(40); System.out.println(test.queue); test.pop(); System.out.println(test.queue); } }
Output Result
[40, 30, 20, 10An element removed from a stack is: 40[30, 20, 10]