Skip to main content

Posts

Showing posts with the label queue

Josephus Problem using Queue

//Josephus.java //Out of  n members, every m'th person will be eliminated import java.io.*; //Stack--------------- class Node<T> {     T value;     Node<T> link; } class Stack<T> {     Node<T> top;     public Stack() {         top = null;     }     public void push(T item) {         Node<T> n = new Node<T>();         n.value = item;         n.link = top;         top = n;     }     public T pop() {         T item;         item = top.value;         Node<T> n = top;         n = null;         top = top.l...

Queue using Stack

import java.io.*; //Node--------------- class Node<T> {     T value;     Node<T> link; } // Stack--------------- class Stack<T> {     Node<T> top;     public Stack() {         top = null;     }     public void push(T item) {         Node<T> n = new Node<T>();         n.value = item;         n.link = top;         top = n;     }     public T pop() {         T item;         item = top.value;         Node<T> n = top;         n = null;         top = top.link;         return item; ...