| Copywriting & Content Creation Copywriting is the most valuable skill you can learn. Learn how to influence people with words and make them click your order button. Ask for and do sales copy critiques. Everything about writing articles, getting content for your websites or distributing it. |
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 (permalink) |
| Status: Junior Member Join Date: Jul 2009 Posts: 1
![]() | // Queue.java // demonstrates queue // to run this program: C>java QueueApp class Queue { private int maxSize; private long[] queArray; private int front; private int rear; private int nItems; //-------------------------------------------------------------- public Queue(int s) // constructor { maxSize = s; queArray = new long[maxSize]; front = 0; rear = -1; nItems = 0; } //-------------------------------------------------------------- public void insert(long j) // put item at rear of queue { if(rear == maxSize-1) // deal with wraparound rear = -1; queArray[++rear] = j; // increment rear and insert nItems++; // one more item } //-------------------------------------------------------------- public long remove() // take item from front of queue { long temp = queArray[front++]; // get value and incr front if(front == maxSize) // deal with wraparound front = 0; nItems--; // one less item return temp; } //-------------------------------------------------------------- public long peekFront() // peek at front of queue { return queArray[front]; } //-------------------------------------------------------------- public boolean isEmpty() // true if queue is empty { return (nItems==0); } //-------------------------------------------------------------- public boolean isFull() // true if queue is full { return (nItems==maxSize); } //-------------------------------------------------------------- public int size() // number of items in queue { return nItems; } //-------------------------------------------------------------- } // end class Queue import java.util.Scanner; class QueueApp { public static void main(String[] args) { Queue theQueue = new Queue(5); // queue holds 5 items Scanner scannerObject = new Scanner(System.in); theQueue.insert(scannerObject.nextInt()); theQueue.insert(10); // insert 4 items theQueue.insert(20); theQueue.insert(30); theQueue.insert(40); theQueue.remove(); // remove 3 items theQueue.remove(); // (10, 20, 30) theQueue.remove(); theQueue.insert(50); // insert 4 more items theQueue.insert(60); // (wraps around) theQueue.insert(70); theQueue.insert(80); while( !theQueue.isEmpty() ) // remove and display { // all items long n = theQueue.remove(); // (40, 50, 60, 70, 80) System.out.print(n); System.out.print(" "); } System.out.println(""); } // end main() } // end class QueueApp |
| | |
| | #2 (permalink) |
| Status: Junior Member Join Date: Jul 2009 Posts: 1
![]() | The easiest way would be to add a method in your queue class that would look something like: public long getNum(int index) { return queArray[index]; } then your output code would look like for (int i = 0; i < theQueue.size();i++) { System.out.println( theQueue.getNum(i) ); } |
| | |
| | #3 (permalink) |
| Status: Junior Member Join Date: Jul 2009 Posts: 2
![]() | I must admit, I'm not all that comfortable with your Queue implementation here. Particularly the wraparound behavoir. When a node is inserted and wraps past first, first is unchanged. Also, nItems is incremented, even though one of the items has been overwritten. Neither of these seems to agree with the way data is represented. Anyway, I've written three toString methods, choose whatever is the most appropriate (of course, none of these will change any data associated with the queue): public String toString() { String outStr = queArray[front]+" "; int iter = front; while (iter!=rear) { iter++; if(iter == maxSize) iter = 0; outStr = outStr + queArray[iter]+" "; } return outStr; } This is the most logical sort of toString for a Queue, it starts at front, and builds a string of the values until it reaches rear. Because of the problems mentioned above, this may not make the most sense for you. public String toStringStraight() { String outStr = ""; int iter = 0; while (iter!=maxSize) { outStr = outStr + queArray[iter]+" "; iter++; } return outStr; } This is a standard array toString. It just builds it's output string of the array from index 0 to max-1. public String toStringMod() { String outStr = ""; int iter = front; int count = nItems; while (count!=0) { outStr = outStr + queArray[iter]+" "; iter++; if(iter == maxSize) iter = 0; count--; } return outStr; } This one is designed to make the most sense with your implementation. It loops through the array, starting with front, until it has added nItems numbers to the output String. This is the same output you will get from removing everything from the queue as you have done at the end of QueueApp. If you're sure you like the way your Queue works, then this would probably be the one to use. |
| | |
![]() |
| Thread Tools | |
| Display Modes | |
| |