Programming Question 2.
Chap. 4 No. 3 (25%) 313
Part (a): Here is skeleton code that you may wish to use to help you to create the method deleteSmallest for this exercise.
public **** deleteSmallest()
{
LinkedListNode current; //variable to traverse the list
************** trailCurrent; //variable just before current
************** small;
LinkedListNode trailSmall = null;
if(first == **** )
System.err.println("Can not delete from an empty list.");
else
if(first.**** == null)
{
first = null;
last = ****;
}
else
{
small = first;
trailCurrent = first;
current = first.****;
while(current ** null)
{
if(small.****.compareTo(current.****) > 0)
{
trailSmall = trailCurrent;
small = *******;
}
trailCurrent = current;
current = *******.link;
}
if(small == first)
first = first.****;
else
{
trailSmall.link = small.****;
if(small == last)
last = **********;
}
}
}//end deleteSmallest
Here is skeleton code that you may wish to use to help you to create the program to test the method deleteSmallest.
// import the package java.io.*
// import the package java.util.*
****** ***** TestDeleteSmallest
{
static BufferedReader keyboard = new
**************(new InputStreamReader(******.in));
public static void main(String[] args) throws IOException
{
UnorderedLinkedList list
= *** *******************();
IntElement num = *** IntElement();
StringTokenizer tokenizer;
System.out.println("Enter integers ending with -999 on one line.");
tokenizer = new ***************(keyboard.********());
num.setNum(Integer.parseInt(tokenizer.nextToken()));
*****(num.getNum() != -999)
{
list.insertLast(***);
***.setNum(
Integer.********(*********.nextToken()));
}
System.out.println();
System.out.println("list: ");
****.print();
System.out.println();
System.out.println("Length of list: " + ****.******());
// delete the smallest
****.**************();
System.out.println("After deleting the smallest element, list:");
list.*****();
System.out.println();
System.out.println("Length of list: " + ****.******());
}
}
Part (b): Here is skeleton code that you may wish to use to help you to create the method deleteAll for this exercise.
public void deleteAll(DataElement deleteItem)
{
LinkedListNode current; //variable to traverse the list
************** trailCurrent = null; //variable just before current
**(first ** null) //Case 1; list is empty.
System.err.println("Can not delete from an empty list.");
else
{
current = first;
while(current != ****)
{
if(current.****.equals(**********))
{
if(current ** first)
{
first = first.****;
current = *****;
if(first == null)
last * null;
}
else
{
trailCurrent.link = current.****;
if(current ** last)
{
last = trailCurrent;
}
current = trailCurrent.****;
}
}
else
{
trailCurrent = *******;
current = current.****;
}
} //end while
}
} //end deleteAll
Adapt the code that you wrote to test the method deleteSmallest, so that you test the method deleteAll.
Programming Question 3.
Chap. 5 No. 1 (25%) 357
Homework Week 8 Ends