Results 1 to 2 of 2
  1. #1
    crise1967 is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default 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

  2. #2
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Print elements in queue

    If you are trying to keep track of the number of items in the queue, why not use a counter variable?

Similar Threads

  1. Replies: 3
    Last Post: 03-08-2012, 01:09 AM
  2. Replies: 3
    Last Post: 10-19-2011, 11:42 PM
  3. Replies: 2
    Last Post: 12-02-2010, 04:18 AM
  4. Replies: 7
    Last Post: 08-30-2010, 03:35 PM
  5. send documents to print queue
    By kurenai in forum Advanced Java
    Replies: 10
    Last Post: 06-26-2008, 07:47 AM

Posting Permissions

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