lab5 - Lab 5: Additional Data Structures: Exercise 1:...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Page 1 of 5 Lab 5: Additional Data Structures: Exercise 1: Linked List Demo #1 The following program shows how to construct a Linked List and illustrates the add , addLast and remove , contains and size methods. a. Compile, then run the program. Study how it works and confirm the output is what you expect. Notice how the linked list is printed using only the println() method. b. Adjust the program so it constructs and prints out the following two linked lists. primeNumbers = [2 3 5 7 11 13] and fibonacciNumbers = [2 3 5 8 13 21] c. You can see some numbers are in both linked lists. Using a "for each" loop and the contains() method to create a new linked list which contains numbers which are in both the lists above. That is – you are creating the intersection . Print out the following message using the size() method to compute the 4. The intersection contains 4 elements: [2, 3, 5, 13] LinkedListExample1.java import java.util.LinkedList; public class LinkedListExample1 { public static void main(String[] args) { // 1. Create LinkedList object named "myLinkedList" LinkedList<String> myLinkedList = new LinkedList<String>(); // 2. add elements to LinkedList myLinkedList.add("1"); myLinkedList.add("2"); myLinkedList.add("3"); myLinkedList.add("4"); myLinkedList.addFirst("5"); myLinkedList.remove("4"); System.out.println("LinkedList contains : " + myLinkedList); } // end of method main } // end of class
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Page 2 of 5 Exercise 2: Linked List Demo #2 The following program explores how to search a Linked List and how to construct a linked list from an array. New methods include indexOf() and lastIndexOf(). LinkedListExample2.java
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/22/2009 for the course ECEC 301 taught by Professor Carr during the Spring '09 term at Drexel.

Page1 / 5

lab5 - Lab 5: Additional Data Structures: Exercise 1:...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online