Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

BurgessDeque.java is a program that implements and demonstrates a deque interface. In the main method I am having issues with line 200 throwing a NullPointerException.

BurgessDeque.java is a program that implements and demonstrates a deque interface. In the main method I am having issues with line 200 throwing a NullPointerException. I have done basically the exact project in the past but this one requires the use of the NoSuchElementException so that is where I believe I am going wrong. Please take a look at my code and help me fix it. Note: All methods must have time complexity O(1) and the NoSuchElementException has to be used.

DoubleNode.java

/**

* Represents nodes in a doubly linked list

*

* @author Breanna Burgess

* @version 1.0

*/

public class DoubleNode

{

private DoubleNode previous;

private DoubleNode next;

private Item element;

/**

* Creates an empty double node

*/

public DoubleNode()

{

previous = null;

next = null;

element = null;

}

/**

* Creates a double node and stores specified element

* @param elem element to be stored

*/

public DoubleNode(Item elem)

{

previous = null;

next = null;

element = elem;

}

/**

* Returns the following node

* @return reference to next node

*/

public DoubleNode getNext()

{

return next;

}

/**

* Sets the following node to specified node

* @param node node set to follow this one

*/

public void setNext(DoubleNode node)

{

next = node;

}

/**

* Returns the prior node to this one

* @return reference to previous node

*/

public DoubleNode getPrev()

{

return previous;

}

/**

* Sets the previous node to specified node

* @param node node previous to this one

*/

public void setPrev(DoubleNode node)

{

previous = node;

}

/**

* Returns the element stored in this node

* @return element stores in this node

*/

public Item getElement()

{

return element;

}

/**

* Sets the element in this node

* @param elem element to be stored in this node

*/

public void setElement(Item elem)

{

element = elem;

}

} // end class

BurgessDeque.java

/**

* This program provides an implementation of the Deque interface

* and demonstrates it.

*

* @author Breanna Burgess

* @version 1.0

*/

import java.util.NoSuchElementException;

//TODO: implement.

public class BurgessDeque implements Deque

{

private int count;

private DoubleNode head;

private DoubleNode tail;

/**

* Creates a deque

*

*/

public BurgessDeque()

{

count = 0;

head = tail = null;

}

/**

* Adds one element to the front of this deque.

* @param element the element to be added to the front of the deque

*/

public void enqueueFront(Item element)

{

DoubleNode node = new DoubleNode(element);

if(isEmpty())

tail = node;

else

{

node.setNext(head);

head.setPrev(node);

}

head = node;

count++;

}

/**

* Adds one element to the back of this deque.

* @param element the element to be added to the back of the deque

*/

public void enqueueBack(Item element)

{

DoubleNode node = new DoubleNode(element);

if(isEmpty())

head = node;

else

tail.setElement(element);

tail = node;

count++;

}

/**

* Removes and returns the element at the front of this deque.

* Throws an exception if the deque is empty.

* @return the element at the front of this deque

* @throws NoSuchElementException if the deque is empty

*/

public Item dequeueFront() throws NoSuchElementException

{

if(isEmpty())

throw new NoSuchElementException("deque is empty");

Item result = head.getElement();

head = head.getNext();

count--;

if (isEmpty())

head = tail = null;

return result;

}

/**

* Removes and returns the element at the back of this deque.

* Throw an exception if the deque is empty.

* @return the element at the back of the deque.

* @throws NoSuchElementException if the deque is empty

*/

public Item dequeueBack() throws NoSuchElementException

{

if(isEmpty())

throw new NoSuchElementException("deque is empty");

Item result = tail.getElement();

tail = tail.getPrev();

count--;

if(isEmpty())

head = null;

else

tail.setNext(null);

return result;

}

/**

* Returns, without removing, the element at the front of this deque.

* Should throw an exception if the deque is empty.

* @return the first element in the deque

* @throws NoSuchElementException if the deque is empty

*/

public Item first() throws NoSuchElementException

{

if(isEmpty())

throw new NoSuchElementException("deque is empty");

return head.getElement();

}

/**

* Returns, without removing, the element at the back of this deque.

* Should throw an exception if the deque is empty.

* @return the last element in the deque

* @throws NoSuchElementException if the deque is empty

*/

public Item last() throws NoSuchElementException

{

if(isEmpty())

throw new NoSuchElementException("deque is empty");

return tail.getElement();

}

/**

* Returns true if this deque is empty and false otherwise.

* @return if deque empty

*/

public boolean isEmpty()

{

return (count == 0);

}

/**

* Returns the number of elements in this deque.

* @return the number of elements in the deque

*/

public int size()

{

return count;

}

/**

* Returns a string representation of this deque. The back element

* occurs first, and each element is separated by a space. If the

* deque is empty, returns "empty".

* @return the string representation of the deque

*/

@Override

public String toString()

{

String result;

if(isEmpty())

return "empty";

else

{

result = "";

DoubleNode current = head;

while(current != null)

{

result = result + current.getElement() + " ";

current = current.getNext();

}

}

return result;

}

/**

* Program entry point for deque.

* @param args command line arguments

*/

public static void main(String[] args)

{

BurgessDeque deque = new BurgessDeque<>();

//standard queue behavior

deque.enqueueBack(3);

deque.enqueueBack(7);

deque.enqueueBack(4);

deque.dequeueFront();

deque.enqueueBack(9);

deque.enqueueBack(8);

deque.dequeueFront();

System.out.println("size: " + deque.size());

System.out.println("contents: " + deque.toString());

//deque features

System.out.println(deque.dequeueFront());

deque.enqueueFront(1);

deque.enqueueFront(11);

deque.enqueueFront(3);

deque.enqueueFront(5);

System.out.println(deque.dequeueBack());

System.out.println(deque.dequeueBack());

System.out.println(deque.last());

deque.dequeueFront();

deque.dequeueFront();

System.out.println(deque.first());

System.out.println("size: " + deque.size());

System.out.println("contents: " + deque.toString());

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 19th International Conference Dexa 2008 Turin Italy September 2008 Proceedings Lncs 5181

Authors: Sourav S. Bhowmick ,Josef Kung ,Roland Wagner

2008th Edition

3540856536, 978-3540856535

More Books

Students also viewed these Databases questions