TestLinkedList.java
Select all
/* TestLinkedList.java David Klick CIS 260 3/23/09 This program demonstrates a few features of the LinkedList class. */ import java.util.*; public class TestLinkedList { public static void main(String[] args) { LinkedList
llist = new LinkedList
(); int n; // you can add and remove items dynamically System.out.println("Adding: 17, 26, -8, 142"); llist.add(17); llist.add(26); llist.add(-8); llist.add(142); for (int i : llist) System.out.print(i + " "); System.out.println("\nRemoving item at index 1"); llist.remove(1); // removes item at index 1 for (int i : llist) System.out.print(i + " "); System.out.println("\nFirst: " + llist.getFirst()); System.out.println("Last: " + llist.getLast()); System.out.println("Peeking at first: " + llist.peekFirst()); System.out.println("Peeking at last: " + llist.peekLast()); System.out.println("Popping first item: " + (n = llist.pop())); for (int i : llist) System.out.print(i + " "); System.out.println("\nPushing first item: " + n); llist.push(n); for (int i : llist) System.out.print(i + " "); System.out.println("\nRemoving first: " + llist.removeFirst()); System.out.println("Removing last: " + llist.removeLast()); for (int i : llist) System.out.print(i + " "); System.out.println("\nAdding first: " + -75); llist.addFirst(-75); System.out.println("Adding last: " + 32); llist.addLast(32); for (int i : llist) System.out.print(i + " "); // you can also out a LinkedList System.out.println("\nClearing linked list"); llist.clear(); System.out.println("isEmpty(): " + llist.isEmpty()); } } /* Output: Adding: 17, 26, -8, 142 17 26 -8 142 Removing item at index 1 17 -8 142 First: 17 Last: 142 Peeking at first: 17 Peeking at last: 142 Popping first item: 17 -8 142 Pushing first item: 17 17 -8 142 Removing first: 17 Removing last: 142 -8 Adding first: -75 Adding last: 32 -75 -8 32 Clearing linked list isEmpty(): true */