lecture_27_s2005

lecture_27_s2005 - 1.00 Lecture 27 Data Structures: Linked...

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

View Full Document Right Arrow Icon
1.00 Lecture 27 Data Structures: Linked lists Reading for next time: Big Java: 20.5 Lists as an Abstract Data Type A list is a collection of elements that has an order. It can have arbitrary length. You should be able to efficiently insert or delete an element anywhere. You should be able to go through the list in order an element at a time.
Background image of page 1

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

View Full DocumentRight Arrow Icon
A List Interface import java.util.*; public interface List { public boolean isEmpty(); public void addFirst( Object o ); public void addLast( Object o ); public boolean contains(Object o); public Object removeLast() throws NoSuchElementException; public Object removeFirst() throws NoSuchElementException; public boolean remove(Object o); // Only in download public void clear(); public int size(); public void print(); public ListIterator listIterator(); // Only in download } // This interface doesn’t have an add() method to // place an element in an arbitrary position. It’s // straightforward but tedious—we don’t cover it Arrays Don’t Work If we used an array: Inserting an element at any place except the end of the list is very expensive because all the elements from the point of insertion until the end must be moved back to make room for the new entry. There is a similar problem with deletion. a b c e f g h i j k l m d For this reason, lists use a linked implementation.
Background image of page 2
Singly Linked List Diagram List first last Node 1 Item 1 Node n Item 2 Item n ... null Node 2 next item Each Node has two data members: item next item next item next Singly Linked Lists, 2 The List points to the first Node , and to the last Node to make it easier to append items. points to” means has a reference to. A Node doesn't contain the item . It has a reference to the item , which can be any Object . By pointing to, rather than containing the item , we can have one Node (and List ) implementation that works for all lists, regardless of what object type they hold.
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/29/2011 for the course CIVIL 1.00 taught by Professor Georgekocur during the Spring '05 term at MIT.

Page1 / 17

lecture_27_s2005 - 1.00 Lecture 27 Data Structures: Linked...

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

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