CSCI 62
Data Structures
Dr. Joshua Stough
September 23, 2008
Today
• Concordance Program
• Program 3
• Lists
Lists
• 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
Lists: interface
public interface List<E> extends Structure<E>
{
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
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
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
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<E> iterator();
// post: returns an iterator allowing
// ordered traversal of elements in list
List extends Structure
• Anywhere is a Structure is desired, a list
can serve.
• The interface makes implementationindependent 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.
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<String> lines = new SinglyLinkedList<String>();
//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);
}
}
}
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;
}}
Free Lists: managing a pool of
resources
Free Lists: managing a pool of
resources
Free Lists: managing a pool of
resources
• In order to return a spot, similar to add:
– Create the proposed Assoc<String,Space>
(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.
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.
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
Linked Lists
List
Item
data
Node
Item
nextNode
head
Node
Node
Node
Node
Node
Node
Linked Lists
New
List
head
Node
Node
Node
Node
Node
Node
List
head
Node
Node
Node
Node
Node
Node
Node
Linked Lists
Find
List
head
Node
Node
Node
Node
Node
Node
Linked Lists
Insert
List
head
Node
Node
Node
Node
Node
Node
Node
Linked Lists
Delete
List
head
Node
Node
Node
Node
Node
Node
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?
© Copyright 2026 Paperzz