AP Computer Science
> Labs 10-12
Data Structures – LinkedList, Queue, Stack
This series of labs and the next will investigate, use and/or
Lists (sequential elements) | {5,8,10,3,7,8,3,10} |
Sets (unique elements) | {5, 8, 10, 3, 7} |
Maps (associated pairs) | (1,2), (2,5), (3,2) } first elements form
a set |
Trees (branching hierarchy) |
|
Hash Table (keyed access) |
These are general names and are referred to as ADT’s (abstract data types).
Each one has at least one implementation in the Java.util package. We will also implement very specialized data
types using the built-in types. The
types of lists that you need to know are listed here:
List -random access (indexed), implemented as
arrays – may contain primitive data types
2-d arrays – may contain primitive
data types
ArrayList – must contain objects
List – linked, implemented as a doubly linked list
LinkedList – must contain objects
ListNode – AP class for the exam
(BP p. 119), creating linked lists from scratch
List - special access
queue – FIFO – we will implement with the ArrayList class (BP
p. 124-5)
stack – FILO – we will implement with the ArrayList class (BP
p. 123)
priority queue – two implementations
ArrayPriorityQueue
HeapPriorityQueue
The context
For all the data structure assignments we will be
Lab 10 – storing and reading in the data
Write the class Song as a data storage unit. It should have the following fields
private String itsTitle; // the name of the song
private String itsArtist;
// the artist or group that performs the song
private int itsTrack;
// the track number on itsCD
private String itsCD;
// the name of the CD that contains this song
Also include accessors for each instance variable as well as a 4-param constructor
Write the class CD.
It should have private instance variables
private LinkedList itsContents; // holds a list of the Song’s on the CD
private String itsName; // the name of the CD
The CD class should also have a printContents. Use a ListIterator to traverse itsContents.
Note: The CD does NOT know its own author.
Finally write a class, CDReader, that will use the
Name of CD
Artist
number of songs
Song title
Song title
Song title
….
Declare a CD. For each CD, read in
the name, artist and number of songs.
In a loop, create a song with a constructor that has all four
Finally, write an application program (Lab10) that uses the above
Lab 11
Copy the implementation of the Queue class from here
(taken from Be Prepared, Litvin, p. 124) into a new
Queue class.
Write an application program (Lab11) that reads your data file
These are songs that have been requested and must be played
Extra Credit – since it is inefficient to use random access
(like we do in the choosing of
the song above in a LinkedList),
use the toArray() method to make the list faster
to access.
Lab 12
Copy the implementation of the Stack class from here
(taken from Be Prepared, Litvin, p. 124) into a new
Stack class.
Copy your Lab11 and rename it as Lab12. Revise the program
“Play the songs” back by popping all the entries.