Week 4 - Friday What did we talk about last time? Linked lists You - - PowerPoint PPT Presentation

week 4 friday what did we talk about last time linked
SMART_READER_LITE
LIVE PREVIEW

Week 4 - Friday What did we talk about last time? Linked lists You - - PowerPoint PPT Presentation

Week 4 - Friday What did we talk about last time? Linked lists You are given a reference to a node in a singly linked list and some data You need to insert the data into the node in front of the node you are given a reference to


slide-1
SLIDE 1

Week 4 - Friday

slide-2
SLIDE 2

 What did we talk about last time?  Linked lists

slide-3
SLIDE 3
slide-4
SLIDE 4
slide-5
SLIDE 5
slide-6
SLIDE 6

 You are given a reference to a node in a singly linked list and

some data

 You need to insert the data into the node in front of the node

you are given a reference to

 You do not have the head reference, you do not have a

reference to the previous node

 How do you do the insertion?

slide-7
SLIDE 7
slide-8
SLIDE 8

 The generic part is really easy once you get the syntax set up  You use T instead of int (or whatever type you designed your

linked lists to hold)

 The trickier thing is to define an iterator class that can keep

track of where you are inside the list

  • It has to be a non-static inner class!
slide-9
SLIDE 9

public class LinkedList<T> implements Iterable<T> { private class Node { public T data; public Node next; public Node previous; } private class ListIterator implements Iterator<T> { public Node current; … } private Node head = null; private Node tail = null; public int size = 0; … }

slide-10
SLIDE 10

Create a new iterator that points at the head of the list

slide-11
SLIDE 11

Whether or not there is something in the current spot in the list

slide-12
SLIDE 12

Get the current thing in the list

slide-13
SLIDE 13

Remove the current item from the list (optional)

slide-14
SLIDE 14
slide-15
SLIDE 15

 Linked lists can be made circular such that the last node

points back at the head node

 This organization is good for situations in which we want to

cycle through all of the nodes in the list repeatedly

tail 23 47 58

slide-16
SLIDE 16

 Insert at front (or back)

  • Θ(1)

 Delete at front

  • Θ(1)
  • Delete at back costs Θ(n) unless we used doubly linked lists

 Search

  • Θ(n)
slide-17
SLIDE 17

 We can design linked lists with multiple pointers

in some nodes

 We want ½ of the nodes to have 1 pointer, ¼ of

the nodes to have 2 pointers, 1/8 of the nodes to have 3 pointers…

head 14 5 3 29 28 41 58

X X X

slide-18
SLIDE 18

 If ordered, search is

  • Θ(log n)

 Go to index is

  • Θ(log n)

 Insert at end

  • Θ(log n)

 Delete

  • Totally insane, at least Θ(n)

 Trees end up being a better alternative

slide-19
SLIDE 19

 We want to make items that are used frequently easy to get at  Several different approaches, mostly based on finding items

repeatedly

  • Move to front: After finding an item, put it in the front
  • Transpose: After finding an item, move it up by one
  • Count: Keep the list ordered by how often you get a particular item

(requires a counter in each node)

  • Ordering: Sort the list according to some feature of the data
slide-20
SLIDE 20
slide-21
SLIDE 21

 Dynamic array

  • Advantages: pop and top are O(1)
  • Disadvantages: limited size, making push O(n) in the worst case (still

O(1) amortized)

 Linked list

  • Advantages: push, pop, and top are O(1)
  • Disadvantages: slightly slower than the array version, considerably

more memory overhead

slide-22
SLIDE 22

public class ListStack { private static class Node { public String data; public Node next; } private Node top = null; private int size = 0; public void push(String value) {} public String pop() {} public String peek() {} //instead of top public int size() {} }

slide-23
SLIDE 23
slide-24
SLIDE 24
slide-25
SLIDE 25
slide-26
SLIDE 26
slide-27
SLIDE 27

 Circular array

  • Advantages: dequeue and front are O(1)
  • Disadvantages: limited size, making enqueue O(n) in the worst case

(still O(1) amortized)

 Linked list

  • Advantages: enqueue, dequeue, and front are O(1)
  • Disadvantages: slightly slower than the array version, considerably

more memory overhead

slide-28
SLIDE 28

class ListQueue { private class Node { public String data; public Node next; } private Node head = null; private Node tail = null; private int size = 0; public void enqueue(String value) {} public String dequeue() {} public String front() {} public int size() {} }

slide-29
SLIDE 29
slide-30
SLIDE 30
slide-31
SLIDE 31
slide-32
SLIDE 32
slide-33
SLIDE 33
slide-34
SLIDE 34

 Finish linked list queue implementation  Recursion

slide-35
SLIDE 35

 Finish Assignment 2

  • Due tonight by midnight!

 Keep working on Project 1

  • Don't fall behind!