Results 1 to 2 of 2
  1. #1
    specter113 is offline Member
    Join Date
    Apr 2015
    Posts
    1
    Rep Power
    0

    Default Java Deque problem

    Hey all,

    This is a homework assignment regarding deques. I have it all finished and working with my inserts going from all left or all right but it doesn't work when I do both. I get this weird zero popping up and I have no clue as to what I am missing. Ive shown others in my class and they don't know. Thanks for any tips in advance!

    Java Code:
    //demonstrates queue
    //to run this program: C>java QueueApp
    ////////////////////////////////////////////////////////////////
    
    package project2;
    
    class deque {
    	private int maxSize;
    	private long[] queArray;
    	private int front;
    	private int rear;
    	private int nItems;
    
    	// --------------------------------------------------------------
    	public deque(int s) // constructor
    	{
    		maxSize = s;
    		queArray = new long[maxSize];
    		front = 0;
    		rear = maxSize-1;
    		nItems = 0;
    	}
    
    	// --------------------------------------------------------------
    	public void insertfront(long j) // put item at rear of queue
    	{
    		if (isFull()){
    			System.out.println("Full cannot insert!");
    		}
    		if (front == 0) // deal with wraparound
    			front = maxSize-1;
    		queArray[--front] = j; // increment rear and insert
    		nItems++; // one more item
    	}
    
    	// --------------------------------------------------------------
    	public void insertrear(long j) // put item at rear of queue
    	{
    		if (isFull()){
    			System.out.println("Full cannot insert!");
    		}
    		if (rear == maxSize-1) // deal with wraparound
    			rear = -1;
    		queArray[++rear] = j; // increment rear and insert
    		nItems++; // one more item
    	}
    
    	// --------------------------------------------------------------
    	public long removefront() // take item from front of queue
    	{
    		if (isEmpty()){
    			System.out.println("Empty!");
    		}
    		long temp = queArray[front++];// get value and incr front
    		if (front == maxSize-1) // deal with wraparound
    			front = -1;
    		nItems--; // one less item
    		return temp;
    	}
    
    	// --------------------------------------------------------------
    	public long removerear() // take item from front of queue
    	{
    		if (isEmpty()){
    			System.out.println("Empty!");
    		}
    		long temp = queArray[rear--];// get value and incr front
    		if (rear < 0) // deal with wraparound
    			rear = maxSize-1;
    		nItems--; // one less item
    		return temp;
    	}
    	//----------------------------------------------------------------
    	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;
    	}
    	// --------------------------------------------------------------
    	public void display() // displays array contents
    	{
    		for (int j = 0; j < nItems; j++) {
    	        long dis=queArray[(front + j) % maxSize];
    	        System.out.print(dis);
    	        System.out.print(" ");
    		}
    		System.out.println("");
    	}
    	//---------------------------------------------------------------
    } // end class Queue
    ////////////////////////////////////////////////////////////////
    
    class QueueApp {
    	public static void main(String[] args) {
    		deque theQueue = new deque(5); // queue holds 5 items
    
    		theQueue.insertrear(10); // insert 4 items
    		theQueue.display();
    		theQueue.insertrear(20);
    		theQueue.display();
    		theQueue.insertrear(30);
    		theQueue.display();
    		theQueue.insertfront(40);
    		theQueue.display();
    
    		/*theQueue.removefront(); // remove 3 items
    		theQueue.display();
    		theQueue.removefront(); // (10, 20, 30)
    		theQueue.display();
    		theQueue.removefront();
    		theQueue.display();
    		
    		theQueue.insertfront(50); // insert 4 more items
    		theQueue.display();
    		theQueue.insertfront(60); // (wraps around)
    		theQueue.display();
    		theQueue.insertfront(70);
    		theQueue.display();
    		theQueue.insertfront(80);
    		theQueue.display();*/
    
    		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
    ////////////////////////////////////////////////////////////////
    The output that I get when I do insert front, front, front, rear:
    10
    10 20
    10 20 30
    40 0 10 20
    40 0 10 20

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Java Deque problem

    Notice that the problem occurs after insertFront. So you should carefully examine that method. Especially the boundary conditions.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 2
    Last Post: 04-19-2014, 02:50 PM
  2. Deque
    By java4amanda in forum New To Java
    Replies: 1
    Last Post: 05-19-2012, 03:48 PM
  3. Small problem with problem with Java, C++ parse program.
    By dragstang86 in forum New To Java
    Replies: 4
    Last Post: 10-30-2011, 03:43 AM
  4. Problem Display Jmenubar Java Se6 u23 versus Java SE6 u22
    By Ravanelly in forum Advanced Java
    Replies: 0
    Last Post: 01-07-2011, 09:36 AM
  5. deque
    By asheeshiit in forum Introductions
    Replies: 0
    Last Post: 01-21-2009, 04:11 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
  •