List ADT. B/W Confirming Pages

Size: px
Start display at page:

Download "List ADT. B/W Confirming Pages"

Transcription

1 wu3399_ch8.qxd //7 :37 Page 98 8 List ADT O b j e c t i v e s After you have read and studied this chapter, you should be able to Describe the key features of the List ADT. the List ADT using an array and Implement linked list. Describe and implement the iterator pattern. the key differences between the array Explain and linked implementations of the List ADT. 98

2 wu3399_ch8.qxd //7 :37 Page Chapter 8 List ADT I n t r o d u c t i o n We introduced the concept of linked lists in Chapter 6 and provided the fundamentals of manipulating links. And, in Chapter 7, we introduced the generics feature of Java 5. and used it in defining the SimpleLinkedList class that provides a higher-level abtraction to the client programmers. Using a SimpleLinkedList, the client programmers do not have to deal directly with the linked nodes. Instead, they deal strictly with list objects. The fact that the linked nodes are used in implementation is hidden from the client programmers. This embodies the software engineering principle of information hiding. In this chapter, we will expand this concept further by introducing an abstract data type (ADT). ADTs are reusable software components that support reliable and flexible software construction. abstract data An abstract data type (ADT) is a mathematical specification of a set of data type (ADT) and the corresponding set of operations performed on those data. The key point is that an ADT does not specify how the set of data is actually represented in memory or how the set of operations is implemented. Nice examples of ADTs can be found in the Java Collections Framework. When we look in the java.util package, for example, we see the Java interface List and two classes that implement the List interface. The List interface represents the List ADT, and the two classes ArrayList and LinkedList implement the List ADT. The ArrayList uses an array, and the LinkedList uses linked nodes to implement the List ADT, respectively. To study how the ADT is defined and implemented, we will create our own List ADT and provide two implementations in this chapter which we model after the java.util.list interface and its two implementations. Ours is a much simplified version of those in the java.util package. In Chapters 9 and, we will study the Stack and Queue ADTs, respectively. list ADT 8. The List ADT A list is a linearly ordered collection of elements. Figure 8. shows a sample list, named samplelist, with five elements and another list, named, with five three-letter animal names (String objects). Mathematically, we can designate samplelist as samplelist = ( L o, L, L, L 3, L 4 ) linear ordering duplicate elements Elements in a list are said to be linearly ordered. This means L comes before L and L comes before L, and so forth. For the sample list, the first element is L and the last element is L 4. Note that we are using the zero-based indexing. Elements in a list do not have to be distinct; i.e., duplicate elements are

3 wu3399_ch8.qxd //7 :37 Page The List ADT 983 This value indicates the position of an element in the list. samplelist 3 4 L L L L 3 L 4 This line indicates that the list elements are connected in a linear sequence. 3 4 "dog" "dog" "eel" Notice that the list elements do not have to be unique. Figure 8. A generic list named samplelist with five elements and a list of three-letter animals, named. null elements add allowed. Two objects o and o are considered duplicates if o.equals(o) is true. In the sample list, we see that L and L 3 are duplicates. A list also allows multiple null elements. We will discuss more about the impact of duplicate and null elements in the implementation sections. In the remainder of this section, we will describe the operations of the List ADT. The add operation adds a new element to a list. There are two versions: The first one adds a new element at the end of the list. The second one adds a new element at the designated position in the list. For the second version, all elements in that and higher positions will be shifted to a position one index higher; that is, an element at position i will be shifted to position i. If the given position is a negative value, or larger than the current size of the list, then the index out-of-bound exception will be thrown. Figure 8. illustrates the second version of the add operation.

4 wu3399_ch8.qxd //7 :37 Page Chapter 8 List ADT Before "dog" 3 add(, gnu ) After 3 4 "gnu" "dog" Before "dog" 3 add(5, "gnu") throws <index-out-of-bounds-exception> After "dog" 3 No structural change to the list Figure 8. Sample version add operations on. clear contains get The clear operation removes all elements in the list, which results in an empty list. Figure 8.3 illustrates the clear operation. The contains operation returns true if a specified element is included in the list. Otherwise, false is returned. Notice that there could be duplicate objects. The search will stop immediately after the first match, and true is returned. Figure 8.4 illustrates the contains operation. The get operation returns the element stored at the designated position. If the given position is outside of a valid range less than or greater than or equal to the size of the list an index out-of-bound exception is thrown. The get operation is a read-only operation. It does not remove the element, so the list remains the same after the get operation. Figure 8.5 illustrates the get operation.

5 wu3399_ch8.qxd //7 :37 Page The List ADT 985 Before "dog" 3 clear( ) After Figure 8.3 The effect of the clear operation on the list. Before "dog" 3 contains("gnu") returns false After "dog" 3 No structural change to the list Before "dog" 3 contains() returns true After "dog" 3 No structural change to the list Figure 8.4 Sample contains operations on the list.

6 wu3399_ch8.qxd //7 :37 Page Chapter 8 List ADT Before "dog" 3 get() returns dog After "dog" 3 No structural change to the list Before "dog" 3 get(4) throws <index-out-of-bounds-exception> After "dog" 3 No structural change to the list Figure 8.5 Sample get operations on the list. indexof isempty remove The indexof operation returns the position of the specified object in the list. If the specified object is not in the list, then NOT_FOUND ( ) is returned. If there are duplicates, then the position of the first match is returned. Figure 8.6 illustrates the indexof operation. The isempty operation returns true if the list is empty. Otherwise, false is returned. Figure 8.7 illustrates the isempty operation. There are two versions of the remove operation. In the first version, we specify the element to remove by passing its position; in the second version, we specify the element to remove by passing the element itself. The first version returns the element removed from the list. It throws an index out-of-bound exception if the index i is outside the valid range of and the size of the list minus, that is, i size.

7 wu3399_ch8.qxd //7 :37 Page The List ADT 987 Before "dog" 3 indexof("gnu") returns NOT_FOUND ( ) After "dog" 3 No structural change to the list Before "dog" 3 indexof("dog") returns After "dog" 3 No structural change to the list Figure 8.6 Sample indexof operations on the list. set size The second version removes the element and returns true. If the designated element is not found, then the list remains the same and false is returned. If any duplicates exist, then the first element in the list is removed. In both versions, if an element is successfully removed, then all elements in the position higher than the position of the removed element will be shifted to a position one index lower; that is, elements at position i will be shifted to position i. Figure 8.8 illustrates both types of removal operations. The set operation replaces the element at the designated position with the given element. If the operation is successful, then the operation returns the element previously stored in the designated position. If the designated position is outside of a valid range, then an index out-of-bound exception is thrown. Figure 8.9 illustrates the set operations. The size operation returns the size of the list. Figure 8. illustrates the size operation.

8 wu3399_ch8.qxd //7 :37 Page Chapter 8 List ADT Before "dog" 3 isempty( ) returns false After "dog" 3 No structural change to the list Before isempty( ) returns true After No structural change to the list Figure 8.7 Sample isempty operations on the list. 8. The List Interface We will use a Java interface to define the List ADT. To avoid confusion with the java.util classes, we will prefix the name of the interfaces and classes we define here with NPS (NPS stands for the author s affiliation, Naval Postgraduate School, in Monterey, California). In the NPSList interface definition, we use the generics feature introduced in Chapter 7. Formalizing what we have presented in Section 8., we have the following definition, and Table 8. summarizes the methods: NPSList package edu.nps.util; interface NPSList<E> { public void add(e item); public void add(int index, E item) throws IndexOutOfBoundsException;

9 wu3399_ch8.qxd //7 :37 Page The List Interface 989 Before "gnu" 3 "dog" 4 remove() returns gnu After "dog" 3 Before "gnu" 3 "dog" 4 remove( gnu ) returns true After "dog" 3 Figure 8.8 Sample remove operations on the list. public void clear( ); public boolean contains(e item); public E get(int index) throws IndexOutOfBoundsException; public int indexof(e item); public boolean isempty( ); public E remove(int index)throws IndexOutOfBoundsException; public boolean remove(e item); public E set(int index, E item) throws IndexOutOfBoundsException; public int size( );

10 wu3399_ch8.qxd //7 :37 Page Chapter 8 List ADT Before "dog" 3 set(, "fox") returns ape After "fox" "dog" 3 Before "dog" 3 set(4, "fox") throws <index-out-of-bounds-exception> After "dog" 3 No structural change to the list Figure 8.9 Sample set operations on the list. Before "dog" 3 size( ) returns 4 After "dog" 3 No structural change to the list Figure 8. A sample size operation on the list.

11 wu3399_ch8.qxd //7 :37 Page The List Interface 99 Table 8. The public methods of the list interface. The identifier E is the parameterized type that will be replaced by the actual type such as String and Person Table Interface: NPSList void add( E item ) Adds the item to the end of the list. void add( int index, E item ) throws IndexOutOfBoundsException Adds the item at the specified index position. If the value for index is less than or larger than the size of the list, an IndexOutOfBoundsException is thrown. void clear( ) Empties the list. After the clear method is called, the method isempty will return true. boolean contains( E item ) Returns true if the item is in the list. Otherwise, returns false.if the list is empty, the result is false. E get( int index ) throws IndexOutOfBoundsException Returns the item at the specified index position. Notice that the item is not removed from the list. int indexof( E item ) Returns the index position of the specified item in the list.the value NOT_FOUND (-) is returned when the item is not in the list. boolean isempty( ) Returns true if the list is empty. Otherwise, returns false. E remove( int index ) throws IndexOutOfBoundsException Removes the item at the index position from the list. If the specified index value is less than or greater than size( )-, an IndexOutOfBoundsException is thrown. boolean remove( E item ) Removes the item from the list and returns true.returns false if the item is not found. void set( int index, E item ) throws IndexOutOfBoundsException Replaces the item at the index position with the passed item. If the index is less than or greater than size( ) -, an IndexOutOfBoundsException is thrown. int size( ) Returns the number of items in the list.

12 wu3399_ch8.qxd //7 :37 Page Chapter 8 List ADT We will define two classes NPSArrayList and NPSLinkedList in Sections 8.3 and 8.4. A typical way to declare and create an NPSList object is as follows: NPSList<Person> personlist = new NPSArrayList<Person>( ); NPSList<String> stringlist = new NPSLinkedList<String>( ); The first is a (homogeneous) list of Person objects, and the second is a list of String objects. 8.3 The Array Implementation of the List ADT There are two basic approaches in implementing the List ADT array and linkedlist implementions. In this section, we will implement the list interface by using an array. The linked-list implementation is presented in Section 8.4. For all our implementations, we will use the generics feature so the list members will be homogeneous. To illustrate the list operations with concrete examples, we will use String objects as list members in the following discussion. Figure 8. illustrates the array implementation of a list. The NPSArrayList Class We use an array to store the elements and an int variable to keep track of the number of elements currently in the list. These members are declared as private E[] element; private int count; and initialized as element = (E[]) new Object[DEFAULT_SIZE]; count = ; ADT List: = (,,, "eel" ) Array Implementation: :NPSArrayList element n- count 4 :String :String :String :String "eel" Figure 8. An array implementation of the list.

13 wu3399_ch8.qxd //7 :37 Page The Array Implementation of the List ADT 993 add in a constructor. We assume the class constant DEFAULT_SIZE is assigned some arbitrary integer. Notice that we first create an array of Object and then typecast it to an array of E, the type parameter. This is necessary because it is not allowed to create an array of generic type. Adding an element at the end of the list can be achieved very easily. We add a new element at the count position and increment the count by. The only complication is the overflow condition. The expand method is used to handle the overflow condition. We detect the overflow condition when the value for count is equal to the length of the element array. Here s the version add method: public void add(e item) { if (count == element.length) { expand( ); element[count] = item; count++; See the full source code listing at the end of this section for the expand method. Instead of implementing the first add method explicitly as shown, we can implement it by simply calling the second add method as public void add(e item) { add(count, item); because the value of count is also the index position of the next available slot in the array, i.e., the end of the list, to add an item. Now, let s see how we can implement the second version of the add method. To insert an element at position i, we must shift current elements at positions i to count- one position to the right to positions i+,..., count, respectively. If an invalid value is given for the position, then an index out-of-bound exception is thrown. Here s the method: public void add(int index, E item) throws IndexOutOfBoundsException { checkinsertposition(index, size()); if (count == element.length) { expand( ); //shift one position to the right for (int i = count; i > index; i--) { element[i] = element[i-]; element[index] = item; count++;

14 wu3399_ch8.qxd //7 :37 Page Chapter 8 List ADT The private method checkinsertposition verifies the validity of index. If it is outside the valid range, an IndexOutOfBoundsException is thrown. The method is defined as follows: private void checkinsertposition(int index) { if (index < ) { throw new IndexOutOfBoundsException( "Negative index of " + index + " is invalid"); else if (index > size()) { throw new IndexOutOfBoundsException(index + " is larger than valid upper bound" + size()); clear The size method returns the number of elements in the list (the method is defined at the end of this section). We will set element[i] to null, where i=,..., count-, sothe objects referenced by element[i] will be garbage-collected. We will then reset the count variable to. public void clear( ) { for (int i = ; i < count; i++) { element[i] = null; count = ; contains We implement this method by using the indexof method. If the indexof method returns a value other than NOT_FOUND (-), then the specified object is in the list, so the contains method returns true. If the indexof method returns NOT_FOUND, then the contains method returns false. public boolean contains(e item) { boolean result = true; int loc = indexof(item); if (loc == NOT_FOUND) { result = false; NOT_FOUND is a class constant. return result;

15 wu3399_ch8.qxd //7 :37 Page The Array Implementation of the List ADT 995 get Because we are using an array, this method is straightforward. All we have to do is to return an object at position index of the element array, after confirming that the value for index is valid. Here s the method: public E get(int index) throws IndexOutOfBoundsException { checkaccessposition(index); return element[index]; The private method checkaccessposition ensures that the given index specifies a valid position for accessing an element. If it is outside the valid range, an IndexOutOfBoundsException is thrown. Here s the method: private void checkaccessposition(int index) { if (size() == ) { throw new IndexOutOfBoundsException( "Index " + index + " is invalid. List is empty."); else if (index < ) { throw new IndexOutOfBoundsException( "Negative index of " + index + " is invalid"); else if (index > size()-) { throw new IndexOutOfBoundsException(index + " is larger than valid upper bound" + (size()-)); indexof This method scans the list from the beginning and stops when the first match is located. If the specified object is not in the list, then NOT_FOUND (-) is returned. For this method to work properly, the elements in the list must support the equals method, which we will use to locate the matching element. public int indexof(e item) { int loc = ; while (loc < count &&!element[loc].equals(item)) { loc++; if (loc == count) { loc = NOT_FOUND; return loc;

16 wu3399_ch8.qxd //7 :37 Page Chapter 8 List ADT isempty This method is also straightforward. We just check the value of the count variable. public boolean isempty( ) { return (count == ); remove After the element at position i is removed, we must shift current elements at positions i+ to count one position to the left, to positions i,..., count-, respectively. And, we decrement the count variable by. public E remove(int index) throws IndexOutOfBoundsException { checkaccessposition(index); E item = element[index]; //shift one position to the left for (int i = index; i < count; i++) { element[i] = element[i+]; element[count] = null; count--; return item; The second version of remove requires a routine to find the index of the item to be removed. Once we get the index of an object to remove, we call the first remove method to remove it. If there are any duplicates, then only the first match is removed. If the passed item is not found, then nothing happens. public boolean remove(e item) { int loc = indexof(item); if (loc == NOT_FOUND) { return false; else { remove(loc); return true; set The set method replaces an object at the specified index position with the passed object. If the index is invalid, an index out-of-bound error is thrown. If the

17 wu3399_ch8.qxd //7 :37 Page The Array Implementation of the List ADT 997 operation is successful, then the method returns the previous object in the index position. public E set(int index, E item) throws IndexOutOfBoundsException { checkaccessposition(index); E old = element[index]; element[index] = item; return old; size This method is straightforward. We simply return the value of the count variable. public int size( ) { return count; We are now ready to list the complete NPSArrayList class. In the source code listing, we do not show any javadoc comments in order to keep the listing to a manageable size. The actual source code includes the full javadoc comments. NPSArrayList package edu.nps.util; public class NPSArrayList<E> implements NPSList<E> { public static final int DEFAULT_SIZE = 5; public static final int NOT_FOUND = -; private E[] element; private int count; public NPSArrayList( ) { this(default_size); Constructors public NPSArrayList(int size) { if (size <= ) { throw new IllegalArgumentException( "Initial capacity must be positive" ); element = (E[]) new Object[size]; count = ;

18 wu3399_ch8.qxd //7 :37 Page Chapter 8 List ADT public void add(e item) { if (count == element.length) { expand( ); add element[count] = item; count++; public void add(int index, E item) throws IndexOutOfBoundsException { checkinsertposition(index); add if (count == element.length) { expand( ); //shift one position to the right for (int i = count; i > index; i--) { element[i] = element[i-]; element[index] = item; count++; public void clear( ) { clear for(int i = ; i < count; i++) { element[i] = null; count = ; public boolean contains(e item) { contains boolean result = true; int loc = indexof(item); if (loc == NOT_FOUND) { result = false; return result; public E get(int index) throws IndexOutOfBoundsException { checkaccessposition(index); get return element[index];

19 wu3399_ch8.qxd //7 :37 Page The Array Implementation of the List ADT 999 public int indexof(e item) { int loc = ; indexof while (loc < count &&!element[loc].equals(item)) { loc++; if (loc == count) { loc = NOT_FOUND; return loc; public boolean isempty( ) { isempty return (count == ); public E remove(int index) throws IndexOutOfBoundsException { checkaccessposition(index); remove E item = element[index]; //shift one position to the left for (int i = index; i < count; i++) { element[i] = element[i+]; element[count] = null; count--; return item; public boolean remove(e item) { remove int loc = indexof(item); if (loc == NOT_FOUND) { return false; else { remove(loc); return true; public E set(int index, E item) throws IndexOutOfBoundsException { checkaccessposition(index); set

20 wu3399_ch8.qxd //7 :37 Page Chapter 8 List ADT E old = element[index]; element[index] = item; return old; public int size( ) { size return count; private void checkaccessposition(int index) { checkaccessposition if (size() == ) { throw new IndexOutOfBoundsException( "Index " + index + " is invalid. List is empty."); else if (index < ) { throw new IndexOutOfBoundsException("Negative index of" + index + "is invalid"); else if (index > size()-) { throw new IndexOutOfBoundsException(index + "is larger than valid upper bound" + (size()-)); private void checkinsertposition(int index) { checkinsertposition if (index < ) { throw new IndexOutOfBoundsException( "Negative index of " + index + " is invalid"); else if (index > size()) { throw new IndexOutOfBoundsException(index + " is larger than valid upper bound" + size()); private void expand( ) { expand //create a new array whose size is 5% of //the current array int newlength = (int) (.5 * element.length); E[] temp = (E[]) new Object[newLength];

21 wu3399_ch8.qxd //7 :37 Page 8.4 The Linked-List Implementation of the List ADT //now copy the data to the new array for (int i = ; i < element.length; i++) { temp[i] = element[i]; //finally set the variable entry to point to the new array element = temp; 8.4 The Linked-List Implementation of the List ADT With the array implementation, a whole block of memory is allocated at once. When an overflow condition occurs, a new larger block of memory is allocated and the contents of the original array are copied to the new array. The original array will eventually get garbage-collected when the data member element is set to refer to the new array. Instead of allocating a large block of memory, the linked implementation allocates a small amount of memory that is just large enough for a single element. In general, this finer granularity of memory allocation leads to a more efficient use of memory. These points were discussed in detail in Chapter 6. In this section, we will describe how the linked-list implementation works. Figure 8. illustrates the linked implementation of a list. ADT List: = (,,, "eel" ) Linked-List Implementation: :NPSLinkedList tail head count 4 :String :String :String :String "eel" Figure 8. A linked-list implementation of the list.

22 wu3399_ch8.qxd //7 :37 Page Chapter 8 List ADT The ListNode Class Figure 8.3 compares the array implementation and the linked implementation. With the array implementation, these references are grouped together, occupying the contiguous memory location. Each reference is accessed by the index value of the array position. As detailed in Chapter 6, with the linked-list implementation, they are individually allocated. Since they do not occupy any contiguous memory locations, they must be linked to form a chain for us to be able access them. We define a class named ListNode to represent the pair of references. We often use the term link or pointer for the second reference that points to another list node. To distinguish the two different types of references, we use a blue arrow for the first reference and a red arrow for the link. Each node will be represented by a single ListNode object. The ListNode class has two data members, and the class is declared as follows (the generic parameter E is declared in the NPSLinkedList class): class ListNode { private E item; private ListNode next; public ListNode(E item) { this.item = item; this.next = null; 3 4 Array Implementation: :String :String :String :String "eel" Linked-List Implementation: :String :String :String :String "eel" Figure 8.3 The structural difference between the array and linked-list implementations.

23 wu3399_ch8.qxd //7 :37 Page The Linked-List Implementation of the List ADT 3 The NPSLinkedList Class Similar to the NPSArrayList class, we need an int variable to keep track of the number of elements currently in the list. In addition, we use two references: one points to the first node, and the other points to the last node. The reference to the last node allows us to add a new node at the end of the list quickly. Without it, we have to traverse the links from the head to locate the last node. These data members are declared as private ListNode head; private ListNode tail; private int count; and initialized as head = null; tail = null; count = ; add in a constructor (the actual constructor calls the clear method, which resets the list as an empty list by executing the above three statements). Let s start with the first version. There are two cases we need to consider when adding an element at the end of the list. The first case involves adding an element to an empty list. Figure 8.4 illustrates this case. Both head and tail are null when the list is empty. When a node is added to an empty list, this node is both the first and last node of the list, so both head and tail are set to point to this newly added node. This is the end case. :NPSLinkedList tail head :NPSLinkedList tail head Note: newnode is a local variable and will be erased when the method terminates. newnode count count item Before After ListNode newnode = new ListNode(item); head = newnode; tail = newnode; count++; Figure 8.4 Adding an element at the end of an empty list.

24 wu3399_ch8.qxd //7 :37 Page 4 4 Chapter 8 List ADT Before :NPSLinkedList tail head count k :NPSLinkedList tail After newnode head count k+ item ListNode newnode = new ListNode(item); tail.next = newnode; tail = newnode; count++; Figure 8.5 The general case of adding an element at the end of a list. In the general case, where the list has one or more nodes, a newly added node is added at the end, so this new added node becomes the new tail node of the list. The pointer from the tail node before the addition is set to point to the new tail node after the addition. Figure 8.5 illustrates the general case. Here s the version add method: public void add(e item) { //creates a new ListNode ListNode newnode = new ListNode(item); if (isempty()) { head = tail = newnode;

25 wu3399_ch8.qxd //7 :37 Page The Linked-List Implementation of the List ADT 5 else { tail.next = newnode; tail = newnode; count++; As was the case with the NPSArrayList class, we implemented the method explicitly to illustrate clearly the thinking process behind the operation, but we can actually implement it succinctly by calling the second version as follows: public void add(e item) { add(count, item); Now, let s see how we can implement the second version of the add method. Again, we have to handle the two cases separately. The end case occurs when the new node is added as the first element, that is, index ==.Inthis case we have to adjust the head to point to the newly added node and the link field of this new node to point to the node that was the first node before the addition. Figure 8.6 illustrates this case. In the general case, we must locate the position to insert the new node as the ith node. Unlike the array implementation, where we can locate an element at position i by simply using an indexed expression, in the case of linked implementation we must traverse the pointers in the linked fields. To insert a new node as the ith node in the list (the first node being the node), we need a pointer to the i-st node. Once we locate this node, the rest is a matter of adjusting two link fields. Figure 8.7 illustrates the general case. Here s the complete definition for the second version of the add method: public void add(int index, E item) throws IndexOutOfBoundsException { checkinsertposition(index); ListNode ptr = head; ListNode newnode = new ListNode(item); if (index == ) { //adding the new node as // the first node newnode.next = head; head = newnode; else { for (int i = ; i < index; i++) { ptr = ptr.next; newnode.next = ptr.next; ptr.next = newnode;

26 wu3399_ch8.qxd //7 :37 Page 6 6 Chapter 8 List ADT :NPSLinkedList tail head count k This is the th node. We mark its item X, so the before and after diagrams can be compared easily. Before X :NPSLinkedList tail After head count k+ newnode X item ListNode newnode = new ListNode(item); newnode.next = head; head = newnode; count++; Figure 8.6 The second version of add with index ==, i.e., adding an element as the first node in a list. //adjust tail if the new node added is //the last node in the list if (index == count) { tail = newnode; count++;

27 wu3399_ch8.qxd //7 :37 Page The Linked-List Implementation of the List ADT 7 :NPSLinkedList This is the ith node before the insertion of the new node. tail Before head count k X ptr points to the (i-)st node. :NPSLinkedList ptr tail After head count k X newnode item This is the new ith node. ListNode newnode = new ListNode(item); newnode.next = ptr.next; ptr.next = newnode; count++; Figure 8.7 The general case of adding a new node as the ith node. Notice the last if statement in the method. If the newly added node is the last node after the insertion, then we must adjust the tail pointer. We know that the newly added node is the last node if its index is equal to count. If the test is true, then we set tail to point to the new node.

28 wu3399_ch8.qxd //7 :37 Page 8 8 Chapter 8 List ADT clear The clear method empties the list; that is, it resets the list as an empty list. Its implementation is straightforward. We reset head and tail to null and count to. By setting head to null, all the nodes in the list will eventually get garbage-collected. Here s the method: public void clear( ) { head = tail = null; count = ; contains The contains method is implemented by using the indexof method. If the indexof method returns a value other than NOT_FOUND (-), then the specified object is in the list, so the contains method returns true. If the indexof method returns NOT_FOUND, then the contains method returns false. public boolean contains(e item) { boolean result = true; int loc = indexof(item); if (loc == NOT_FOUND) { result = false; return result; get The get method for the linked implementation requires the traversing of link fields to locate the desired node. After checking that the passed index value is valid, the method traverses the pointers. The traversing is essentially the same as we did for the second version of the add method. The only difference is that we are setting the pointer ptr to point to the index node, not to the index-st node, as was the case with the add method. Here s the method: public E get(int index) throws IndexOutOfBoundsException { checkaccessposition(index); E item = null; This loop traverses the list by following the link field for index times. ListNode ptr = head; for (int i = ; i < index; i++) { ptr = ptr.next; item = ptr.item; return item;

29 wu3399_ch8.qxd //7 :37 Page The Linked-List Implementation of the List ADT 9 indexof The indexof method traverses the pointers from head and stops when the first match is located. The counter loc is incremented every time the next node is visited. If the specified object is not in the list, then NOT_FOUND (-) is returned. public int indexof(e item) { int loc = ; ListNode ptr = head; while (loc < count &&!ptr.item.equals(item)) { loc++; ptr = ptr.next; if (loc == count) { loc = NOT_FOUND; return loc; Be careful that the order of operands in the while test is critical. We cannot write it as INVALID while (!ptr.item.equals(item) && loc < count) { isempty because doing so would result in a NullPointerException error in the case where the searched item is not found (i.e., when loc becomes equal to count, ptr is null). The isempty method is simple. We just check the value of the count variable. public boolean isempty( ) { return (count == ); remove For the first version of remove, wemust locate the i-st node. Once this node is located, all that is left to do is to adjust this node s next field. Unlike in the array implementation, no shifting is necessary. This is one advantage of the linked implementation. On the other hand, with the array implementation, there s no search is involved, while the linked implemention requires the traversal of links to locate the i-st node. Figure 8.8 illustrates the operation for the general case. There are end cases we need to watch out for: First, when the node we are removing is the last node in the list, we must adjust the tail pointer to point to the new last node after the removal. Second, when the node we are removing is the first node, then we must adjust the head pointer. If this first node is the only node in the list, then we must also adjust the tail pointer. If we are removing the

30 wu3399_ch8.qxd //7 :37 Page Chapter 8 List ADT :NPSLinkedList tail This is the ith node we re deleting. Before head count k X ptr points to the (i-)st node. :NPSLinkedList ptr tail After head count k X ptr.next = ptr.next.next; count--; Figure 8.8 The first version of the remove operation. Remove the ith node given the value i. only node in the list (both head and tail point to this node), the net result is that both head and tail become null. public E remove(int index) throws IndexOutOfBoundsException { checkaccessposition(index); ListNode deletenode; ListNode ptr = head; if (index == ) { //removing the first node deletenode = ptr; head = head.next;

31 wu3399_ch8.qxd //7 :37 Page 8.4 The Linked-List Implementation of the List ADT if (head == null) { //the first node is //the only node tail = null; else { for (int i = ; i < index; i++) { ptr = ptr.next; deletenode = ptr.next; ptr.next = deletenode.next; if (ptr.next == null) { //very last node was removed tail = ptr; //we have a new last node count--; return deletenode.item; The second version of remove requires a traversal to locate the node to be removed. If there are duplicates, then the first match is removed. If the passed item is not found, then nothing happens. To locate the desired node, we use two pointers: ptr and trail. The ptr will point to the node to be removed, and the trail will point to the node one before the ptr node. In other words, the following relationship holds between the two: trail.next == ptr As with the first version, we must watch out for the end cases of removing the first node, the last node, or the only node. Here s the second remove method: public boolean remove(e item) { boolean result = false; ListNode ptr = head; ListNode trail = null; while (ptr!= null &&!ptr.item.equals(item)) { trail = ptr; ptr = ptr.next; if (ptr!= null) { //found item if (trail == null) { //removing the first node head = head.next;

32 wu3399_ch8.qxd //7 :37 Page Chapter 8 List ADT if (head == null) { //the first node is //the only node tail = null; else { trail.next = ptr.next; if (trail.next == null) { //very last node was tail = trail; //removed, so set tail to //point to a new last node count--; result=true; return result; set After checking that the passed index value is valid, the set method traverses the list index times and sets the pointer ptr to the node to be modified. Once this node is located, its data field is set to refer to the passed item. public E set(int index, E item) { checkaccessposition(index); ListNode ptr = head; for (int i = ; i < index; i++) { ptr = ptr.next; E old = ptr.item; ptr.item = item; return old; size This method is easy (for a change). We simply return the value of the count variable. public int size( ) { return count;

33 wu3399_ch8.qxd //7 :37 Page The Linked-List Implementation of the List ADT 3 We are now ready to list the complete NPSLinkedList class. In the source code listing, we do not show any javadoc comments, to keep the listing to a manageable size. The actual source code includes the full javadoc comments. NPSLinkedList package edu.nps.util; public class NPSLinkedList<E> implements NPSList<E> { public static final int NOT_FOUND = -; private ListNode head; private ListNode tail; private int count; public NPSLinkedList( ) { Constructor clear( ); public void add(e item) { add //creates a new ListNode ListNode newnode = new ListNode(item); if (count == ) { head = tail = newnode; else { tail.next = newnode; tail = newnode; count++; public void add(int index, E item) throws IndexOutOfBoundsException { checkinsertposition(index); add ListNode ptr = head; ListNode newnode = new ListNode(item); if (index == ) { //adding the new node as the first node newnode.next = head; head = newnode; else {

34 wu3399_ch8.qxd //7 :37 Page 4 4 Chapter 8 List ADT for (int i = ; i < index; i++) { ptr = ptr.next; newnode.next = ptr.next; ptr.next = newnode; //adjust tail if the new node added is //the last node in the list if (index == count) { tail = newnode; count++; public void clear( ) { clear head = tail = null; count = ; public boolean contains(e item) { contains boolean result = true; int loc = indexof(item); if (loc == NOT_FOUND) { result = false; return result; public E get(int index) throws IndexOutOfBoundsException { checkaccessposition(index); E item = null; get ListNode ptr = head; for (int i = ; i < index; i++) { ptr = ptr.next; item = ptr.item; return item; public int indexof(e item) { indexof int loc = ;

35 wu3399_ch8.qxd //7 :37 Page The Linked-List Implementation of the List ADT 5 ListNode ptr = head; while (loc < count &&!ptr.item.equals(item)) { loc++; ptr = ptr.next; if (loc == count) { loc = NOT_FOUND; return loc; public boolean isempty( ) { isempty return (count == ); public E remove(int index) throws IndexOutOfBoundsException { checkaccessposition(index); ListNode deletenode; remove ListNode ptr = head; if (index == ) { //removing the first node deletenode = ptr; head = head.next; if (head == null) { //the first node is the only node tail = null; else { for (int i = ; i < index; i++) { ptr = ptr.next; deletenode = ptr.next; ptr.next = deletenode.next; if (ptr.next == null) { //very last node was removed tail = ptr; //we have a new last node count--; return deletenode.item;

36 wu3399_ch8.qxd //7 :37 Page 6 6 Chapter 8 List ADT public boolean remove(e item) { boolean result = false; remove ListNode ptr = head; ListNode trail = null; while (ptr!= null &&!ptr.item.equals(item)) { trail = ptr; ptr = ptr.next; if (ptr!= null) { //found item if (trail == null) { //removing the first node head = head.next; if (head == null) { //the first node is the only node tail = null; else { trail.next = ptr.next; if (trail.next == null) { //very last node was removed, so tail = trail; //set tail to point to a new last node count--; result = true; return result; public E set(int index, E item) { set checkacessposition(index); ListNode ptr = head; for (int i = ; i < index; i++) { ptr = ptr.next; E old = ptr.item; ptr.item = item; return old;

37 wu3399_ch8.qxd //7 :37 Page The Linked-List Implementation of the List ADT 7 public int size( ) { return count; private void checkaccessposition(int index) { size checkaccessposition if (size() == ) { throw new IndexOutOfBoundsException( "Index " + index + " is invalid. List is empty."); else if (index < ) { throw new IndexOutOfBoundsException("Negative index of " + index + " is invalid"); else if (index > size()-) { throw new IndexOutOfBoundsException(index + " is larger than valid upper bound" + (size()-)); private void checkinsertposition(int index) { checkinsertposition if (index < ) { throw new IndexOutOfBoundsException( "Negative index of " + index + " is invalid"); else if (index > size()) { throw new IndexOutOfBoundsException(index + " is larger than valid upper bound" + size()); // Inner Class: ListNode class ListNode { ListNode private E item; private ListNode next; public ListNode(E item) { this.item = item; this.next = null;

38 wu3399_ch8.qxd //7 :37 Page 8 8 Chapter 8 List ADT end cases 8.5 The Linked Implementation with the Head Node In both versions of the add and remove methods of the linked implementation, we have to include a test to determine the special case of removing the first node because we need to treat the removal of the first node differently from the other general cases. The special cases are also called end cases or boundary cases. Consider the case of removal: If we are dealing with a large list, we do not expect the removal of the first node to happen frequently in ordinary applications. Yet, every time we remove a node we must test if it is the removal of the first node. It is not efficient to check every time for something that rarely occurs. Can we do something about it? Is there a way to eliminate this testing? The technique we can use to eliminate the testing of the end case is to insert one extra dummy node at the head of a list. By including this head node, one set of code can be used to handle both the end and general cases. Figure 8.9 shows the lists with the head node. An empty list :NPSLinkedList tail head This is the head node. It contains no information. count :NPSLinkedList tail A list with four items head count 4 :String :String :String :String "eel" Figure 8.9 Sample linked lists with the head node.

39 wu3399_ch8.qxd //7 :37 Page The Linked Implementation with the Head Node 9 With the head node in a list, version remove can be written as follows: public E remove(int index) throws IndexOutOfBoundsException { checkaccessposition(index); ListNode deletenode; ListNode trail = head; for (int i = ; i <= index; i++) { trail = trail.next; deletenode = trail.next; trail.next = deletenode.next; if (deletenode.next == null) { //very last node was tail = trail; //removed so set tail //to the new last node count--; return deletenode.item; Compare this remove to the corresponding method of the NPSLinkedList class. The other remove method and the two versions of the add method can be improved in a similar manner. Here is the NPSLinkedListWithHeader class (the methods that remain the same as those in NPSLinkedList are not listed here). NPSLinkedListWithHeader package edu.nps.util; public class NPSLinkedListWithHeader<E> implements NPSList<E> { public static final int NOT_FOUND = -; private ListNode head; private ListNode tail; private int count; public NPSLinkedListWithHeader( ) { ListNode headnode = new ListNode(null); head = headnode; tail = headnode; count = ; Note: The methods that remain the same as those in NPSLinkedList are not listed here. Constructor

40 wu3399_ch8.qxd //7 :37 Page Chapter 8 List ADT public void add(e item) { //creates a new ListNode ListNode newnode = new ListNode(item); add tail.next = newnode; tail = newnode; count++; public void add(int index, E item) throws IndexOutOfBoundsException { checkinsertposition(index); ListNode ptr = head; add ListNode newnode = new ListNode(item); for (int i = ; i < index; i++) { ptr = ptr.next; newnode.next = ptr.next; ptr.next = newnode; //adjust tail if the new node added is //the last node in the list if (index == count) { tail = newnode; count++; public void clear( ) { clear head.next = null; //don't remove the dummy head node tail = head; count = ; public E get(int index) { get checkaccessposition(index); ListNode ptr = head.next; for (int i = ; i < index; i++) { ptr = ptr.next; return ptr.item;

41 wu3399_ch8.qxd //7 :37 Page 8.5 The Linked Implementation with the Head Node public int indexof(e item) { int loc = ; indexof ListNode ptr = head.next; while (loc < count &&!ptr.item.equals(item)) { loc++; ptr = ptr.next; if (loc == count) { loc = NOT_FOUND; return loc; public E remove(int index) throws IndexOutOfBoundsException { checkacessposition(index); ListNode deletenode; remove ListNode trail = head; for (int i = ; i <= index; i++) { trail = trail.next; deletenode = trail.next; trail.next = deletenode.next; if (deletenode.next == null) { //very last node was tail = trail; //removed so set tail //to the new last node count--; return deletenode.item; public boolean remove(e item) { remove boolean result = false; ListNode ptr = head.next; ListNode trail = head; while (ptr!= null &&!ptr.item.equals(item)) { trail = ptr; ptr = ptr.next;

42 wu3399_ch8.qxd //7 :37 Page Chapter 8 List ADT if (ptr!= null) { trail.next = ptr.next; if (trail.next == null) { //very last node was removed tail = trail; //we have a new last node Count--; result = true; return result; public E set(int index, E item) { set checkaccessposition(index); ListNode ptr = head.next; for (int i = ; i < index; i++) { ptr = ptr.next; E old = ptr.item; ptr.item = item; return old;... traversal 8.6 The Iterator Design Pattern One of the most common operations we perform on a data structure, including the linear list, is a traversal. Traversing a data structure means visiting, or accessing, every element in the data structure. For example, if a data structure contains Person objects and we want to find out their average age, we have to visit every Person object in the data structure to derive the sum of their ages and then divide the sum by the total number of elements. Traversing a complex data structure can be complicated because there could be many different ways to visit elements. For a simpler data structure, such as the linear list, however, traversal is straightforward. We visit the elements in sequence: visit the first element, the second element, and so forth. Here s a sample code for

43 wu3399_ch8.qxd //7 :37 Page The Iterator Design Pattern 3 traversing Person objects in a list to compute their average: NPSList<Person> personlist; //Assume personlist is created and includes //Person objects; for simplicity we assume //the list includes at least one element double agesum, avgage; agesum = ; for (int i = ; i < personlist.size(); i++) { Person p = personlist.get(i); agesum += p.getage(); avgage = agesum / personlist.size(); iterator What would be the cost of such a traversal operation? It depends on the implementation. With the array implementation, the cost of accessing every element in the list is N, but with the linked-list implementation, the cost is (N N). Let s see why there is such an order-of-magnitude difference. The cost of the get method in the array implementation is the same regardless of the location of an element, because any element in an array can be accessed by applying the same address computation formula. How about the get method of the linked implementation? To access the ith element, we must traverse the links i times, starting from the head node, so the cost is i. Assuming that the cost of following a link and the cost of address computation are the same, we can derive the total cost for scanning as follows: N N for the i array implementation and N i (N N) for the linked implementation. i Since traversal is a frequently used operation, it is worth the effort to improve its performance. We would like to improve the performance of the linked-list implementation to the level of the array implementation, that is, from (N N) to N. We can do so by applying the technique known as the iterator design pattern. An iterator is a design pattern that supports a consistent way to access the elements stored in a data structure. We define an iterator as an ADT that supports two operations the first to prompt if there are more elements to visit and the second to retrieve the next element to visit. Here s the interface definition for the ADT: NPSIterator package edu.nps.util; interface NPSIterator<E> { public boolean hasnext( ); public E next( ) throws NPSNoSuchElementException;

44 wu3399_ch8.qxd //7 :37 Page 4 4 Chapter 8 List ADT The hasnext method returns true if there are more elements in the iterator. The next method returns the next object in the iterator if there is one. Otherwise, it throws an NPSNoSuchElementException. This exception class is patterned after the NoSuchElementException class in the java.util package. We avoid using classes and interfaces from the java.util package in our edu.nps.util package to make it selfcontained. We do not want to require client programmers to import the java.util package also when using the edu.nps.util package. We add a method, named iterator, to the NPSList interface that returns an iterator. With the iterator method, we can compute the average age of Person objects in a list as follows: NPSList<Person> personlist; //Assume personlist is created and includes //Person objects; for simplicity we assume //the list includes at least one element double agesum, avgage; agesum = ; NPSIterator itr = personlist.iterator(); while (itr.hasnext()) { Person p = itr.next(); agesum += p.getage(); avgage = agesum / personlist.size(); Let s first modify the NPSLinkedList class to include the iterator method. Notice that NPSIterator is an interface, so the iterator method must return an instance of a class that implements this interface. The best way to achieve this is to define an inner class that implements the NPSIterator interface inside the NPSLinkedList class. In this inner class, we keep a data member current that keeps track of the node whose item value we return when the next method is called. Every time the next method is called, we update the current pointer to the next node in the list. When the next method is called and the current points to the last node in the list, the updated current becomes null, and the next time the hasnext method is called, it returns false. Here s how we define the modified class: NPSLinkedList (final version) package edu.nps.util; public class NPSLinkedList<E> implements NPSList<E> {...

45 wu3399_ch8.qxd //7 :37 Page The Iterator Design Pattern 5 public NPSIterator iterator( ) { return new MyIterator(head);... //-----Inner Class: MyIterator // private class MyIterator implements NPSIterator<E> { private ListNode current; public MyIterator(ListNode node) { current = node; public boolean hasnext( ) { return current!= null; public E next( ) throws NPSNoSuchElementException { if (current == null) { throw new NPSNoSuchElementException("No more element"); Object item = current.item; current = current.next; return item; INVALID Notice that the type parameter is not specified for the inner class MyIterator. It is invalid to declare the inner class as private class MyIterator<E> implements NPSIterator<E> { Doing so would conflict with the type parameter of the outer class NPSLinkedList. With this invalid declaration, E in the inner class and E in the outer class would be viewed as referring to two different actual types. By not associating any type parameter to MyIterator, the generic type E in the inner and outer classes refers to the same actual type.

46 wu3399_ch8.qxd //7 :37 Page 6 6 Chapter 8 List ADT The NPSNoSuchElementException class is a very simple class. Here s the definition: NPSNoSuchElementException package edu.nps.util; public class NPSNoSuchElementException extends RuntimeException { public NPSNoSuchElementException( ) { this("requested element does not exist in the data structure"); public NPSNoSuchElementException(String message) { super(message); Now let s look at how we can implement the iterator method for the NPSArrayList class. We should realize that we are not gaining any performance improvement in doing this, because the cost of calling the get method N times, once for each node in the list, to scan the elements in a list is O(N). But defining the iterator method to the NPSArrayList class gives us a consistent and easy-to-use manner of traversing elements in all types of diverse data structures. We follow the same approach used in the iterator inner class of NPSLinkedList. This time, however, instead of the current pointer, we maintain an int data member current that keeps track of the index of the current node. We initialize current to. The hasnext method returns true if the value of current is less than or equal to the maximum possible index value, i.e., the size of the list minus. When the next method is called and the value of current is not less than the size of the list, it throws an exception. Otherwise, the method returns the current element and increments current by so it will point to the next element. The modified NPSArrayList is defined as follows: NPSArrayList (final version) package edu.nps.util; public class NPSArrayList<E> implements NPSList<E> {... public NPSIterator iterator( ) { return new MyIterator();

47 wu3399_ch8.qxd //7 :37 Page Sample Development 7 //// Inner Class : MyIterator //// private class MyIterator implements NPSIterator<E> { private int current; public MyIterator( ) { current = ; public boolean hasnext( ) { if (current < size()-) { return true; else { return false; public E next( ) throws NPSNoSuchElementException { if (current >= size()) { throw new NPSNoSuchElementException(); else { int idx = current; //these three statements can be written current++; //succinctly in a single statement as return element[idx]; //return element[current++] Sample Development 8.7 Sample Development Fortune Cookies Let s write a simple application that illustrates the use of a list. The program will display a fortune cookie, a short text message, when requested by the user. Since each fortune cookie is a string, we maintain a database of fortune cookies using a list ADT. Problem Statement Write a program that displays a fortune cookie (text message) every time the user wishes it. Repeatedly prompt the user if he or she wishes to read another fortune cookie. If the reply is yes, display it. If the reply is no, terminate the program. Assume there is a text file named fortune.txt that contains text messages. Each line of text represents a single fortune cookie.

Linked List. ape hen dog cat fox. tail. head. count 5

Linked List. ape hen dog cat fox. tail. head. count 5 Linked Lists Linked List L tail head count 5 ape hen dog cat fox Collection of nodes with a linear ordering Has pointers to the beginning and end nodes Each node points to the next node Final node points

More information

Queue ADT. B/W Confirming Pages

Queue ADT. B/W Confirming Pages wu23399_ch20.qxd 1/2/07 20:56 Page 1069 20 Queue ADT O b j e c t i v e s After you have read and studied this chapter, you should be able to Describe the key features of the Queue ADT. Implement the Queue

More information

CSC 1052 Algorithms & Data Structures II: Lists

CSC 1052 Algorithms & Data Structures II: Lists CSC 1052 Algorithms & Data Structures II: Lists Professor Henry Carter Spring 2018 Recap Collections hold and access elements based on content Order and index no longer considered Comparable elements implement

More information

Implementing a List in Java. CSE 143 Java. Just an Illusion? List Interface (review) Using an Array to Implement a List.

Implementing a List in Java. CSE 143 Java. Just an Illusion? List Interface (review) Using an Array to Implement a List. Implementing a List in Java CSE 143 Java List Implementation Using Arrays Reading: Ch. 13 Two implementation approaches are most commonly used for simple lists: Arrays Linked list Java Interface List concrete

More information

ITI Introduction to Computing II

ITI Introduction to Computing II index.pdf March 17, 2013 1 ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Version of March 17, 2013 Definitions A List is a linear abstract

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 07: Linked Lists MOUNA KACEM mouna@cs.wisc.edu Spring 2019 Linked Lists 2 Introduction Linked List Abstract Data Type SinglyLinkedList ArrayList Keep in Mind Introduction:

More information

Stack ADT. B/W Confirming Pages

Stack ADT. B/W Confirming Pages wu23399_ch19.qxd 1/2/07 20:46 Page 1035 19 Stack ADT O b j e c t i v e s After you have read and studied this chapter, you should be able to Describe the key features of the Stack ADT. Develop applications

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 07: Linked Lists and Iterators MOUNA KACEM mouna@cs.wisc.edu Fall 2018 Linked Lists 2 Introduction Linked List Abstract Data Type General Implementation of the ListADT

More information

Implementing a List in Java. CSE 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List.

Implementing a List in Java. CSE 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List. Implementing a List in Java CSE 143 Java List Implementation Using Arrays Reading: Ch. 22 Two implementation approaches are most commonly used for simple lists: Arrays Linked list Java Interface List concrete

More information

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, ,

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , CSE 143 Lecture 26 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, 15.3-15.4, 16.4-16.5 slides created by Marty Stepp, adapted by Alyssa Harding

More information

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print) Computer Science 62 Bruce/Mawhorter Fall 16 Midterm Examination October 5, 2016 Question Points Score 1 15 2 10 3 10 4 8 5 9 TOTAL 52 SOLUTIONS Your name (Please print) 1. Suppose you are given a singly-linked

More information

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12 CS 151 Linked Lists, Recursively Implemented 1 2 Linked Lists, Revisited Recall that a linked list is a structure that represents a sequence of elements that are stored non-contiguously in memory. We can

More information

Implementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations

Implementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations Readings List Implementations Chapter 20.2 Objectives Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations Additional references:

More information

CS201 ArrayLists, Generics, and Dynamic Data Structures (Chapters 14, 15)

CS201 ArrayLists, Generics, and Dynamic Data Structures (Chapters 14, 15) CS201 ArrayLists, Generics, and Dynamic Data Structures (Chapters 14, 15) A data structure is a software construct used to organize our data in a particular way. Some common data structures include lists,

More information

CSC 172 Data Structures and Algorithms. Lecture #9 Spring 2018

CSC 172 Data Structures and Algorithms. Lecture #9 Spring 2018 CSC 172 Data Structures and Algorithms Lecture #9 Spring 2018 SINGLY LINKED LIST 3.1.3 Linked lists We will consider these for Singly linked lists Doubly linked lists Basic Singly Linked List class Node

More information

A Linked Structure. Chapter 17

A Linked Structure. Chapter 17 Chapter 17 A Linked Structure This chapter demonstrates the OurList interface implemented with a class that uses a linked structure rather than the array implementation of the previous chapter. The linked

More information

COMP200 GENERICS. OOP using Java, from slides by Shayan Javed

COMP200 GENERICS. OOP using Java, from slides by Shayan Javed 1 1 COMP200 GENERICS OOP using Java, from slides by Shayan Javed 2 ArrayList and Java Generics 3 Collection A container object that groups multiple objects 4 Collection A container object that groups multiple

More information

CSC 172 Data Structures and Algorithms. Lecture #8 Spring 2018

CSC 172 Data Structures and Algorithms. Lecture #8 Spring 2018 CSC 172 Data Structures and Algorithms Lecture #8 Spring 2018 Project 1 due tonight Announcements Project 2 will be out soon Q & A Reminder: For sharing your concern anonymously, you can always go to:

More information

An Introduction to Data Structures

An Introduction to Data Structures An Introduction to Data Structures Advanced Programming ICOM 4015 Lecture 17 Reading: Java Concepts Chapter 20 Fall 2006 Adapded from Java Concepts Companion Slides 1 Chapter Goals To learn how to use

More information

Linked Lists. Chapter 12.3 in Savitch

Linked Lists. Chapter 12.3 in Savitch Linked Lists Chapter 12.3 in Savitch Preliminaries n Arrays are not always the optimal data structure: q An array has fixed size needs to be copied to expand its capacity q Adding in the middle of an array

More information

Arrays and ArrayLists. David Greenstein Monta Vista High School

Arrays and ArrayLists. David Greenstein Monta Vista High School Arrays and ArrayLists David Greenstein Monta Vista High School Array An array is a block of consecutive memory locations that hold values of the same data type. Individual locations are called array s

More information

CSCI 200 Lab 3 Using and implementing sets

CSCI 200 Lab 3 Using and implementing sets CSCI 200 Lab 3 Using and implementing sets In this lab, you will write a program that manages a set of workers, using the Worker hierarchy you developed in Lab 2. You will also implement your own version

More information

Class 26: Linked Lists

Class 26: Linked Lists Introduction to Computation and Problem Solving Class 26: Linked Lists Prof. Steven R. Lerman and Dr. V. Judson Harward 2 The Java Collection Classes The java.util package contains implementations of many

More information

An Activation Record for Simple Subprograms. Activation Record for a Language with Stack-Dynamic Local Variables

An Activation Record for Simple Subprograms. Activation Record for a Language with Stack-Dynamic Local Variables Activation Records The storage (for formals, local variables, function results etc.) needed for execution of a subprogram is organized as an activation record. An Activation Record for Simple Subprograms.

More information

Array Based Lists. Collections

Array Based Lists. Collections Array Based Lists Reading: RS Chapter 15 1 Collections Data structures stores elements in a manner that makes it easy for a client to work with the elements Specific collections are specialized for particular

More information

Arrays. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Arrays. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html 1 Arrays Arrays in Java an array is a container object that holds a fixed number of values of a single type the length of an array is established when the array is created 2 https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

More information

Collections and Iterators. Collections

Collections and Iterators. Collections Collections and Iterators Based on the notes from David Fernandez-Baca and Steve Kautz Based on The Java Tutorial (http://docs.oracle.com/javase/tutorial/java/) Bryn Mawr College CS206 Intro to Data Structures

More information

Introduction to Computer Science II CS S-20 Linked Lists III

Introduction to Computer Science II CS S-20 Linked Lists III Introduction to Computer Science II CS112-2012S-20 Linked Lists III David Galles Department of Computer Science University of San Francisco 20-0: Linked List Previous Practical Example: removeat(int index)

More information

Recursive Objects. Singly Linked List (Part 2)

Recursive Objects. Singly Linked List (Part 2) Recursive Objects Singly Linked List (Part 2) 1 Operations at the head of the list operations at the head of the list require special handling because there is no node before the head node 2 Adding to

More information

What is an Iterator? An iterator is an abstract data type that allows us to iterate through the elements of a collection one by one

What is an Iterator? An iterator is an abstract data type that allows us to iterate through the elements of a collection one by one Iterators What is an Iterator? An iterator is an abstract data type that allows us to iterate through the elements of a collection one by one 9-2 2-2 What is an Iterator? An iterator is an abstract data

More information

ADT Stack. Inserting and deleting elements occurs at the top of Stack S. top. bottom. Stack S

ADT Stack. Inserting and deleting elements occurs at the top of Stack S. top. bottom. Stack S Stacks Stacks & Queues A linear sequence, or list, is an ordered collection of elements: S = (s 1, s 2,..., s n ) Stacks and queues are finite linear sequences. A Stack is a LIFO (Last In First Out) list.

More information

CS Introduction to Data Structures Week 1 Thursday

CS Introduction to Data Structures Week 1 Thursday CS 367 - Introduction to Data Structures Week 1 Thursday We assume that you are proficient at object-oriented programming in Java. Please enroll in CS300 if you do not know Java and have not written object-oriented

More information

EECS 2011 M: Fundamentals of Data Structures

EECS 2011 M: Fundamentals of Data Structures EECS 2011 M: Fundamentals of Data Structures Suprakash Datta Office: LAS 3043 Course page: http://www.eecs.yorku.ca/course/2011m Also on Moodle S. Datta (York Univ.) EECS 2011 W18 1 / 19 Iterators and

More information

Chapter 17: Linked Lists

Chapter 17: Linked Lists Chapter 17: Linked Lists 17.1 Introduction to the Linked List ADT Introduction to the Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures list head

More information

Implementing a List in Java. CSC 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List CSC

Implementing a List in Java. CSC 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List CSC Implementing a List in Java CSC 143 Java List Implementation Using Arrays Updated with Java 5.0 Generics Reading: Ch. 13 Two implementation approaches are most commonly used for simple lists: Arrays Linked

More information

CSC 321: Data Structures. Fall 2017

CSC 321: Data Structures. Fall 2017 CSC 321: Data Structures Fall 2017 Linked structures nodes & recursive fields singly-linked list doubly-linked list LinkedList implementation iterators 1 ArrayLists vs. LinkedLists to insert or remove

More information

Linked List Nodes (reminder)

Linked List Nodes (reminder) Outline linked lists reminders: nodes, implementation, invariants circular linked list doubly-linked lists iterators the Java foreach statement iterator implementation the ListIterator interface Linked

More information

CE204 Data Structures and Algorithms Part 2

CE204 Data Structures and Algorithms Part 2 CE204 Data Structures and Algorithms Part 2 14/01/2018 CE204 Part 2 1 Abstract Data Types 1 An abstract data type is a type that may be specified completely without the use of any programming language.

More information

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University CS 112 Introduction to Computing II Wayne Snyder Department Boston University Today Introduction to Linked Lists Stacks and Queues using Linked Lists Next Time Iterative Algorithms on Linked Lists Reading:

More information

What is the Java Collections Framework?

What is the Java Collections Framework? 1 of 13 What is the Java Collections Framework? To begin with, what is a collection?. I have a collection of comic books. In that collection, I have Tarzan comics, Phantom comics, Superman comics and several

More information

Linked Lists. Linked List Nodes. Walls and Mirrors Chapter 5 10/25/12. A linked list is a collection of Nodes: item next -3.

Linked Lists. Linked List Nodes. Walls and Mirrors Chapter 5 10/25/12. A linked list is a collection of Nodes: item next -3. Linked Lists Walls and Mirrors Chapter 5 Linked List Nodes public class Node { private int item; private Node next; public Node(int item) { this(item,null); public Node(int item, Node next) { setitem(item);

More information

The ArrayList class CSC 123 Fall 2018 Howard Rosenthal

The ArrayList class CSC 123 Fall 2018 Howard Rosenthal The ArrayList class CSC 123 Fall 2018 Howard Rosenthal Lesson Goals Describe the ArrayList class Discuss important methods of this class Describe how it can be used in modeling Much of the information

More information

Problem 1: Building and testing your own linked indexed list

Problem 1: Building and testing your own linked indexed list CSCI 200 Lab 8 Implementing a Linked Indexed List In this lab, you will be constructing a linked indexed list. You ll then use your list to build and test a new linked queue implementation. Objectives:

More information

JAVA COLLECTION FRAMEWORK & SETS

JAVA COLLECTION FRAMEWORK & SETS JAVA COLLECTION FRAMEWORK & SETS Ch07.4-5 & Ch10.5 Presentation for use with the textbook 1. Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,

More information

16. Dynamic Data Structures

16. Dynamic Data Structures Data Structures 6. Dynamic Data Structures A data structure is a particular way of organizing data in a computer so that it can be used efficiently Linked lists, Abstract data types stack, queue, Sorted

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms CS245-2015S-05 Abstract Data Types and Lists David Galles Department of Computer Science University of San Francisco 05-0: Abstract Data Types Recall that an Abstract Data

More information

11/2/ Dynamic Data Structures & Generics. Objectives. Array-Based Data Structures: Outline. Harald Gall, Prof. Dr.

11/2/ Dynamic Data Structures & Generics. Objectives. Array-Based Data Structures: Outline. Harald Gall, Prof. Dr. 12. Dynamic Data Structures & Generics Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich http://seal.ifi.uzh.ch Objectives! Define and use an instance of ArrayList! Describe general idea

More information

CMSC 206: Data Structures Final Exam Reference May 2018

CMSC 206: Data Structures Final Exam Reference May 2018 CMSC 206: Data Structures Final Exam Reference May 2018 public interface BMCSet /** Adds a new item to the set * @param item The new item to add to the set * @return true if the item is a new item added

More information

January 24, Abstract Data Types (ADTs) An ADT is an abstraction of a data structure.

January 24, Abstract Data Types (ADTs) An ADT is an abstraction of a data structure. Lists CSE 2011 Winter 2007 January 24, 2007 1 Abstract Data Types (ADTs) An ADT is an abstraction of a data structure. An ADT specifies: data stored operations on the data error conditions associated with

More information

CS S-05 Abstract Data Types and Lists 1

CS S-05 Abstract Data Types and Lists 1 CS245-2016S-05 Abstract Data Types and Lists 1 05-0: Abstract Data Types Recall that an Abstract Data Type is a definition of a type based on the operations that can be performed on it. An ADT is an interface

More information

DNHI Homework 3 Solutions List, Stacs and Queues

DNHI Homework 3 Solutions List, Stacs and Queues Solutions List, Stacs and Queues Problem 1 Given the IntegerQueue ADT below state the return value and show the content of the, initially empty, queue of Integer objects after each of the following operations.

More information

Introduction. Reference

Introduction. Reference Introduction Data Structures - Linked Lists 01 Dr TGI Fernando 1 2 Problems with arrays Unordered array - searching is slow, deletion is slow Ordered array - insertion is slow, deletion is slow Arrays

More information

CSE 143. Lecture 7: Linked List Basics reading: 16.2

CSE 143. Lecture 7: Linked List Basics reading: 16.2 CSE 143 Lecture 7: Linked List Basics reading: 16.2 References vs. objects variable = value; a variable (left side of = ) is an arrow (the base of an arrow) a value (right side of = ) is an object (a box;

More information

Introduction to Computer Science II CS S-18 Linked Lists

Introduction to Computer Science II CS S-18 Linked Lists Introduction to Computer Science II CS112-2012S-18 Linked Lists David Galles Department of Computer Science University of San Francisco 18-0: Linked Lists Linked List node Data Pointer to the next element

More information

Lists. CITS2200 Data Structures and Algorithms. Topic 9

Lists. CITS2200 Data Structures and Algorithms. Topic 9 CITS2200 Data Structures and Algorithms Topic 9 Lists Why lists? List windows Specification Block representation Singly linked representation Performance comparisons Reading: Lambert and Osborne, Sections

More information

CS 61B Data Structures and Programming Methodology. June David Sun

CS 61B Data Structures and Programming Methodology. June David Sun CS 61B Data Structures and Programming Methodology June 26 2008 David Sun Announcements Project 1 will be handed out at the end of next week. Project 1 is now due after Midterm I on 7/15. Lab2 homework

More information

2. The actual object type stored in an object of type CollectionClassName<E> is specified when the object is created.

2. The actual object type stored in an object of type CollectionClassName<E> is specified when the object is created. 1. Because an ArrayList is an indexed collection, you can access its elements using a subscript. 2. The actual object type stored in an object of type CollectionClassName is specified when the object

More information

Standard ADTs. Lecture 19 CS2110 Summer 2009

Standard ADTs. Lecture 19 CS2110 Summer 2009 Standard ADTs Lecture 19 CS2110 Summer 2009 Past Java Collections Framework How to use a few interfaces and implementations of abstract data types: Collection List Set Iterator Comparable Comparator 2

More information

LINKED LISTS cs2420 Introduction to Algorithms and Data Structures Spring 2015

LINKED LISTS cs2420 Introduction to Algorithms and Data Structures Spring 2015 LINKED LISTS cs2420 Introduction to Algorithms and Data Structures Spring 2015 1 administrivia 2 -assignment 5 due tonight at midnight -assignment 6 is out -YOU WILL BE SWITCHING PARTNERS! 3 assignment

More information

CS S-20 Linked Lists III 1. We can then use the next pointer of the previous node to do removal (example on board)

CS S-20 Linked Lists III 1. We can then use the next pointer of the previous node to do removal (example on board) CS112-2012S-20 Linked Lists III 1 20-0: Linked List ious Practical Example: removeat(int index) remove( o) 20-1: removeat First need to get to node before the one we want to remove We can then use the

More information

CSC 321: Data Structures. Fall 2013

CSC 321: Data Structures. Fall 2013 CSC 321: Data Structures Fall 2013 Lists, stacks & queues Collection classes: List (ArrayList, LinkedList), Set (TreeSet, HashSet), Map (TreeMap, HashMap) ArrayList performance and implementation LinkedList

More information

Review: List Implementations. CSE 143 Java. Links. A Different Strategy: Lists via Links. Linked Links. CSE143 Au List

Review: List Implementations. CSE 143 Java. Links. A Different Strategy: Lists via Links. Linked Links. CSE143 Au List Review: Implementations CSE 143 Java ed s Reading: Ch. 23 The external interface is already defined Implementation goal: implement methods efficiently Array approach: use an array with extra space internally

More information

Arrays. Chapter Arrays What is an Array?

Arrays. Chapter Arrays What is an Array? Chapter 8 Arrays 81 Arrays 811 What is an Array? To motivate why we might be interested in using arrays, let us implement an app that creates a collection of doubles We will keep track of the number of

More information

Arrays and Linked Lists

Arrays and Linked Lists Arrays and Linked Lists Abstract Data Types Stacks Queues Priority Queues and Deques John Edgar 2 And Stacks Reverse Polish Notation (RPN) Also known as postfix notation A mathematical notation Where every

More information

INTRODUCTION TO DATA AND PROCEDURE

INTRODUCTION TO DATA AND PROCEDURE INTRODUCTION TO DATA AND PROCEDURE In this book, our goal is to understand computation. In particular, we want to be able to take any computational problem and produce a technique for solving it that is

More information

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011 Stacks (5.1) CSE 2011 Winter 2011 26 January 2011 1 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data Error

More information

Introduction to Linked Data Structures

Introduction to Linked Data Structures Introduction to Linked Data Structures A linked data structure consists of capsules of data known as nodes that are connected via links Links can be viewed as arrows and thought of as one way passages

More information

Java Review: Objects

Java Review: Objects Outline Java review Abstract Data Types (ADTs) Interfaces Class Hierarchy, Abstract Classes, Inheritance Invariants Lists ArrayList LinkedList runtime analysis Iterators Java references 1 Exam Preparation

More information

COMP 250 Winter generic types, doubly linked lists Jan. 28, 2016

COMP 250 Winter generic types, doubly linked lists Jan. 28, 2016 COMP 250 Winter 2016 5 generic types, doubly linked lists Jan. 28, 2016 Java generics In our discussion of linked lists, we concentrated on how to add or remove a node from the front or back of a list.

More information

Data abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003

Data abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003 Data abstractions: ADTs Invariants, Abstraction function Lecture 4: OOP, autumn 2003 Limits of procedural abstractions Isolate implementation from specification Dependency on the types of parameters representation

More information

Specifications for the ADT List Ali list provides a way to organize data List of students List of sales List of lists

Specifications for the ADT List Ali list provides a way to organize data List of students List of sales List of lists Abstract Data Type List ADT List Specifications for the ADT List Ali list provides a way to organize data List of students List of sales List of lists A list has first, last, and in between items (the

More information

Recursion (Part 3) 1

Recursion (Part 3) 1 1 Recursion (Part 3) Computational complexity computational complexity is concerned with describing the amount of resources needed to run an algorithm for our purposes, the resource is time complexity

More information

Outline. iterator review iterator implementation the Java foreach statement testing

Outline. iterator review iterator implementation the Java foreach statement testing Outline iterator review iterator implementation the Java foreach statement testing review: Iterator methods a Java iterator only provides two or three operations: E next(), which returns the next element,

More information

Exceptions and Design

Exceptions and Design Exceptions and Exceptions and Table of contents 1 Error Handling Overview Exceptions RuntimeExceptions 2 Exceptions and Overview Exceptions RuntimeExceptions Exceptions Exceptions and Overview Exceptions

More information

package weiss.util; // Fig , pg // Fig 6.16,6.17, pg package weiss.util;

package weiss.util; // Fig , pg // Fig 6.16,6.17, pg package weiss.util; package weiss.util; // Fig 6.9-10, pg 192-4. public interface Collection extends java.io.serializable int size( ); boolean isempty( ); boolean contains( Object x ); boolean add( Object x ); boolean remove(

More information

Lecture 4. The Java Collections Framework

Lecture 4. The Java Collections Framework Lecture 4. The Java s Framework - 1 - Outline Introduction to the Java s Framework Iterators Interfaces, Classes and Classes of the Java s Framework - 2 - Learning Outcomes From this lecture you should

More information

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue Queues CSE 2011 Fall 2009 9/28/2009 7:56 AM 1 Queues: FIFO Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue Applications,

More information

Insertion Sort: an algorithm for sorting an array

Insertion Sort: an algorithm for sorting an array Insertion Sort: an algorithm for sorting an array Let s use arrays to solve a problem that comes up often in programming, namely sorting. Suppose we have an array of objects that is in no particular order

More information

Abstract Data Types. Data Str. Client Prog. Add. Rem. Find. Show. 01/22/04 Lecture 4 1

Abstract Data Types. Data Str. Client Prog. Add. Rem. Find. Show. 01/22/04 Lecture 4 1 Abstract Data Types Add Client Prog Rem Find Data Str. Show 01/22/04 Lecture 4 1 Containers Powerful tool for programming data structures Provides a library of container classes to hold your objects 2

More information

The Java Collections Framework. Chapters 7.5

The Java Collections Framework. Chapters 7.5 The Java s Framework Chapters 7.5 Outline Introduction to the Java s Framework Iterators Interfaces, Classes and Classes of the Java s Framework Outline Introduction to the Java s Framework Iterators Interfaces,

More information

Lists. The List ADT. Reading: Textbook Sections

Lists. The List ADT. Reading: Textbook Sections Lists The List ADT Reading: Textbook Sections 3.1 3.5 List ADT A list is a dynamic ordered tuple of homogeneous elements A o, A 1, A 2,, A N-1 where A i is the i-th element of the list The position of

More information

Lists, Stacks, and Queues

Lists, Stacks, and Queues Unit 8, Part 2 Lists, Stacks, and Queues Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Representing a Sequence: Arrays vs. Linked Lists Sequence an ordered collection of items (position

More information

Lecture 3 Linear Data Structures: Arrays, Array Lists, Stacks, Queues and Linked Lists

Lecture 3 Linear Data Structures: Arrays, Array Lists, Stacks, Queues and Linked Lists Lecture 3 Linear Data Structures: Arrays, Array Lists, Stacks, Queues and Linked Lists Chapters 3.1-3.3, 5.1-5.2, 6.1-1 - Core Collection Interfaces - 2 - The Java Collections Framework Interface Abstract

More information

CSC 321: Data Structures. Fall 2012

CSC 321: Data Structures. Fall 2012 CSC 321: Data Structures Fall 2012 Lists, stacks & queues Collection classes: List (ArrayList, LinkedList), Set (TreeSet, HashSet), Map (TreeMap, HashMap) ArrayList performance and implementation LinkedList

More information

DM537 Object-Oriented Programming. Peter Schneider-Kamp.

DM537 Object-Oriented Programming. Peter Schneider-Kamp. DM537 Object-Oriented Programming Peter Schneider-Kamp petersk@imada.sdu.dk! http://imada.sdu.dk/~petersk/dm537/! RECURSION (REVISITED) 2 Recursion (Revisited) recursive function = a function that calls

More information

Queues. CITS2200 Data Structures and Algorithms. Topic 5

Queues. CITS2200 Data Structures and Algorithms. Topic 5 CITS2200 Data Structures and Algorithms Topic 5 Queues Implementations of the Queue ADT Queue specification Queue interface Block (array) representations of queues Recursive (linked) representations of

More information

Introduction to Computing II (ITI 1121) Final Examination

Introduction to Computing II (ITI 1121) Final Examination Université d Ottawa Faculté de génie École de science informatique et de génie électrique University of Ottawa Faculty of Engineering School of Electrical Engineering and Computer Science Introduction

More information

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp DM550 / DM857 Introduction to Programming Peter Schneider-Kamp petersk@imada.sdu.dk http://imada.sdu.dk/~petersk/dm550/ http://imada.sdu.dk/~petersk/dm857/ ABSTRACT DATATYPES 2 Abstract Datatype (ADT)

More information

ADTs, Arrays, Linked Lists

ADTs, Arrays, Linked Lists 1 ADTs, Arrays, Linked Lists Outline and Required Reading: ADTs ( 2.1) Using Arrays ( 3.1) Linked Lists ( 3.2, 3.3, 3.4) CSE 2011, Winter 2017 Instructor: N. Vlajic Data Type 2 A data type is a classification

More information

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp DM550 / DM857 Introduction to Programming Peter Schneider-Kamp petersk@imada.sdu.dk http://imada.sdu.dk/~petersk/dm550/ http://imada.sdu.dk/~petersk/dm857/ RECURSION (REVISITED) 2 Recursion (Revisited)

More information

Implementing Lists, Stacks, Queues, and Priority Queues

Implementing Lists, Stacks, Queues, and Priority Queues Implementing Lists, Stacks, Queues, and Priority Queues CSE260, Computer Science B: Honors Stony Brook University http://www.cs.stonybrook.edu/~cse260 1 Objectives To design common features of lists in

More information

DM503 Programming B. Peter Schneider-Kamp.

DM503 Programming B. Peter Schneider-Kamp. DM503 Programming B Peter Schneider-Kamp petersk@imada.sdu.dk! http://imada.sdu.dk/~petersk/dm503/! ABSTRACT DATATYPE FOR LISTS 2 List ADT: Specification data are all integers, here represented as primitive

More information

List ADT. Announcements. The List interface. Implementing the List ADT

List ADT. Announcements. The List interface. Implementing the List ADT Announcements Tutoring schedule revised Today s topic: ArrayList implementation Reading: Section 7.2 Break around 11:45am List ADT A list is defined as a finite ordered sequence of data items known as

More information

CS/CE 2336 Computer Science II

CS/CE 2336 Computer Science II CS/CE 2336 Computer Science II UT D Session 11 OO Data Structures Lists, Stacks, Queues Adapted from D. Liang s Introduction to Java Programming, 8 th Ed. 2 What is a Data Structure? A collection of data

More information

Dynamic Data Structures and Generics

Dynamic Data Structures and Generics Dynamic Data Structures and Generics Reading: Savitch ch. 12 Objectives Introduce Abstract Data Types (ADTs) and review interfaces Introduce Java's ArrayList class Learn about linked lists and inner classes

More information

Lecture 6: ArrayList Implementation

Lecture 6: ArrayList Implementation Lecture 6: ArrayList Implementation CS 62 Fall 2018 Alexandra Papoutsaki & William Devanny 1 Programming Assignment Weak AI/Natural Language Processing: Generate text by building frequency lists based

More information

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE TED (10)-3071 Reg. No.. (REVISION-2010) Signature. FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours (Maximum marks: 100)

More information

Collections, Maps and Generics

Collections, Maps and Generics Collections API Collections, Maps and Generics You've already used ArrayList for exercises from the previous semester, but ArrayList is just one part of much larger Collections API that Java provides.

More information

Computer Science 62. Midterm Examination

Computer Science 62. Midterm Examination Computer Science 62 Bruce/Mawhorter Fall 16 Midterm Examination October 5, 2016 Question Points Score 1 15 2 10 3 10 4 8 5 9 TOTAL 52 Your name (Please print) 1. Suppose you are given a singly-linked list

More information

The list abstract data type defined a number of operations that all list-like objects ought to implement:

The list abstract data type defined a number of operations that all list-like objects ought to implement: Chapter 7 Polymorphism Previously, we developed two data structures that implemented the list abstract data type: linked lists and array lists. However, these implementations were unsatisfying along two

More information