How do I limit the size of "com" from the combine function? I want it to have a strict size of 4, but it still allows more than 4 in size.
How do I limit the size of "com" from the combine function? I want it to have a strict size of 4, but it still allows more than 4 in size.
import java.util.LinkedList; public class Lab5 { // BEGIN EDITING HERE // CREATE VARIABLES YOU MAY NEED, SUCH AS: // public int count=0; /******************************************** Requirement: Replace this comment here with a comment that explains how your rules work and why you believe it is fair. *********************************************/ private LinkedList<Integer> stack; public void combine() { if(com.size()>4){ return; }// Limit to 4 elements if(pri.size()>0) com.add(pri.pop()); // Something is in priority, so pop it over to combined else if(reg.size()>0) com.add(reg.pop()); // If priority is empty, pop something from regular to combined } // DO NOT EDIT BELOW HERE // The three lists: regular, priority, and combined private LinkedList<Integer> reg, pri, com; // Constructor: Make three empty FIFO queues public Lab5() { reg = new LinkedList<Integer>(); pri = new LinkedList<Integer>(); com = new LinkedList<Integer>(); } // Add an element to the regular queue public void addReg(Integer i) { reg.add(i); combine(); } // Add an element to the priority queue public void addPri(Integer i) { pri.add(i); combine(); } // Pop an element off the combined queue public Integer pop() { Integer i = com.pop(); combine(); return i; } // Show all three queues as a string public String toString() { String s = "REG:"+reg+" "; s+= "PRI:"+pri+" "; s+= "COM:"+com; if(com.size()>4) s+= " WARNING: COMBINED LINE EXCEEDS 4"; return s; } // Create the three queues and push/pop elements on/off it public static void main(String[] args) { Lab5 qs = new Lab5(); qs.addReg(1); System.out.println(qs); qs.addPri(11); System.out.println(qs); qs.addPri(12); System.out.println(qs); qs.addPri(13); System.out.println(qs); qs.addReg(2); qs.pop(); System.out.println(qs); qs.addPri(14); System.out.println(qs); qs.addPri(15); qs.pop(); System.out.println(qs); qs.addPri(16); System.out.println(qs); qs.addReg(3); qs.pop(); System.out.println(qs); qs.addReg(4); System.out.println(qs); qs.addPri(17); System.out.println(qs); qs.addPri(18); System.out.println(qs); qs.addPri(19); System.out.println(qs); qs.addReg(5); qs.pop(); System.out.println(qs); qs.addPri(20); System.out.println(qs); qs.addPri(21); qs.pop(); System.out.println(qs); qs.addPri(22); System.out.println(qs); qs.addReg(6); qs.pop(); System.out.println(qs); qs.pop(); System.out.println(qs); qs.pop(); System.out.println(qs); qs.pop(); System.out.println(qs); qs.pop(); System.out.println(qs); qs.pop(); System.out.println(qs); }
Step by step
Solved in 3 steps