+ All Categories

CS221

Date post: 08-Jan-2016
Category:
Upload: purity
View: 40 times
Download: 1 times
Share this document with a friend
Description:
Week 4 – Monday. CS221. Last time. What did we talk about last time? Linked list implementations Generic linked lists Iterators. Questions?. Project 1. Bitmap Manipulator. JCF List. JCF L ist usage. import java.util .*; public class Testing { - PowerPoint PPT Presentation
36
CS221 Week 4 - Monday
Transcript
Page 1: CS221

CS221Week 4 - Monday

Page 2: CS221

Last time

What did we talk about last time? Stacks Implementing stacks with arrays

Page 3: CS221

Questions?

Page 4: CS221

Bitmap Manipulator

Project 1

Page 5: CS221

Queues

Page 6: CS221

Queue

A queue is a simple data structure that has three basic operations (very similar to a stack) Enqueue Put an item at the back of

the queue DequeueRemove an item from the

front of the queue Front Return the item at the front

of the queue A queue is considered FIFO (First In First

Out) or LILO (Last In Last Out)

Page 7: CS221

Application of queues

Queues are useful whenever you want to keep track of the order of arrival A line in a fast food restaurant A job in a printer queue A buffer for managing data

Page 8: CS221

Circular array implementation

A queue is a little bit harder to implement than a stack with an array

The trouble is that you're enqueuing and dequeuing from different ends

Removing something from the front seems to imply that you'll need to shift over all the contents of the array

Enter the circular array!

Page 9: CS221

Circular array

A circular array is just a regular array

However, we keep a start index as well as a size that lets us start the array at an arbitrary point

Then, the contents of the array can go past the end of the array and wrap around

The modulus operator (%) is a great way to implement the wrap around

Page 10: CS221

Circular array example

1. Starting array

2. Enqueue 9

3. Dequeue

4. Dequeue

5. Enqueue 14

6. Dequeue18 3 21 9

Start

Size = 4

7 18 3 21 9

Start

Size = 5

7 18 3 21

Start

Size = 4

14 3 21 9

Start

Size = 4

14 21 9

Start

Size = 3

3 21 9

Start

Size = 3

Page 11: CS221

Circular array implementation

Advantages: Dequeue is Θ(1) Front is Θ(1)

Disadvantages Enqueue is Θ(n) in the very worst case,

but not in the amortized case

Page 12: CS221

Circular array implementation

public class ArrayQueue {private int[] data = new int[10];private int start = 0;private int size = 0;

public void enqueue(int value) {}public int dequeue() {}public int front() {}

public int size() {}}

Page 13: CS221

Circular Array Front

Page 14: CS221

Circular Array Get Size

Page 15: CS221

Circular Array Enqueue

Page 16: CS221

Circular Array Dequeue

Page 17: CS221

JCF Stacks and Queues

Page 18: CS221

Deque<T>

Java does have a Stack class which extends Vector

The Deque (double ended queue) interface is preferred

A double ended queue can be used as either stack or queue

Stack Operati

on Deque Method

Push addFirst(T element)

Pop removeFirst()

Top peekFirst()

Size size()

Queue Operati

on Deque Method

Enqueue addLast(T element)

Dequeue removeFirst()

Front peekFirst()

Size size()

Page 19: CS221

ArrayDeque<T>

Since Deque is an interface, we have to have classes that can implement it

ArrayDeque is an implementation of a double ended queue that uses a circular array for backing

Probably the best choice for both queues and stacks in terms of speed and memory use addFirst() (push) is O(1) amortized addLast() (enqueue) is O(1) amortized removeFirst() (pop and dequeue) is O(1) peekFirst() (top and front) is O(1)

Page 20: CS221

LinkedList<T>

Good old LinkedList is an implementation of a double ended queue that uses a doubly linked list for backing

Generally slower than ArrayDeque, but the important operations are O(1) without being amortized addFirst() (push) is O(1) addLast() (enqueue) is O(1) removeFirst() (pop and dequeue) is O(1) peekFirst() (top and front) is O(1)

Page 21: CS221

Priority queues

A priority queue is like a regular queue except that items are not always added at the end

They are added to the place they need to be in order to keep the queue sorted in priority order

Not all requests are created equal A higher priority job can come along and jump in

front of a lower priority job Unfortunately, we have to wait for the heap

data structure to implement priority queues efficiently

Page 22: CS221

Linked Lists

Page 23: CS221

Purpose and design of linked listsImpromptu student lecture

Page 24: CS221

Linked lists

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

for everything?

X

head

23 47 58

Page 25: CS221

Pros

Insert at front (or back) O(1)

Delete at front (or back) O(1)

Arbitrary amounts of storage with low overhead

Page 26: CS221

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 27: CS221

Implementations

Page 28: CS221

Levels of flexibility

Class protecting nodes implementation

Generic class providing nodes with arbitrary type

Generic class with the addition of iterators

Page 29: CS221

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 30: CS221

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 31: CS221

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 32: CS221

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 33: CS221

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 34: CS221

Upcoming

Page 35: CS221

Next time…

Implementation of a linked list Circular linked lists and skip lists Implementing a stack with a linked

list Keep reading section 1.3

Page 36: CS221

Reminders

Keep reading section 1.3 Keep working on Project 1

Due this Friday, September 22 by 11:59pm


Recommended