These are three solutions for packaging problem: two from from Fall
2004 and one from Spring 2005.
A solution with no nested loops:
public class Packages {
public static void main(String [] args) {
// creating a new queue
IntQueue packages = new IntQueue();
// storing some elements
packages.enqueue(2);
packages.enqueue(1);
packages.enqueue(5);
packages.enqueue(3);
packages.enqueue(4);
packages.enqueue(2);
int capacity = 5;
// create a IntQueue, an integer for a current item
IntQueue helper = new IntQueue();
int item;
while(!packages.isEmpty()) {
// take a package off the queue
item = packages.dequeue();
// subtract the item from the capacity
capacity = capacity - item;
if (capacity >= 0) {
// if the result is >+ 0, then load the item
System.out.println("Loading " + item + " on the truck");
} else {
// otherwise the item has to be put back on the queue,
// store it in the helper queue for now
helper.enqueue(item);
}
}
System.out.println("Done loading");
// move the items from the helper queue to the original one
while (!helper.isEmpty()) {
item = helper.dequeue();
packages.enqueue(item);
}
// print the queue
while (!packages.isEmpty()){
item = packages.dequeue();
System.out.println(item);
}
}
}
public class Packages2 {
public static void main(String [] args) {
// creating a new queue
IntQueue packages = new IntQueue();
// storing some elements
packages.enqueue(2);
packages.enqueue(1);
packages.enqueue(5);
packages.enqueue(3);
packages.enqueue(4);
packages.enqueue(2);
int capacity = 10;
IntQueue helper = new IntQueue();
while(!packages.isEmpty()) {
int item = packages.dequeue();
if (item < capacity) {
System.out.println("Loading " + item + " on the truck");
capacity = capacity - item;
}
else {
System.out.println("done loading");
capacity = 0; // to stop the loop
helper.enqueue(item);
// move the rest of the items to another queue
while (!packages.isEmpty()) {
item = packages.dequeue();
helper.enqueue(item);
}
}
}
// move the other items back to the original queue
while (!helper.isEmpty()) {
int temp = helper.dequeue();
packages.enqueue(temp);
}
// print the queue
while (!packages.isEmpty()){
int item = packages.dequeue();
System.out.println(item);
}
}
}
public class Packages1 {
public static void main(String [] args) {
// creating a new queue
IntQueue packages = new IntQueue();
// storing some elements
packages.enqueue(2);
packages.enqueue(1);
packages.enqueue(5);
packages.enqueue(3);
packages.enqueue(4);
packages.enqueue(2);
int capacity = 20;
while(capacity > 0 && !packages.isEmpty()) {
int item = packages.dequeue();
if (item < capacity) {
System.out.println("Loading " + item + " on the truck");
capacity = capacity - item;
}
else {
System.out.println("done loading");
capacity = 0; // to stop the loop
// put the item in the front of the queue:
// move the rest of the items to another queue
IntQueue helper = new IntQueue();
while (!packages.isEmpty()) {
int temp = packages.dequeue();
helper.enqueue(temp);
}
// put the item back to the original queue
packages.enqueue(item);
// move the other items back to the original queue
while (!helper.isEmpty()) {
int temp = helper.dequeue();
packages.enqueue(temp);
}
}
}
// print the queue
while (!packages.isEmpty()){
int item = packages.dequeue();
System.out.println(item);
}
}
}