Java- My toString is not allowing it to print the whole deque. It is only printing two elements of the deque. I need ALL

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Java- My toString is not allowing it to print the whole deque. It is only printing two elements of the deque. I need ALL

Post by answerhappygod »

Java- My toString is not allowing it to print
the whole deque. It is only printing two elements of the deque. I
need ALL elements to be printed when I call the
system.out.println(deque). Thanks in
advance.
copyable code:
public class LinkedDeque<T> implements
DequeADT<T>
{
private int size=0;
private LinearNode<T> head, tail;

public LinkedDeque(int n)
{
size = 0;
head = tail = null;
}
public void enqueueFront(T element)
{
LinearNode<T> node= new LinearNode<T>(element);

if(isEmpty())
tail=head=node;
else
head.setNext(node);

head=node;
size++;
}

public void enqueueRear(T element)
{
LinearNode<T> node=new LinearNode<T>(element);

if(isEmpty())
head=tail=node;
else
tail.setNext(node);

tail=node;
size++;
}
public T dequeueRear() throws EmptyCollectionException
{
if (isEmpty())
throw new EmptyCollectionException("deque");
T result =tail.getElement();
tail=tail.getNext();

size--;

if(isEmpty())
head=null;

return result;
}

public T dequeueFront() throws EmptyCollectionException
{
if(isEmpty())
throw new EmptyCollectionException("deque");

T result=head.getElement();
head=head.getNext();
size--;

if (isEmpty())
tail=null;

return result;
}

public T getFront() throws EmptyCollectionException
{
if (isEmpty())
throw new EmptyCollectionException("deque");
return head.getElement();
}
public boolean isEmpty()
{
return (size == 0);
}
public int size()
{
return size;
}

public T getRear() throws EmptyCollectionException
{
if(isEmpty())
throw new EmptyCollectionException("deque");

return tail.getElement();
}

public void clear()
{
tail=null;
while(head != null)
{
T result=head.getElement();
head=head.getNext();
}
size=0;
}
public String toString()
{
String result = "";
LinearNode<T> current=head=tail;
while (current != null)
{
result = result += current.getElement() + "\n";
current = current.getNext();
}
return result;
}
}
public class LinearNode<T>
{
private LinearNode<T> next;
private T element;
public LinearNode()
{
next = null;
element = null;
}
public LinearNode(T elem)
{
next = null;
element = elem;
}
public LinearNode<T> getNext()
{
return next;
}
public void setNext(LinearNode<T> node)
{
next = node;
}
public T getElement()
{
return element;
}
public void setElement(T elem)
{
element = elem;
}
}
driver:
public class DequeDriver
{
public static void main(String[]args)
{
LinkedDeque deque= new LinkedDeque(6);
System.out.println("--Inserting an element at front of deque:
5--" );
deque.enqueueFront(5);
System.out.println(deque);
System.out.println("Size of the deque: "+ deque.size()+ "\nFirst
element: " + deque.getFront() + "\nLast element: " +
deque.getRear());



System.out.println();
//removing elements from deque
System.out.println("--Removing an element at rear of deque:
10--");
deque.dequeueRear();
System.out.println(deque);
System.out.println("Size of the deque: "+ deque.size()+ "\nFirst
element: " + deque.getFront() + "\nLast element: " +
deque.getRear());
System.out.println("\n");
System.out.println("--Removing an element at front of deque:
3--");
deque.dequeueFront();
System.out.println(deque);
System.out.println("Size of the deque: "+ deque.size()+ "\nFirst
element: " + deque.getFront() + "\nLast element: " +
deque.getRear());

System.out.println();
System.out.println("--Removing an element at rear of deque:
2--");
deque.dequeueRear();
System.out.println(deque);
System.out.println("Size of the deque: "+ deque.size()+ "\nFirst
element: " + deque.getFront() + "\nLast element: " +
deque.getRear());
System.out.println("\n");
System.out.println("--Removing an element at front of deque: 7--"
);
deque.dequeueFront();
System.out.println(deque);
System.out.println("Size of the deque: "+ deque.size()+ "\nFirst
element: " + deque.getFront() + "\nLast element: " +
deque.getRear());
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply