Linked Lists CSC 172 SPRING 2002 LECTURE 3 Data Structures? We make distinctions in the level of...

28
Linked Lists CSC 172 SPRING 2002 LECTURE 3

Transcript of Linked Lists CSC 172 SPRING 2002 LECTURE 3 Data Structures? We make distinctions in the level of...

Page 1: Linked Lists CSC 172 SPRING 2002 LECTURE 3 Data Structures?  We make distinctions in the level of abstraction  Abstract Data Type (ADT) - functionality/behavior.

Linked Lists

CSC 172

SPRING 2002

LECTURE 3

Page 2: Linked Lists CSC 172 SPRING 2002 LECTURE 3 Data Structures?  We make distinctions in the level of abstraction  Abstract Data Type (ADT) - functionality/behavior.

Data Structures?

We make distinctions in the level of abstractionAbstract Data Type (ADT) - functionality/behavior

Dictionary, Stack, Queue

Data Model – organizationList, Tree, Graph

Data Structure – implementationArray, Linked list, BST, Adjacency Matrix

Page 3: Linked Lists CSC 172 SPRING 2002 LECTURE 3 Data Structures?  We make distinctions in the level of abstraction  Abstract Data Type (ADT) - functionality/behavior.

The Dictionary ADT Computer programs commonly maintain a set of

values where we wish to Insert elements into the setDelete Elements from the setLook up elements (see if they are currently in the set)

There are lots of ways to implement this Some are more efficient than others

Page 4: Linked Lists CSC 172 SPRING 2002 LECTURE 3 Data Structures?  We make distinctions in the level of abstraction  Abstract Data Type (ADT) - functionality/behavior.

The Dictionary Interface

public interface Dictionary {

public abstract void insert(Object o);

public abstract void delete(Object o);

public abstract boolean lookup(Object o);

}

Page 5: Linked Lists CSC 172 SPRING 2002 LECTURE 3 Data Structures?  We make distinctions in the level of abstraction  Abstract Data Type (ADT) - functionality/behavior.

List Data Model A list is a finite sequence of zero or more elements

Grocery listLaundry list (a1,a2,…,an)

Formally, we can think of a list as either Empty, orAn element followed by a (possibly empty) list

Page 6: Linked Lists CSC 172 SPRING 2002 LECTURE 3 Data Structures?  We make distinctions in the level of abstraction  Abstract Data Type (ADT) - functionality/behavior.

Lists Empty (no elements) An element (head), followed by a list (tail)

Head sometimes called CARTail sometimes called CDR

Length of a list is 0 for an empty list1 + length(tail) for a non-empty list

Page 7: Linked Lists CSC 172 SPRING 2002 LECTURE 3 Data Structures?  We make distinctions in the level of abstraction  Abstract Data Type (ADT) - functionality/behavior.

How can we implement this?

We can implement a Dictionary ADT . . .

using a list data model . . .

with an array data structure . . .

or a linked-list data structure.

Page 8: Linked Lists CSC 172 SPRING 2002 LECTURE 3 Data Structures?  We make distinctions in the level of abstraction  Abstract Data Type (ADT) - functionality/behavior.

Arrays An array is a collection of data items of the same type Every element of the collection can be accessed

separately.

Object [] data = new Object[10];

Page 9: Linked Lists CSC 172 SPRING 2002 LECTURE 3 Data Structures?  We make distinctions in the level of abstraction  Abstract Data Type (ADT) - functionality/behavior.

Array Implementationpublic class myInfo implements Dictionary{

private int defaultCapacity = 100, length = 0;private Object[] datum;public myInfo() {

datum = new Object[defaultCapacity];} public myInfo(int initCapacity) {

datum = new Object[initCapacity];}

Page 10: Linked Lists CSC 172 SPRING 2002 LECTURE 3 Data Structures?  We make distinctions in the level of abstraction  Abstract Data Type (ADT) - functionality/behavior.

Implementationpublic class myInfo implements Dictionary{

private int defaultCapacity = 100, length = 0;private Object[] datum;public myInfo() {

datum = new Object[defaultCapacity];} public myInfo(int initCapacity) {

datum = new Object[initCapacity];}

Page 11: Linked Lists CSC 172 SPRING 2002 LECTURE 3 Data Structures?  We make distinctions in the level of abstraction  Abstract Data Type (ADT) - functionality/behavior.

Insert

Is this ok?

public void insert (Object o) {

datum[length++] = o;

}

What is the run time?

What if I have more than 100 elements?

Page 12: Linked Lists CSC 172 SPRING 2002 LECTURE 3 Data Structures?  We make distinctions in the level of abstraction  Abstract Data Type (ADT) - functionality/behavior.

Expand

private void expand(){Object[] tempData =

new Object[datum.length * 2];for (int j = 0 ; j<length;j++)

tempData[j] = datum[j];datum = tempData;

}So, what is the runtime of this

Page 13: Linked Lists CSC 172 SPRING 2002 LECTURE 3 Data Structures?  We make distinctions in the level of abstraction  Abstract Data Type (ADT) - functionality/behavior.

InsertBetter?

public void insert (Object o) {

if (length >= (datum.length –1)) expand();

datum[length++] = o;

}

This is what the Java class Vector gets you

What is the (worst case) run time, now?

Page 14: Linked Lists CSC 172 SPRING 2002 LECTURE 3 Data Structures?  We make distinctions in the level of abstraction  Abstract Data Type (ADT) - functionality/behavior.

Self-referential data typesclass Node {

private Object data; // the “data”

private Node next; // the “link”

}

data next

Page 15: Linked Lists CSC 172 SPRING 2002 LECTURE 3 Data Structures?  We make distinctions in the level of abstraction  Abstract Data Type (ADT) - functionality/behavior.

Linked List

A linked list “has-a” reference to a node The “head” of the listThe ending node has “null” for the next

data next data Next

head

data Next

null

Page 16: Linked Lists CSC 172 SPRING 2002 LECTURE 3 Data Structures?  We make distinctions in the level of abstraction  Abstract Data Type (ADT) - functionality/behavior.

Linked-List Implementation

public class myInfo implements Dictionary{

private int length = 0;

private Node head = null;

Page 17: Linked Lists CSC 172 SPRING 2002 LECTURE 3 Data Structures?  We make distinctions in the level of abstraction  Abstract Data Type (ADT) - functionality/behavior.

Adding a first node

Page 18: Linked Lists CSC 172 SPRING 2002 LECTURE 3 Data Structures?  We make distinctions in the level of abstraction  Abstract Data Type (ADT) - functionality/behavior.

Adding a first node

public void insert(Object obj){

Node newLink = new Node();

newLink.data = obj;

newLink.next = head;

head = newLink;

}

What is the run time of this?

Page 19: Linked Lists CSC 172 SPRING 2002 LECTURE 3 Data Structures?  We make distinctions in the level of abstraction  Abstract Data Type (ADT) - functionality/behavior.

Lookup: Array

public boolean lookup(Object o){

for (int j = 0 ; j < length;j++)

if (datum[j].equals(o)) return true;

return false;

}

Run time?

Page 20: Linked Lists CSC 172 SPRING 2002 LECTURE 3 Data Structures?  We make distinctions in the level of abstraction  Abstract Data Type (ADT) - functionality/behavior.

Lookup: Linked List

public boolean lookup(Object o){Object temp = head; while (temp != null){

if ((temp.data).equals(o)) return true;temp = temp.next;

}return false;

}

Run time?

Page 21: Linked Lists CSC 172 SPRING 2002 LECTURE 3 Data Structures?  We make distinctions in the level of abstraction  Abstract Data Type (ADT) - functionality/behavior.

Lookup: Linked List, recursive

public boolean lookup(Object o){return lookupNode(o,head);

}public boolean lookupNode(Object o, Node n){

if (n == null) return false;else if((n.data).equals(o) return true;

else return lookupNode(o,n.next);}

Run time?

Page 22: Linked Lists CSC 172 SPRING 2002 LECTURE 3 Data Structures?  We make distinctions in the level of abstraction  Abstract Data Type (ADT) - functionality/behavior.

Deleting a node from a List

Page 23: Linked Lists CSC 172 SPRING 2002 LECTURE 3 Data Structures?  We make distinctions in the level of abstraction  Abstract Data Type (ADT) - functionality/behavior.

Delete: Linked List, recursive

public boolean delete(Object o){

return deleteNode(o,head);

}

Page 24: Linked Lists CSC 172 SPRING 2002 LECTURE 3 Data Structures?  We make distinctions in the level of abstraction  Abstract Data Type (ADT) - functionality/behavior.

Delete: Linked List, recursive

public void deleteNode(Object o, Node n){if (n == null) return ; if (n.next == null) return;else if(((n.next).data).equals(o)) {

n.next = (n.next).nextreturn ;}else return deleteNode(o,n.next);

} Run time?

Page 25: Linked Lists CSC 172 SPRING 2002 LECTURE 3 Data Structures?  We make distinctions in the level of abstraction  Abstract Data Type (ADT) - functionality/behavior.

Delete: Array

We have to look it up O(n) Deletion is setting it to null But, we have to “shift” all the remaining elements.

Page 26: Linked Lists CSC 172 SPRING 2002 LECTURE 3 Data Structures?  We make distinctions in the level of abstraction  Abstract Data Type (ADT) - functionality/behavior.

Delete: Array

public void delete(Object o){for (int j = 0 ; j < length;j++)

if (datum[j].equals(o)) {for (int k = j; k<(length-1);k++)

datum[k] = datum[k+1];return;

};}Run time?

Page 27: Linked Lists CSC 172 SPRING 2002 LECTURE 3 Data Structures?  We make distinctions in the level of abstraction  Abstract Data Type (ADT) - functionality/behavior.

Compare (worst case) Run Time (what about space?)

ARRAY LINKED-LIST

Insert N (in case of expansion)

1

Lookup N N

Delete N (lookup + shift) N (for lookup)

Page 28: Linked Lists CSC 172 SPRING 2002 LECTURE 3 Data Structures?  We make distinctions in the level of abstraction  Abstract Data Type (ADT) - functionality/behavior.

Workshop sing-up

. The coefficient for the workshop differential is positive and significant. On average, each additional workshop attended in CSC 172 increases the final grade point by 0.19. In substantive terms, this means that a student who attends five additional workshops in CSC 172 should expect his/her grade to increase by almost one full letter grade.