Top Banner
CSC 212 – Data Structures Lecture 20: Deques
15

CSC 212 – Data Structures Lecture 20: Deques. Question of the Day How did the clerk know that the man telling the following story is a fraud? I hope you.

Dec 28, 2015

Download

Documents

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: CSC 212 – Data Structures Lecture 20: Deques. Question of the Day How did the clerk know that the man telling the following story is a fraud? I hope you.

CSC 212 –Data Structures

Lecture 20:

Deques

Page 2: CSC 212 – Data Structures Lecture 20: Deques. Question of the Day How did the clerk know that the man telling the following story is a fraud? I hope you.

Question of the Day

How did the clerk know that the man telling the following story is a fraud?I hope you can help me. I'm an English Professor and I find myself in need of help. My wife and oldest daughter went shopping with my wallet which contained my cash and my credit cards and my identification and all that. My other daughter has taken ill in our hotel room. I must buy her some medication immediately, but I have no money. I have a check in my jacket pocket, but I, of course, have no identification.

older

Page 3: CSC 212 – Data Structures Lecture 20: Deques. Question of the Day How did the clerk know that the man telling the following story is a fraud? I hope you.

Debugging a Program

Debug program when not working properlyCould be an error in the codeProgram logic could be incorrectDesign may not allow certain features

Better to find a bug as early as possibleEasiest to fix when still on paperUnderstood best immediately after writing it

This is NOT just a programming issueDebugging is best when starting early

Page 4: CSC 212 – Data Structures Lecture 20: Deques. Question of the Day How did the clerk know that the man telling the following story is a fraud? I hope you.

Starting the Program

How to start a program1. Determine what the problem is asking

2. Come up with a good set of tests cases Test cases crucial for working programs

After all, how else can you know it works? Include a range of different possible inputs For each input determine the correct output

Page 5: CSC 212 – Data Structures Lecture 20: Deques. Question of the Day How did the clerk know that the man telling the following story is a fraud? I hope you.

Test Cases

Begin with description of input and output How else do you know where to start? How else can you check if you are correct?

Break up into different possible outcomes Example: rounding float to nearest int

Positive rounding up -- 3.6. 14.5. 99.9999 Positive rounding down -- 3.2, 12.1, 99.00001 Negative rounding down -- -3.6, -313.9, -334.51 Negative rounding up -- -3.1, -313.001, -334.49 Whole numbers -- 6, -4, 0

Page 6: CSC 212 – Data Structures Lecture 20: Deques. Question of the Day How did the clerk know that the man telling the following story is a fraud? I hope you.

Begin to Develop Algorithm

Much easier to start on paperEasier to write, test, and fix

Break problem into smaller stepsEach step need not be easy or obviousBreak test cases to test each stepDo not worry solving subproblems yet

Use test cases to verify algorithm works If it does not, go back and fix those problems

Page 7: CSC 212 – Data Structures Lecture 20: Deques. Question of the Day How did the clerk know that the man telling the following story is a fraud? I hope you.

Solving Subproblems

Solve subproblems separatelyDo not worry about larger problem or issuesCan break up into even smaller subproblemsWorry about code only when it is obvious

Solving problems in any part of lifeSolving large problems is hardSolving small problems is not

Page 8: CSC 212 – Data Structures Lecture 20: Deques. Question of the Day How did the clerk know that the man telling the following story is a fraud? I hope you.

Stack Interface

public interface Collection {public int size();public boolean isEmpty();

}

public interface Stack<E> extends Collection {public E top() throws EmptyStackException;public E pop() throws EmptyStackException;public void push(E element);

}

Page 9: CSC 212 – Data Structures Lecture 20: Deques. Question of the Day How did the clerk know that the man telling the following story is a fraud? I hope you.

Queue Interface

public interface Queue<E> extends Collection {public E front() throws EmptyQueueException;public E dequeue() throws EmptyQueueException;public void enqueue(E element);

}

Page 10: CSC 212 – Data Structures Lecture 20: Deques. Question of the Day How did the clerk know that the man telling the following story is a fraud? I hope you.

Stacks vs. Queues

Stacks access data using LIFO orderLast In-First OutGood for the perpetually late

Queues access data in FIFO orderFirst In-First OutResembles how we handle lines

Page 11: CSC 212 – Data Structures Lecture 20: Deques. Question of the Day How did the clerk know that the man telling the following story is a fraud? I hope you.

Limitations of these ADTs

Need to access both sides of CollectionTransplant waiting listsHelp center phone banksMy Grandpa playing cards

Cannot be done with StackCan only insert at & access top

Cannot be done with QueueCan only add to end, get element from front

Page 12: CSC 212 – Data Structures Lecture 20: Deques. Question of the Day How did the clerk know that the man telling the following story is a fraud? I hope you.

Deque ADT

Pronounced “deck”Avoids mistaking it for dequeue()Stands for Double Ended QUEue

Elements added & removed from front & rear Can be implemented with array or linked list

Really needs doubly-linked list Combines Stack and Queue ADT

Page 13: CSC 212 – Data Structures Lecture 20: Deques. Question of the Day How did the clerk know that the man telling the following story is a fraud? I hope you.

Deque Interface

public interface Deque<E> extends Collection {public E getFirst() throws EmptyDequeException;public E getLast() throws EmptyDequeException;public E removeFirst() throws EmptyDequeException;public E removeLast() throws EmptyDequeException;public E addFirst();public E addLast();

}

Page 14: CSC 212 – Data Structures Lecture 20: Deques. Question of the Day How did the clerk know that the man telling the following story is a fraud? I hope you.

Your Turn

Get back into groups and do activity

Page 15: CSC 212 – Data Structures Lecture 20: Deques. Question of the Day How did the clerk know that the man telling the following story is a fraud? I hope you.

Before Next Lecture…

Keep up with your reading! Start Week #9 Assignment Work on Programming Assignment #2