Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays."— Presentation transcript:

1 Week 4 - Monday

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

3

4 Bitmap Manipulator

5  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/

6  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 memory temp = head; Node temp = new Node(); //wastes memory temp = head; public boolean contains(int element) { Node temp = head; while(temp.next != null){ //two different bugs if( temp.value == element ) return true; temp = temp.next; } return false; } public boolean contains(int element) { Node temp = head; while(temp.next != null){ //two different bugs if( temp.value == element ) return true; temp = temp.next; } return false; }

7  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 contains(int element) { return indexOf(element) != -1; } public boolean isEmpty() { return size == 0; } public boolean isEmpty() { return size == 0; }

8

9 Impromptu student lecture

10  What is a linked list?  Why not just use (dynamic) arrays for everything? X head 23 47 58

11  Insert at front (or back)  O(1)  Delete at front (or back)  O(1)  Arbitrary amounts of storage with low overhead

12  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++)

13

14  Class protecting nodes implementation  Generic class providing nodes with arbitrary type  Generic class with the addition of iterators

15  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; } public static int sum( int[] array ) { int total = 0; for( int value: array ) total += value; return total; }

16  Foreach loops work for any iterable list of any type public static double weigh(LinkedList list) { double total = 0.0; for( Wombat wombat: list ) total += wombat.getWeight(); return total; } public static double weigh(LinkedList list) { double total = 0.0; for( Wombat wombat: list ) total += wombat.getWeight(); return total; } public static double weigh(ArrayList list) { double total = 0.0; for( Wombat wombat: list ) total += wombat.getWeight(); return total; } public static double weigh(ArrayList 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; } public static double weigh(Wombat[] list) { double total = 0.0; for( Wombat wombat: list ) total += wombat.getWeight(); return total; }

17  Node consists of data and a single next pointer  Advantages: fast and easy to implement  Disadvantages: forward movement only X head 23 47 58

18  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 X head 23 47 58 X tail

19  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?

20

21  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; … }

22

23

24

25

26

27  Continued implementation of a linked list  Circular linked lists and skip lists  Implementing a stack with a linked list  Keep reading section 1.3

28  Keep working on Project 1  Due this Friday, September 18 by 11:59pm


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

Similar presentations


Ads by Google