USING JAVA ONLY - USING THE CODE BELOW- Write a method public Link last() that c
ID: 3673830 • Letter: U
Question
USING JAVA ONLY - USING THE CODE BELOW- Write a method public Link last() that could be added to LinkQueue: Return the last data value in the queue; return null if the queue is empty.
USING JAVA ONLY - USING THE CODE BELOW-Write a method public int size() that could be added to LinkQueue: Return the number of elements in the queue.
USING JAVA ONLY - USING THE CODE BELOW- Write a method public void append (LinkQueue queue) that could be in LinkQueue: The executor appends queue's elements in the same order and sets queue to be empty. Precondition: The executor is not empty.
// linkQueue.java
// demonstrates queue implemented as double-ended list
// to run this program: C>java LinkQueueApp
////////////////////////////////////////////////////////////////
class Link
{
public long dData; // data item
public Link next; // next link in list
// -------------------------------------------------------------
public Link(long d) // constructor
{ dData = d; }
// -------------------------------------------------------------
public void displayLink() // display this link
{ System.out.print(dData + " "); }
// -------------------------------------------------------------
} // end class Link
////////////////////////////////////////////////////////////////
class FirstLastList
{
private Link first; // ref to first item
private Link last; // ref to last item
// -------------------------------------------------------------
public FirstLastList() // constructor
{
first = null; // no items on list yet
last = null;
}
// -------------------------------------------------------------
public boolean isEmpty() // true if no links
{ return first==null; }
// -------------------------------------------------------------
public void insertLast(long dd) // insert at end of list
{
Link newLink = new Link(dd); // make new link
if( isEmpty() ) // if empty list,
first = newLink; // first --> newLink
else
last.next = newLink; // old last --> newLink
last = newLink; // newLink <-- last
}
// -------------------------------------------------------------
public long deleteFirst() // delete first link
{ // (assumes non-empty list)
long temp = first.dData;
if(first.next == null) // if only one item
last = null; // null <-- last
first = first.next; // first --> old next
return temp;
}
// -------------------------------------------------------------
public void displayList()
{
Link current = first; // start at beginning
while(current != null) // until end of list,
{
current.displayLink(); // print data
current = current.next; // move to next link
}
System.out.println("");
}
// -------------------------------------------------------------
} // end class FirstLastList
////////////////////////////////////////////////////////////////
class LinkQueue
{
private FirstLastList theList;
//--------------------------------------------------------------
public LinkQueue() // constructor
{ theList = new FirstLastList(); } // make a 2-ended list
//--------------------------------------------------------------
public boolean isEmpty() // true if queue is empty
{ return theList.isEmpty(); }
//--------------------------------------------------------------
public void insert(long j) // insert, rear of queue
{ theList.insertLast(j); }
//--------------------------------------------------------------
public long remove() // remove, front of queue
{ return theList.deleteFirst(); }
//--------------------------------------------------------------
public void displayQueue()
{
System.out.print("Queue (front-->rear): ");
theList.displayList();
}
//--------------------------------------------------------------
} // end class LinkQueue
////////////////////////////////////////////////////////////////
class LinkQueueApp
{
public static void main(String[] args)
{
LinkQueue theQueue = new LinkQueue();
theQueue.insert(20); // insert items
theQueue.insert(40);
theQueue.displayQueue(); // display queue
theQueue.insert(60); // insert items
theQueue.insert(80);
theQueue.displayQueue(); // display queue
theQueue.remove(); // remove items
theQueue.remove();
theQueue.displayQueue(); // display queue
} // end main()
} // end class LinkQueueApp
////////////////////////////////////////////////////////////////
Explanation / Answer
class Link
{
public long dData; // data item
public Link next; // next link in list
// -------------------------------------------------------------
public Link(long d) // constructor
{ dData = d; }
// -------------------------------------------------------------
public void displayLink() // display this link
{ System.out.print(dData + " "); }
// -------------------------------------------------------------
} // end class Link
////////////////////////////////////////////////////////////////
class FirstLastList
{
private Link first; // ref to first item
private Link last; // ref to last item
// -------------------------------------------------------------
public FirstLastList() // constructor
{
first = null; // no items on list yet
last = null;
}
// -------------------------------------------------------------
public boolean isEmpty() // true if no links
{ return first==null; }
// -------------------------------------------------------------
public void insertLast(long dd) // insert at end of list
{
Link newLink = new Link(dd); // make new link
if( isEmpty() ) // if empty list,
first = newLink; // first --> newLink
else
last.next = newLink; // old last --> newLink
last = newLink; // newLink <-- last
}
// -------------------------------------------------------------
public long deleteFirst() // delete first link
{ // (assumes non-empty list)
long temp = first.dData;
if(first.next == null) // if only one item
last = null; // null <-- last
first = first.next; // first --> old next
return temp;
}
// -------------------------------------------------------------
public void displayList()
{
Link current = first; // start at beginning
while(current != null) // until end of list,
{
current.displayLink(); // print data
current = current.next; // move to next link
}
System.out.println("");
}
// -------------------------------------------------------------
} // end class FirstLastList
////////////////////////////////////////////////////////////////
class LinkQueue
{
private FirstLastList theList;
//--------------------------------------------------------------
public LinkQueue() // constructor
{ theList = new FirstLastList(); } // make a 2-ended list
//--------------------------------------------------------------
public boolean isEmpty() // true if queue is empty
{ return theList.isEmpty(); }
//--------------------------------------------------------------
public void insert(long j) // insert, rear of queue
{ theList.insertLast(j); }
//--------------------------------------------------------------
public long remove() // remove, front of queue
{ return theList.deleteFirst(); }
//--------------------------------------------------------------
public void displayQueue()
{
System.out.print("Queue (front-->rear): ");
theList.displayList();
public Liink Last()
{
if(theList.isEmpty())
throw new IllegalStateExceptions("nothing");
else
return theList.Last();
}
public int size()
{
if(theList.isEmpty())
return 0;
else
return next.size();
}
public void append (LinkQueue queue)
{
if(theList.isEmpty())
theList.isEmpty();
else
next.insertInOrder(queue);
}
}
//--------------------------------------------------------------
} // end class LinkQueue
////////////////////////////////////////////////////////////////
class LinkQueueApp
{
public static void main(String[] args)
{
LinkQueue theQueue = new LinkQueue();
theQueue.insert(20); // insert items
theQueue.insert(40);
theQueue.displayQueue(); // display queue
theQueue.insert(60); // insert items
theQueue.insert(80);
theQueue.displayQueue(); // display queue
theQueue.remove(); // remove items
theQueue.remove();
theQueue.displayQueue(); // display queue
theQueue.Last();
int n=theQueue.size();
system.out.println("the size of queue is:"+n);
theQueue.append(queue);
} // end main()
} // end class LinkQueueApp
////////////////////////////////////////////////////////////////
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.