Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 18 of 18

Thread: Deque array base assistant

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Deque array base assistant

    I need help with my deque class i mainly have a issue with my toString where i can naturally insert from the front and print normally
    however when i add anything from the rear and print nothing prints out.

    I will like the tostring to print from front to rear. Thank you



    public class MyDeque {
    	private int maxSize;
    	private Item[] queArray;
    	private int front;
    	private int rear;
    	private int nItems;
     
    	public MyDeque(int s)  // constructor
    	{
    		maxSize = s;    // initialize maxsize
    		queArray = new Item[maxSize]; // type item 
    		front =0;
    		rear = maxSize-1;
    		nItems = 0; 
    	}
    	public void insertFront(Item item) // put item at rear of queue
    	{
    		if(front == 0)
    			front = maxSize;
    		queArray[--front] = item;
    		nItems++;
    	}
    	public void insertRear(Item item) // put item at rear of queue
    	{
    		if(rear==maxSize-1)
    			rear = -1;
    		queArray[++rear] = item;
    		nItems++;
    	}
    	public Item removeFront() //take item from front of queue
    	{
    		Item temp = queArray[front++];
     
    		if(front == maxSize)
    			front = 0;
    		nItems--;
    		return temp;
    	}
    	public Item removeRear() //take item from front of queue
    	{
    		Item temp = queArray[rear--];
     
    		if(rear == -1)
    			rear = maxSize -1 ;
    		nItems--;
    		return temp;
    	}
    	public Item peekFront() //peek at front of queue
    	{
    		return queArray[front];
    	}
    	public Item peekRear() //peek at last of queue
    	{
    		return queArray[rear];
    	}
    	public boolean isEmpty() //true if queue is empty
    	{
    		return(nItems == 0);
    	}
    	public boolean isFull() //true if queue is full
    	{
    		return (nItems == maxSize);
    	}
     
    	[U]public String toString(){
    		String msg = "";
    		for (int i = front; i <= rear; i++){
     
    			msg += queArray[i].toString() + "\n";
    		}
    		return msg;
    	}[/U]
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,141
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Deque array base assistant

    Try debugging the code by adding some println() statements that print out the values of variables when the code is executed. For example: when toString() is called what are the values of front and rear?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Deque array base assistant

    the println will only print the front data insertion ...the rear is not printing. thanks for your help

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,141
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Deque array base assistant

    the println will only print the front data insertion
    Why is that? A println() can print both variables' values with no problem.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Deque array base assistant

    At the first time:
    When we insert front, your front = max_size -1
    Then we insert rear, your rear = 0
    Then look at your toString():
    for(i = front; i <= rear)
    -> front > rear -> print nothing.

    I think for queue & dequeue it's better if you use a linkedlist to implement.
    If you wanna fixed array, you need to check your front, tail carefully.

  6. #6
    Junior Member
    Join Date
    Mar 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Deque array base assistant

    thanks zkidkid

    so i will need a flow control within the tostring to check the front and rear ??

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,141
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Deque array base assistant

    What is a "flow control"?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Feb 2014
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Deque array base assistant

    Quote Originally Posted by gto11520 View Post
    thanks zkidkid

    so i will need a flow control within the tostring to check the front and rear ??
    Yes if you "flow control" mean to make sure "front" always lower than "rear" ( front <= rear)

    For better implement, I also suggest you take a look of LinkedList.

  9. #9
    Junior Member
    Join Date
    Mar 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Deque array base assistant

    Quote Originally Posted by zkidkid View Post
    Yes if you "flow control" mean to make sure "front" always lower than "rear" ( front <= rear)

    For better implement, I also suggest you take a look of LinkedList.
    Thanks. I will test this tonight

  10. #10
    Junior Member
    Join Date
    Mar 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Deque array base assistant

    again i still have issue with my tostring in my dequre class and it isnt working properly. it can't print because its a one way traffic inserting front until full will print normal. or one way traffic inserting rear untill full will print normal. once i combine insert front and then insert rear then print nothing because my loop condition is not true 2<= 0 not true so doesnt print anything.

    i tried putting if then condition and then i get nullpointer error or array out of bounds

  11. #11
    Junior Member
    Join Date
    Feb 2014
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Deque array base assistant

    Please put your code so that we could help.
    at the first step, you could only implement queue(E) and dequeue() method to get understand clearly first and then implement others.
    it would be easier & better to implement by using linkedlist instead fixed array.

  12. #12
    Junior Member
    Join Date
    Mar 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Deque array base assistant

    1. i havent learned linked list quite yet. so i'm not gonna attempt to use linkedlist

    2. below code throws me an error for null pointer

    3. implmentating queue(E) has no error by inserting i had no issues. by removing no issues

    4. the issue lies with the tostring my logic is off. lets say maxsize is 3 so therefore when i insertfront front, this was initialize to zero in beginning so the first condition tested true hence predecrement making front = 2

    5. now lets say i insertrear, rear was initialize to maxsize-1 which a tree statement for the condition therefore rear is set to -1 . then preincrement rear which makes rear = 0

    6. for (int i = front; i <= rear; i++){

    msg += queArray[i].toString() + "\n";
    }
    * front = 2 rear = 0 ..... 2 <= 0 this condition is false therefore the loop never runs which is a problem for me

    however lets say i just insertfront then front = 2 and rear = maxsize -1 which 2 because maxsize is 3
    2 <= 2 is true therefore loop runs and only print front never rear.

    public String toString(){
    String msg = "";
    if (front <= rear){
    for (int i = front; i <= rear; i++){
    System.out.print("testing inner loop");
    msg += queArray[i].toString() + "\n";
    }
    }
    else
    for (int i = front; i >= rear; i--){
    System.out.print("testing inner loop");
    msg += queArray[i].toString() + "\n";
    }

    return msg;
    }

  13. #13
    Junior Member
    Join Date
    Feb 2014
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Deque array base assistant

    NullPointer at this code: queArray[i].toString()
    your queArray[i] == null.

    Please put your whole code.
    It's easier than you think.

  14. #14
    Junior Member
    Join Date
    Mar 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Deque array base assistant

    public class MyDeque {
    private int maxSize;
    private Item[] queArray;
    private int front;
    private int rear;
    private int nItems;

    public MyDeque(int s) // constructor
    {
    maxSize = s; // initialize maxsize
    queArray = new Item[maxSize]; // than requested
    front =0;
    rear = maxSize-1;
    nItems = 0;
    }
    public void insertFront(Item item) // put item at rear of queue
    {
    if(front == 0)
    front = maxSize;
    queArray[--front] = item;
    nItems++;
    }
    public void insertRear(Item item) // put item at rear of queue
    {
    if(rear==maxSize-1)
    rear = -1;
    queArray[++rear] = item;
    nItems++;
    }
    public Item removeFront() //take item from front of queue
    {
    Item temp = queArray[front++];

    if(front == maxSize)
    front = 0;
    nItems--;
    return temp;
    }
    public Item removeRear() //take item from front of queue
    {
    Item temp = queArray[rear--];

    if(rear == -1)
    rear = maxSize -1 ;
    nItems--;
    return temp;
    }
    public Item peekFront() //peek at front of queue
    {
    return queArray[front];
    }
    public Item peekRear() //peek at last of queue
    {
    return queArray[rear];
    }
    public boolean isEmpty() //true if queue is empty
    {
    return(nItems == 0);
    }
    public boolean isFull() //true if queue is full
    {
    return (nItems == maxSize);
    }

    public String toString(){
    String msg = "";
    if (front <= rear){
    for (int i = front; i <= rear; i++){
    System.out.print("testing inner loop");
    msg += queArray[i].toString() + "\n";
    }
    }
    else
    for (int i = front; i >= rear; i--){
    System.out.print("testing inner loop");
    msg += queArray[i].toString() + "\n";
    }

    return msg;
    }
    }

  15. #15
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,783
    My Mood
    Mellow
    Thanks
    175
    Thanked 625 Times in 612 Posts

    Default Re: Deque array base assistant

    Please post your code correctly, like you did in post #1. You can edit your last post and add the code tags.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,141
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Deque array base assistant

    System.out.print("testing inner loop");
    Some comments on the print statements you've used. The same message is printed by both so you won't know which one printed.
    The messages don't show the values of the variables that controlled if they were to be printed: front and rear.
    The print are inside the loop so they'l print several times. That could be useful so you'll see how many times the loop executed.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Mar 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Deque array base assistant

    Quote Originally Posted by Norm View Post
    System.out.print("testing inner loop");
    Some comments on the print statements you've used. The same message is printed by both so you won't know which one printed.
    The messages don't have show the values of the variables that controlled if they were to be printed: front and rear.
    The print are inside the loop so they'l print several times. That could be useful so you'll see how many times the loop executed.
    Thanks let me try this

  18. #18
    Junior Member
    Join Date
    Feb 2014
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Deque array base assistant

    Hi @gto11520,
    Let image that you have a fixed array for 5 items. [][][][][][]
    You put 1 front(value 1) & 1 rear (value2) with your code then you will have:
    public void insertFront(Item item) // put item at rear of queue
    {
    if(front == 0)
    front = maxSize;
    queArray[--front] = item;
    nItems++;
    }
    [][][][][1]
    it's fine.
    Then now I put 1 rear:
    public void insertRear(Item item) // put item at rear of queue
    {
    if(rear==maxSize-1)
    rear = -1;
    queArray[++rear] = item;
    nItems++;
    }
    So you will have:
    [2][][][][1]


    So how could this bellow code work ?
    public String toString(){
    String msg = "";
    if (front <= rear){
    for (int i = front; i <= rear; i++){
    System.out.print("testing inner loop");
    msg += queArray[i].toString() + "\n";
    }


    Let I talk you again, think about it more carefully to understand it & then implement later.

    Let Imagine that you have an unlimited fixed array.
    front=-1, rear = -1;
    At first time, you put 1 item, then:
    ...[][front,rear][]...
    front = 100 (for example)
    rear = 100

    Now you insert front:
    ...[front][rear][]....
    front = 99, rear = 100

    Now you still insert front:
    ...[front][item][rear][].....
    front = 98
    rear = 100

    Now you insert rear:

    ...[front][item][item][rear]...
    front = 98
    rear = 101

    got it ?

    In case you just have a fixed array, if you couldn't put front -> return No Space Error, if you couldn't put rear -> return No Space Error.
    After successful implement this above, you could think more to re-allocate your array or etc ...

Similar Threads

  1. [SOLVED] Deque and Queues: Exception in thread "main" java.lang.NullPointerException Error
    By chalupabatman in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 2nd, 2014, 04:10 PM
  2. help with deque implementation using arrays in java
    By twi in forum Java Theory & Questions
    Replies: 1
    Last Post: June 14th, 2011, 09:55 PM
  3. Base Conversion
    By Pingu in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 22nd, 2011, 02:15 PM