COMP SCI / SFWR ENG 2S03 Department of Computing and Software McMaster University Week 10: November 10 - November 14
Outline 1 Resource 2 Introduction Nodes Illustration 3 Usage Adding Finding and Removing 4 Testing Exceptions Fixtures 5 Exercises
Resource Some of the following material is adapted from: Mughal, Khalid. Java Actually : A Comprehensive Primer in Programming. Australia: Course Technology/Cenage Learning, 2008. Virginia Tech for the Linked List image
Linked lists are dynamic data structures which A. Can create new data on demand B. Have an extra field to store a reference C. Data manipulation can be done by changing references
(continued) Some of the properties of linked lists include A. List of items which are called nodes B. Have a head and a tail Head points to the first node in the list Tail points to the last node in the list C. Every node has a pointer to the next node D. Because of this, lists are not limited in size like arrays!
Nodes Nodes are what make up a linked list. They are records which contain a data element as well as a pointer to the next element in the list. If the pointer is null, that means that the node is at the tail of the list. p u b l i c c l a s s Node<E> { p r i v a t e E data ; // E can be any type e x c e p t p r i m i t i v e s p r i v a t e Node next ;.. }
- Illustration
Using Java Java has a built-in linked list implementation that we will use. Linked lists can be created using any object type. import j a v a. u t i l. L i n k e d L i s t // Do t h i s always! L i n k e d L i s t <S t r i n g > s t r i n g s = new L i n k e d L i s t <S t r i n g >(); L i n k e d L i s t <I n t e g e r > i n t s = new L i n k e d L i s t <I n t e g e r >();
- Adding You can add elements to a linked list by A. Appending to the tail or B. Adding to a specific index // add (E element ) // add ( i n t index, E element ) l i s t. add ( " Hello, " ) ; l i s t. add ( 1, " world! " ) ;
- Finding and Removing When we want to remove and item from a linked list, we must first find its index value. // indexof ( Object o ) // remove ( i n t i n d e x ) l i s t. add ( " Hello, world! " ) ; l i s t. remove ( l i s t. indexof ( " Hello, world! " ) ) ; // L i s t i s now empty!
Testing for Exceptions Using JUnit you can test for an exception being thrown by your code such as A. IndexOutOfBoundsException B. IOException C. ArithmeticException
Testing for Exceptions - Example @Test ( e x p e c t e d= IndexOutOfBoundsException. c l a s s ) p u b l i c void empty ( ) { new L i n k e d L i s t <I n t e g e r >(). get ( 0 ) ; }
Testing Fixtures Using JUnit, you can also specify the state of your program pre-class or pre-test A. @BeforeClass B. @AfterClass C. @Before D. @After
Testing Fixtures - Example @ B e f o r e C l a s s p u b l i c s t a t i c void s e t u p ( ) { Point t e s t P o i n t = new Point ( 0, 5 ) ; i n t t e s t V a l u e = 5 ; }
Exercises There are many methods that are provided by Java for linked lists. You can find them online at http://docs.oracle.com/javase/7/docs/api/java/ util/linkedlist.html In the exercises that follow, be sure to refer to the documentation for any methods not mentioned here!