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
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 ////////////////////////////////////////////////////////////////
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started