Linked Lists - PowerPoint PPT Presentation

About This Presentation
Title:

Linked Lists

Description:

What is the (worst case) run time, now? Self-referential data types. class Node ... Run time? Delete: Array. We have to look it up O(n) Deletion is setting it to null ... – PowerPoint PPT presentation

Number of Views:85
Avg rating:3.0/5.0
Slides: 29
Provided by: thaddeusf
Category:
Tags: linked | lists

less

Transcript and Presenter's Notes

Title: Linked Lists


1
Linked Lists
  • CSC 172
  • SPRING 2002
  • LECTURE 3

2
Data Structures?
  • We make distinctions in the level of abstraction
  • Abstract Data Type (ADT) - functionality/behavior
  • Dictionary, Stack, Queue
  • Data Model organization
  • List, Tree, Graph
  • Data Structure implementation
  • Array, Linked list, BST, Adjacency Matrix

3
The Dictionary ADT
  • Computer programs commonly maintain a set of
    values where we wish to
  • Insert elements into the set
  • Delete Elements from the set
  • Look up elements (see if they are currently in
    the set)
  • There are lots of ways to implement this
  • Some are more efficient than others

4
The Dictionary Interface
  • public interface Dictionary
  • public abstract void insert(Object o)
  • public abstract void delete(Object o)
  • public abstract boolean lookup(Object o)

5
List Data Model
  • A list is a finite sequence of zero or more
    elements
  • Grocery list
  • Laundry list
  • (a1,a2,,an)
  • Formally, we can think of a list as either
  • Empty, or
  • An element followed by a (possibly empty) list

6
Lists
  • Empty (no elements)
  • An element (head), followed by a list (tail)
  • Head sometimes called CAR
  • Tail sometimes called CDR
  • Length of a list is
  • 0 for an empty list
  • 1 length(tail) for a non-empty list

7
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.

8
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 Object10

9
Array Implementation
  • public class myInfo implements Dictionary
  • private int defaultCapacity 100, length 0
  • private Object datum
  • public myInfo()
  • datum new ObjectdefaultCapacity
  • public myInfo(int initCapacity)
  • datum new ObjectinitCapacity

10
Implementation
  • public class myInfo implements Dictionary
  • private int defaultCapacity 100, length 0
  • private Object datum
  • public myInfo()
  • datum new ObjectdefaultCapacity
  • public myInfo(int initCapacity)
  • datum new ObjectinitCapacity

11
Insert
  • Is this ok?
  • public void insert (Object o)
  • datumlength o
  • What is the run time?
  • What if I have more than 100 elements?

12
Expand
  • private void expand()
  • Object tempData
  • new Objectdatum.length 2
  • for (int j 0 jltlengthj)
  • tempDataj datumj
  • datum tempData
  • So, what is the runtime of this

13
Insert
  • Better?
  • public void insert (Object o)
  • if (length gt (datum.length 1)) expand()
  • datumlength o
  • This is what the Java class Vector gets you
  • What is the (worst case) run time, now?

14
Self-referential data types
  • class Node
  • private Object data // the data
  • private Node next // the link

15
Linked List
  • A linked list has-a reference to a node
  • The head of the list
  • The ending node has null for the next

head
16
Linked-List Implementation
  • public class myInfo implements Dictionary
  • private int length 0
  • private Node head null

17
Adding a first node
18
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?

19
Lookup Array
  • public boolean lookup(Object o)
  • for (int j 0 j lt lengthj)
  • if (datumj.equals(o)) return true
  • return false
  • Run time?

20
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?

21
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?

22
Deleting a node from a List
23
Delete Linked List, recursive
  • public boolean delete(Object o)
  • return deleteNode(o,head)

24
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).next
  • return
  • else return deleteNode(o,n.next)
  • Run time?

25
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.

26
Delete Array
  • public void delete(Object o)
  • for (int j 0 j lt lengthj)
  • if (datumj.equals(o))
  • for (int k j klt(length-1)k)
  • datumk datumk1
  • return
  • Run time?

27
Compare (worst case) Run Time (what about space?)
28
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.
Write a Comment
User Comments (0)
About PowerShow.com