Linked Lists
Objective #1: Understand the java.util.List interface and some of its methods.
Objective #2: Use linked lists from the standard library (java.util.LinkedList).
Objective #3: Use iterators to traverse linked lists.
Objective #4: Implement linked lists.
Objective #5: Distinguish between abstract and concrete data types.
Objective #7: Use stacks.
import java.util.Stack;
public class Ch19DemoStack
{
public static void main(String[] args)
{
Stack<String> myFriends = new Stack<String>();
myFriends.push("Britney");
myFriends.push("Paris");
myFriends.push("Hannah");
myFriends.push("Fergi");
myFriends.push("Lindsay");
myFriends.pop();
if (!myFriends.isEmpty())
{
System.out.println(myFriends.peek());
}
System.out.println(myFriends.get(2));
// don't do this since using the get method would
// violate the abstract data structure definition of a stack
// even though the Stack class inherits the get method
}
}
Objective #8: Use queues.
import java.util.LinkedList;
import java.util.Queue;
public class Ch19DemoQueue
{
public static void main(String[] args)
{
Queue<String> myFriends = new LinkedList<String>();
myFriends.add("Britney");
myFriends.add("Paris");
myFriends.add("Hannah");
myFriends.add("Fergi");
myFriends.add("Lindsay");
myFriends.remove();
if (!myFriends.isEmpty())
{
System.out.println(myFriends.peek());
}
System.out.println(myFriends.get(2));
// don't use the get method since it would
// violate the abstract data structure definition of a queue
}
}