Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI 62 Data Structures Dr. Joshua Stough September 23, 2008.

Similar presentations


Presentation on theme: "CSCI 62 Data Structures Dr. Joshua Stough September 23, 2008."— Presentation transcript:

1 CSCI 62 Data Structures Dr. Joshua Stough September 23, 2008

2 Today Concordance Program Program 3 Lists

3 For handling varying amounts of data Grows and shrinks when necessary and does it fast Methods involving random access are slow. (book posits this without detail, because the detail is not the abstract part). –Insert –Find –Get

4 Lists: interface public interface List extends Structure { public int size(); // post: returns number of elements in list public boolean isEmpty(); // post: returns true iff list has no elements public void clear(); // post: empties list public void addFirst(E value); // post: value is added to beginning of list public void addLast(E value); // post: value is added to end of list public E getFirst(); // pre: list is not empty // post: returns first value in list

5 Lists:interface public E getLast(); // pre: list is not empty // post: returns last value in list public E removeFirst(); // pre: list is not empty // post: removes first value from list public E removeLast(); // pre: list is not empty // post: removes last value from list public E remove(E value); // post: removes and returns element equal to value // otherwise returns null public void add(E value); // post: value is added to tail of list public E remove(); // pre: list has at least one element // post: removes last value found in list

6 Lists: interface public E get(); // pre: list has at least one element // post: returns last value found in list public boolean contains(E value); // pre: value is not null // post: returns true iff list contains an object equal to value public int indexOf(E value); // pre: value is not null // post: returns (0-origin) index of value, // or -1 if value is not found public int lastIndexOf(E value); // pre: value is not null // post: returns (0-origin) index of value, // or -1 if value is not found public E get(int i); // pre: 0 <= i < size() // post: returns object found at that location

7 Lists: interface public E set(int i, E o); // pre: 0 <= i < size() // post: sets ith entry of list to value o; // returns old value public void add(int i, E o); // pre: 0 <= i <= size() // post: adds ith entry of list to value o public E remove(int i); // pre: 0 <= i < size() // post: removes and returns object found at that location public Iterator iterator(); // post: returns an iterator allowing // ordered traversal of elements in list

8 List extends Structure Anywhere is a Structure is desired, a list can serve. The interface makes implementation- independent decisions. –Add method adds to end of list. Could be set the last element of array Could be instantiate a new object and have current list reference it.

9 public static void main(String[] args) { // input is read from System.in Scanner s = new Scanner(System.in); String current; // current line // list of unique lines List lines = new SinglyLinkedList (); //A SinglyLinkedList implements the List interface. // read a list of possibly duplicated lines while (s.hasNextLine()) { current = s.nextLine(); // check to see if we need to add it if (!lines.contains(current)) { System.out.println(current); lines.add(current); }

10 Free Lists: managing a pool of resources class Space { // structure describing parking space public final static int COMPACT = 0; // small space public final static int MINIVAN = 1; // medium space public final static int TRUCK = 2; // large space protected int number; // address in parking lot protected int size; // size of space public Space(int n, int s) // post: construct parking space #n, size s { number = n; size = s; } public boolean equals(Object other) // pre: other is not null // post: true iff spaces are equivalent size { Space that = (Space)other; return this.size == that.size; }}

11 Free Lists: managing a pool of resources

12

13 In order to return a spot, similar to add: –Create the proposed Assoc (the one the user says they want to give back) –If it’s among the rented spaces: Remove from rented Add to free –Otherwise say couldn’t remove it.

14 Abstract Lists (extend AbstractStructure, implement List Abstract class may or may not contain abstract methods (declared without implementation). A subclass of an abstract class must implement must implement all abstract methods or else still be abstract.

15 Linked Lists Three classes (typically) working together –an “item” class one atomic unit of the aggregate data e.g., a “Name” class (item) might have two instance variables String first, last; –a “node” class one “item” and a reference to the next “node” the next reference is the “link” in “linked list” –a “list” class reference to the first “node”—head of the list

16 Linked Lists data Item nextNode Node List head Node

17 Linked Lists New List head Node List head Node

18 Linked Lists Find List head Node

19 Linked Lists Insert Node List head Node

20 Linked Lists Delete List head Node

21 Linked Lists: random stuff Generally: advantage to implementing some methods by calling others: –removeLast() -> remove(size()-1) –isEmpty() -> size() == 0 –addFirst(E e) -> add(0, e) Speed: –Remove/add to front is constant time –Remove/add to back or middle is O(n) How to make constant for addLast?


Download ppt "CSCI 62 Data Structures Dr. Joshua Stough September 23, 2008."

Similar presentations


Ads by Google