Section 16.11 Exercises
Subsection 16.11.1
Note: For programming exercises, first draw a UML class diagram describing all classes and their inheritance relationships and/or associations.
- Explain the difference between each of the following pairs of terms:
- Stack and queue.
- Static structure and dynamic structure.
- Data structure and Abstract Data Type.
- Push and pop.
- Enqueue and dequeue.
- Linked list and node.
- Fill in the blanks.
- An Abstract Data Type consists of two main parts:
__________
and__________
. - An object that contains a variable that refers to an object of the same class is a
__________
. - One application for a
__________
is to manage the method call and returns in a computer program. - One application for a
__________
is to balance the parentheses in an arithmetic expression. - A
__________
operation is one that starts at the beginning of a list and processes each element. - A vector is an example of a
__________
data structure. - An array is an example of a
__________
data structure. - By default, the initial value of a reference variable is
__________
.
- Add a
removeAt()
method to theList
class to return the object at a certain index location in the list. This method should take anint
parameter, specifying the object’s position in the list, and it should return anObject
. - Add an
insertAt()
method to theList
class that will insert an object at a certain position in the list. This method should take two parameters, theObject
to be inserted, and anint
to designate where to insert it. It should return aboolean
to indicate whether the insertion was successful. - Add a
removeAll()
method to theList
class. Thisvoid
method should remove all the members of the list. - Write an
int
method namedsize()
that returns the number of elements in aList
. - Write an
boolean
method namedcontains(Object o)
that returnstrue
if itsObject
parameter is contained in the list. - The head of a list is the first element in the list. The tail of a list consists of all the elements except the head. Write a method named
tail()
that returns a reference to the tail of the list. Its return value should beNode
. - Write a program that uses the
List
ADT to store a list of 100 random floating-point numbers. Write methods to calculate the average of the numbers. - Write a program that uses the
List
ADT to store a list ofStudent
records, using a variation of the Student class defined in Chapter 11. Write a method to calculate the mean grade point average for all students in the list. - Write a program that creates a copy of a
List
. It is necessary to copy each node of the list. This will require that you create new nodes that are copies of the nodes in the original list. To simplify this task, define a copy constructor for your node class and then use that to make copies of each node of the list. - Write a program that uses a
Stack
ADT to determine if a string is a palindrome—spelled the same way backward and forward. - Design and write a program that uses a
Stack
to determine whether a parenthesized expression is well-formed. Such an expression is well formed only if there is a closing parenthesis for each opening parenthesis. - Design and write a program that uses
Stack
s to determine whether an expression involving both parentheses and square brackets is well formed. - Write a program that links two lists together, appending the second list to the end of the first list.
- Design a
Stack
class that uses aArrayList
instead of a linked list to store its elements. This is the way Java’sStack
class is defined. - Design a
Queue
class that uses aArrayList
instead of a linked list to store its elements. - Write a program that uses the
List<E>
andLinkedList<E>
classes to store a list ofStudent
records, using a variation of theStudent
class defined in Chapter 11. Write a method to calculate the mean grade point average for all students in the list. - Write an implementation of the
insert()
method of thePhoneTree
class described at the end of this chapter. - Write an implementation of the
insert()
method of thePhoneTreeNode
class described at the end of this chapter. - Challenge: Design a
List
class, similar in functionality to the one we designed in this chapter, that uses an array to store the list’s elements. Set it up so that the middle of the array is where the first element is placed. That way you can still insert at both the front and rear of the list. One limitation of this approach is that, unlike a linked list, an array has a fixed size. Allow the user to set the initial size of the array in a constructor, but if the array becomes full, don’t allow any further insertions. - Challenge: Add a method to the program in the previous exercise that lets the user increase the size of the array used to store the list.
- Challenge: Recursion is a useful technique for list processing. Write recursive versions of the
print()
method and the lookup-by-name method for thePhoneList
. (Hint: The base case in processing a list is the empty list. The recursive case should handle the head of the list and then recurse on the tail of the list. The tail of the list is everything but the first element.) Challenge: Design anOrderedList
class. An ordered list is one that keeps its elements in order. For example, if it’s an ordered list of integers, then the first integer is less than or equal to the second, the second is less than or equal to the third, and so on. If it’s an ordered list of employees, then perhaps the employees are stored in order according to their social security numbers. TheOrderedList
class should contain aninsert(Object o)
method that inserts its object in the proper order. One major challenge in this project is designing your class so that it will work for any kind of object. (Hint: Define anOrderable
interface that defines an abstractprecedes()
method. Then define a subclass ofNode
that implementsOrderable
. This will let you compare any twoNode
s to see which one comes before the other.)
You have attempted of activities on this page.