/* * Primitive Collections for Java. * Copyright (C) 2003 Søren Bak * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ import bak.pcj.IntIterator; import bak.pcj.list.IntDeque; import bak.pcj.list.IntArrayDeque; import bak.pcj.list.IntList; import bak.pcj.list.IntArrayList; /** * This example shows how elements get ordered using a deque. * * @author Søren Bak * @version 1.0 2003/14/1 */ public class DequeExample1 { // Prints a deque by repeatedly removing last element static void printFromLast(IntDeque q) { while (!q.isEmpty()) { int v = q.removeLast(); System.out.print(v + " "); } System.out.println(); } // Prints a deque by repeatedly removing first element static void printFromFirst(IntDeque q) { while (!q.isEmpty()) { int v = q.removeFirst(); System.out.print(v + " "); } System.out.println(); } // Creates a deque by inserting elements first static IntDeque createFromFirst(IntList list) { IntDeque q = new IntArrayDeque(); for (IntIterator i = list.iterator(); i.hasNext(); ) q.addFirst(i.next()); return q; } // Creates a deque by inserting elements last static IntDeque createFromLast(IntList list) { IntDeque q = new IntArrayDeque(); for (IntIterator i = list.iterator(); i.hasNext(); ) q.addLast(i.next()); return q; } public static void main(String[] args) { IntList list = new IntArrayList(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); IntDeque q; q = createFromFirst(list); printFromFirst(q); q = createFromFirst(list); printFromLast(q); q = createFromLast(list); printFromFirst(q); q = createFromLast(list); printFromLast(q); } }