Results 1 to 2 of 2
  1. #1
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default Problem prioritizing a circular queue

    Hi, my problem is that I can't for the life of me wrap my head around how to implement priority in my circular queue. Here's what I've got thus far:

    Also my problem only lies in my insert() where the prioritizing needs to take place.

    I'm reading from a file as well so if needed, probably not though, I can post the part of the file I am using here.

    Main:
    Java Code:
    package project2;
    import java.io.*;
    import java.util.*;
    
    /**
     *
     * @author 
     */
    public class Main
    {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws IOException
        {
            FileInputStream fis1= new FileInputStream("AsiaCountries.Fall2010.txt");
            BufferedReader br1 = new BufferedReader(new InputStreamReader(fis1));
            String inline;
            int totalReg1Counter=0, totalReg2Counter=0, totalReg3Counter=0;
            int totalReg4Counter=0;
    
            ArrayList<Country> reg1ArrList = new ArrayList<Country>();
            ArrayList<Country> reg2ArrList = new ArrayList<Country>();
            ArrayList<Country> reg3ArrList = new ArrayList<Country>();
            ArrayList<Country> reg4ArrList = new ArrayList<Country>();
    
            while ((inline = br1.readLine()) != null)
            {
                Country tempCountry = new Country(inline);
    
                if(tempCountry.getCountryRegionNum() == 1)
                     {
                         reg1ArrList.add(totalReg1Counter, tempCountry);
                         totalReg1Counter++;
                     }// end reg1 if
    
                else if(tempCountry.getCountryRegionNum() == 2)
                     {
                         reg2ArrList.add(totalReg2Counter, tempCountry);
                         totalReg2Counter++;
                     }// end reg2 if
    
                else if(tempCountry.getCountryRegionNum() == 3)
                     {
                         reg3ArrList.add(totalReg3Counter, tempCountry);
                         totalReg3Counter++;
                     }// end reg3 if
    
                else if(tempCountry.getCountryRegionNum() == 4)
                     {
                         reg4ArrList.add(totalReg4Counter, tempCountry);
                         totalReg4Counter++;
                     }//end reg4 if
            }//end while
    
            System.out.println(reg1ArrList);
            System.out.println(reg2ArrList);
            System.out.println(reg3ArrList);
            System.out.println(reg4ArrList);
    
    //        Queue myQueue3 = new Queue(10);
    //
    //        myQueue3.insert(reg3ArrList.get(0));
    //        myQueue3.insert(reg3ArrList.get(1));
    //        myQueue3.insert(reg3ArrList.get(2));
    //        myQueue3.insert(reg3ArrList.get(3));
    //        myQueue3.insert(reg3ArrList.get(4));
    //        myQueue3.insert(reg3ArrList.get(5));
    
     
    
    //        myQueue3.insert(reg3ArrList.get(0));
    //        myQueue3.insert(reg3ArrList.get(1));
    //        myQueue3.insert(reg3ArrList.get(2));
    //        myQueue3.insert(reg3ArrList.get(3));
    //        myQueue3.insert(reg3ArrList.get(4));
    //        myQueue3.insert(reg3ArrList.get(5));
    //        myQueue3.insert(reg3ArrList.get(0));
    //        myQueue3.insert(reg3ArrList.get(1));
    
            Queue myQueue1 = new Queue(10);
    
            myQueue1.insert(reg1ArrList.get(0));
            myQueue1.insert(reg1ArrList.get(1));
            myQueue1.insert(reg1ArrList.get(2));
            myQueue1.insert(reg1ArrList.get(3));
            myQueue1.insert(reg1ArrList.get(4));
            myQueue1.insert(reg1ArrList.get(5));
    
    //        myQueue1.remove();
    //        myQueue1.remove();
    
            myQueue1.insert(reg1ArrList.get(6));
            myQueue1.insert(reg1ArrList.get(7));
            myQueue1.insert(reg1ArrList.get(8));
            myQueue1.insert(reg1ArrList.get(9));
            myQueue1.insert(reg1ArrList.get(10));
            myQueue1.insert(reg1ArrList.get(11));
            myQueue1.insert(reg1ArrList.get(12));
            myQueue1.insert(reg1ArrList.get(13));
    //        myQueue1.insert(reg1ArrList.get(14));
    //        myQueue1.insert(reg1ArrList.get(15));
    //        myQueue1.insert(reg1ArrList.get(16));
    //        myQueue1.insert(reg1ArrList.get(17));
    //        myQueue1.insert(reg1ArrList.get(18));
    
            //System.out.print(myQueue.peekFront());
    
            System.out.print((myQueue1.getQueArrAt(myQueue1.getFrontIndex())));
            System.out.println(myQueue1.getFrontIndex());
            System.out.print((myQueue1.getQueArrAt(myQueue1.getRearIndex())));
            System.out.println(myQueue1.getRearIndex());
    
    
            for (int i =0; i<myQueue1.maxSize(); i++)
            {
                System.out.print((myQueue1.getQueArrAt(i)));
            }
    
        }//end main()
    }//end Main
    Country class:
    Java Code:
    package project2;
    
    /**
     *
     * @author
     */
    public class Country
    {
        private String countryName, countryCapital, countryRegion, countryAbbrev;
        private int countryRegionNum;
    
        //-------------------------------------------------------------------------
        //Constructor: Parses data read into Country attributes and assigns
        //             data to respective variables.
        //-------------------------------------------------------------------------
        /**
         *
         * @param     countryInput String variable.
         * @return    none
         */
        public Country(String countryInput)
        {
            countryName = countryInput.substring(0,17).trim();
            countryCapital = countryInput.substring(21,40).trim();
            countryRegion = countryInput.substring(44,55).trim();
            countryRegionNum = Integer.parseInt(countryInput.substring(63,65).trim());
            countryAbbrev = countryInput.substring(66,68).trim();
        }//end parseData Constructor()
    
        //-------------------------------------------------------------------------
        // Country Name accessor.
        //-------------------------------------------------------------------------
        /**
         * @param none
         * @return     Country Name
         */
        public String getCountryName()
        {
            return countryName;
        }//end getCountryName()
    
        //-------------------------------------------------------------------------
        // Country Capital accessor.
        //-------------------------------------------------------------------------
        /**
         * @param none
         * @return     Country Capital
         */
        public String getCountryCapital()
        {
            return countryCapital;
        }//end getCountryCapital()
    
        //-------------------------------------------------------------------------
        // Country Region accessor.
        //-------------------------------------------------------------------------
        /**
         * @param none
         * @return     Country Region
         */
        public String getCountryRegion()
        {
            return countryRegion;
        }//end getCountryRegion()
    
        //-------------------------------------------------------------------------
        // Country Region Number accessor.
        //-------------------------------------------------------------------------
        /**
         * @param none
         * @return     Country Region Number
         */
        public int getCountryRegionNum()
        {
            return countryRegionNum;
        }//end getCountryRegionNum()
    
        //-------------------------------------------------------------------------
        // Country Abbreviation accessor.
        //-------------------------------------------------------------------------
        /**
         * @param none
         * @return     Country Abbreviation
         */
        public String getCountryCode()
        {
            return countryAbbrev;
        }//end getCountryAbbrev()
    
        //-------------------------------------------------------------------------
        // Formats outputs to fit under headers.
        //-------------------------------------------------------------------------
        /**
         *@param none
         * @return    Formatted string outputs
         */
        public String toString()
        {
            return String.format("%-23s %-30s %-24s  %-16d %-4s\n",
            countryName, countryCapital,countryRegion, countryRegionNum,
            countryAbbrev);
        }//end toString()
    }
    Queue class:
    Java Code:
    package project2cquast;
    
    /**
     *
     * @author Casey Quast
     */
    public class Queue
    {
        private int maxSize, front, rear, nElems;
        private Country[] queArr;
    
        public Queue(int a)
        {
            maxSize = a;
            queArr = new Country[maxSize];
            front = 0;
            rear = -1;
            nElems = 0;
        }//end Queue()
    
        public void insert(Country b)
        {
            for(int j = nElems; j>=0; j--)
                {
                    if (queArr[j].getCountryCode().compareTo(queArr
                            [j+1].getCountryCode()) <0)
    
                        queArr[j + 1] = queArr[j];
    
                    if(isFull())
            {
                if(rear == maxSize-1)
                    rear = -1;
    
                queArr[++rear] = b;
                front++;
            }
            else
            {
                if(rear == maxSize-1)
                    rear = -1;
    
                queArr[++rear] = b;
                nElems++;
    
            }
                }
            
        }//end insert()
    
        public Country remove()
        {
            Country temp = queArr[front++];
            if(front == maxSize)
                front = 0;
    
            nElems--;
            return temp;
        }//end remove()
    
        public Country peekFront()
        {
            return queArr[front];
        }//end peekFront()
    
        public boolean isFull()
        {
            return (nElems == maxSize);
        }//end isFull()
    
        public boolean isEmpty()
        {
            return (nElems==0);
        }//end isEmpty()
     
        public int size()
        {
            return nElems;
        }//end size()
    
        public int getFrontIndex()
        {
            return front;
        }//end getFrontIndex()
    
        public int getRearIndex()
        {
            return rear;
        }//end getFrontIndex()
    
        public Country getQueArrAt(int index)
        {
            return queArr[index];
        }//end getQueueArrAt()
    
        public int maxSize()
        {
            return maxSize;
        }//end maxSize()
    
    }//end Queue

  2. #2
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

Similar Threads

  1. Replies: 9
    Last Post: 11-04-2011, 03:09 AM
  2. Circular Double Linked List
    By theonly in forum Advanced Java
    Replies: 3
    Last Post: 12-06-2009, 05:10 PM
  3. Queue implementation - Enqueue problem
    By jefrazie in forum New To Java
    Replies: 5
    Last Post: 11-05-2009, 01:10 AM
  4. Trouble Developing Singly Linked Circular List
    By VinceGuad in forum New To Java
    Replies: 14
    Last Post: 02-25-2009, 04:38 PM
  5. Using a queue
    By Krmeus in forum New To Java
    Replies: 0
    Last Post: 12-10-2007, 03:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •