Top Banner
CS221 Week 4 - Monday
28

Week 4 - Monday. What did we talk about last time? Queues Implementing queues with circular arrays.

Dec 14, 2015

Download

Documents

Rolf Short
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.

CS221Week 4 - Monday

Page 2: Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.

Last time

What did we talk about last time? Queues Implementing queues with circular

arrays

Page 3: Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.

Questions?

Page 4: Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.

Bitmap Manipulator

Project 1

Page 5: Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.

Assignment 1 issues

Most people messed up the add() method that takes an index

Most people didn't throw exceptions at quite the right times

There was a lot of inconsistent formatting I don't specify what your formatting should be

too closely, but it should be consistent Check out the coding standards for the course:▪ http://users.etown.edu/w/wittmanb/cs221/standards/

Page 6: Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.

Specific Assignment 1 don'ts Don't create a new Node unless you have to:

Never look at the next thing unless you have to:

Node temp = new Node(); //wastes memorytemp = head;

public boolean contains(int element) {Node temp = head;while(temp.next != null){ //two different bugsif( temp.value == element )return true;temp = temp.next;}return false;

}

Page 7: Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.

Specific Assignment 1 do's Use other methods to simplify code:

Except when it doesn't make sense Using get() in addAll() is inefficient

Keep it simple:

Test, test, test!

public boolean contains(int element) {return indexOf(element) != -1;

}

public boolean isEmpty() {return size == 0;

}

Page 8: Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.

Linked Lists

Page 9: Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.

Purpose and design of linked listsImpromptu student lecture

Page 10: Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.

Linked lists

What is a linked list? Why not just use (dynamic) arrays

for everything?

X

head

23 47 58

Page 11: Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.

Pros

Insert at front (or back) O(1)

Delete at front (or back) O(1)

Arbitrary amounts of storage with low overhead

Page 12: Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.

Cons

Search O(n)

Go to index O(n)

Potentially significant memory overhead if data is small

Much easier to make pointer and memory errors (especially in C/C++)

Page 13: Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.

Implementations

Page 14: Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.

Levels of flexibility

Class protecting nodes implementation

Generic class providing nodes with arbitrary type

Generic class with the addition of iterators

Page 15: Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.

Wait, what's an iterator?

I'm glad you asked They allow a collection to be used in a foreach loop So, what's a foreach loop?

It allows you to read (but not change) each value in a list

public static int sum( int[] array ) {int total = 0;for( int value: array )

total += value;return total;

}

Page 16: Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.

So what?

Foreach loops work for any iterable list of any type

public static double weigh(LinkedList<Wombat> list) {double total = 0.0;for( Wombat wombat: list )

total += wombat.getWeight();return total;

}

public static double weigh(ArrayList<Wombat> list) {double total = 0.0;for( Wombat wombat: list )

total += wombat.getWeight();return total;

}

public static double weigh(Wombat[] list) {double total = 0.0;for( Wombat wombat: list )

total += wombat.getWeight();return total;

}

Page 17: Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.

Singly linked list

Node consists of data and a single next pointer

Advantages: fast and easy to implement

Disadvantages: forward movement only

X

head

23 47 58

Page 18: Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.

Doubly linked list

Node consists of data, a next pointer, and a previous pointer

Advantages: bi-directional movement Disadvantages: slower, 4 pointers must

change for every insert/delete Xhead

23 47 58

X tail

Page 19: Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.

Interview question

You are given a singly linked list It may have a loop in it, that is, a

node that points back to an earlier node in the list

If you try to visit every node in the list, you’ll be in an infinite loop

How can you see if there is a loop in a linked list?

Page 20: Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.

Code

Page 21: Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.

Definition

Let’s try a simple definition for a linked list:public class LinkedList {

private static class Node {public int data;public Node next;public Node previous;

}

private Node head = null;private Node tail = null;…

}

Page 22: Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.

Insert at head

Page 23: Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.

Delete from head

Page 24: Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.

Find

Page 25: Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.

Insert in order

Page 26: Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.

Upcoming

Page 27: Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.

Next time…

Continued implementation of a linked list

Circular linked lists and skip lists Implementing a stack with a linked

list Keep reading section 1.3

Page 28: Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.

Reminders

Keep reading section 1.3 Keep working on Project 1

Due this Friday, September 18 by 11:59pm