Results 1 to 2 of 2
Thread: Print elements in queue
- 04-05-2012, 09:22 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 2
- Rep Power
- 0
Print elements in queue
My program works except for the printing of the number of elements in the dynqueue. Can anyone offer some help please?
/*2.Add a private data member of integer type called count to the DynQueue class. The count member will maintain the current number of
elements in the queue. Modify put() and get() functions accordingly to update the value of count. Add a public function called getCount() to DynQueue class. The function returns the current number of elements in the queue. In the main() function, after each put or get operation, display the current number of elements in the queue by calling the getCount() function of the object of DynQueue class. Save your program as IQDemo.java. */
Java Code://A fixed-size queue class for characters. class FixedQueue implements ICharQ { private char q[]; //this array holds the queue private int putloc, getloc; //the put and get indices //Construct an empty queue given its a size. public FixedQueue (int size) { q = new char[size+1]; //allocate memory for queue putloc = getloc = 0; } //Put a character into the queue. public void put (char ch) { if (putloc==q.length-1) { System.out.println (" - Queue is full."); return; } putloc++; q[putloc] = ch; } //Get a character from the queue. public char get() { if(getloc == putloc) { System.out.println(" - Queue is empty."); return (char) 0; } getloc++; return q[getloc]; } } //A circular queue. class CircularQueue implements ICharQ { private char q[]; //this array holds the queue private int putloc, getloc; //the put and get indices //Construct an empty queue given its a size. public CircularQueue (int size) { q = new char[size+1]; //allocate memory for queue putloc = getloc = 0; } //Put a character into the queue. public void put (char ch) { /*Queue is full if either putloc is one less than *getloc, or it putloc is at the end of the array *and getloc is at the beginning.*/ if (putloc+1==getloc | ((putloc==q.length-1) & (getloc==0))) { System.out.println (" - Queue is full."); return; } putloc++; if (putloc==q.length) putloc = 0; //Loop back q[putloc] = ch; } //Get a character from the queue. public char get() { if(getloc == putloc) { System.out.println(" - Queue is empty."); return (char) 0; } getloc++; if(getloc==q.length) getloc = 0; //Loop back return q[getloc]; } } //A dynamic queue. class DynQueue implements ICharQ { private char q[]; //this array holds the queue private int putloc, getloc; //the put and get indices private int count; //maintain current number of elements in queue //Construct an empty queue given its size. public DynQueue(int size) { q = new char[size+1]; //allocate memory for queue putloc = getloc = 0; count = 0; } //Put a character in the queue. public void put (char ch) { if(putloc==q.length-1) { //increases queue size char t[] = new char [q.length * 2]; //copy elements into new queue for (int i=0; i < q.length; i++) t[i] = q[i]; q = t; } count++; putloc++; q[putloc] = ch; } //Get a character from the queue. public char get() { if (getloc == putloc) { System.out.println(" - Queue is empty."); return (char) 0; } count--; getloc++; return q[getloc]; public int getcount(); return count; } } //Demonstrate the ICharQ interface. class IQDemo { public static void main (String args []) { FixedQueue q1 = new FixedQueue(10); DynQueue q2 = new DynQueue(5); CircularQueue q3 = new CircularQueue(10); ICharQ iQ; char ch; int i; iQ = q1; //Put some characters into fixed queue. for (i=0; i < 10; i++) iQ.put ((char) ('A' + i)); //Show the queue. System.out.print("Contents of fixed queue: "); for (i = 0; i < 10; i++) { ch = iQ.get(); System.out.print(ch); } System.out.println(); iQ = q2; //Put some characters into dynamic queue. for(i=0; i < 10; i++) iQ.put ((char) ('Z' - i)); //Show the queue. System.out.print ("Count of dynamic queue: " + q2.getCount()); System.out.print ("Contents of dynamic queue: "); for(i=0; i < 10; i++) { ch = iQ.get(); System.out.print(ch); } System.out.println(); iQ = q3; //Put some characters into circular queue. for(i=0; i < 10; i++) iQ.put ((char) ('A' + i)); //Show the queue. System.out.print ("Contents of circular queue: "); for(i=0; i < 10; i++) { ch = iQ.get(); System.out.print(ch); } System.out.println(); //Put more characters into circular queue. for(i = 10; i < 20; i++) iQ.put ((char) ('A' + i)); //Show the queue. System.out.print("Contents of circular queue: "); for(i = 0; i < 10; i++) { ch = iQ.get(); System.out.print(ch); } System.out.println("\nStore and consume from" + " circular queue."); //Use and consume from circular queue. for(i = 0; i < 20; i++) { iQ.put((char) ('A' + i)); ch = iQ.get(); System.out.print(ch); } } }Last edited by crise1967; 04-05-2012 at 09:29 PM. Reason: code tags
- 04-05-2012, 11:39 PM #2
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
Similar Threads
-
Help with Printing Array... Im supposed to print out so 9 elements show per line.
By s_mxyzptlk in forum New To JavaReplies: 3Last Post: 03-08-2012, 01:09 AM -
Print the sum of elements, determined by zero elements
By Dimitri in forum New To JavaReplies: 3Last Post: 10-19-2011, 11:42 PM -
How to ask the user how many elements to print from an array?
By rania.idan in forum New To JavaReplies: 2Last Post: 12-02-2010, 04:18 AM -
How to print JTable elements using DefaultTableModel
By chyrl in forum Advanced JavaReplies: 7Last Post: 08-30-2010, 03:35 PM -
send documents to print queue
By kurenai in forum Advanced JavaReplies: 10Last Post: 06-26-2008, 07:47 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks