Skip to content

Instantly share code, notes, and snippets.

@dangnhdev
Created November 9, 2015 18:51
Show Gist options
  • Save dangnhdev/99dc17a67a9a846808d3 to your computer and use it in GitHub Desktop.
Save dangnhdev/99dc17a67a9a846808d3 to your computer and use it in GitHub Desktop.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org;
import java.util.LinkedList;
import java.util.Queue;
/**
*
* @author 404NotFound
*/
public class Queues {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<String>();
queue.offer("Oklahoma");
queue.offer("Indiana");
queue.offer("Georgia");
queue.offer("Texas");
queue.offer("abc");
Queue<String> splited = split(queue);
System.out.println(queue.size());
System.out.println(splited.size());
}
public static <E> Queue<E> split(Queue<E> queue) {
Queue<E> myQueue = new LinkedList<>();
if (queue.isEmpty()) {
return myQueue;
}
for (int i = 0; i <= queue.size() / 2; i++) {
E item = queue.remove();
myQueue.offer(item);
}
return myQueue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment