H OMEWORK 2 Q UIZ E XPLANATIONS, N OTES A BOUT L INKED L ISTS I NSIDE O BJECTS, AND A B IT ABOUT S TREAMS.

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Stack & Queues COP 3502.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only 
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
Main Index Contents 11 Main Index Contents Model for a Queue Model for a Queue The Queue The Queue ADTQueue ADT (3 slides) Queue ADT Radix Sort Radix Sort.
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
Stacks (Revised and expanded from CIT 591). What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
CSC 212 – Data Structures Lecture 21: IndexList a/k/a Vector, ArrayList.
Stacks, Queues, and Deques
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 8 Ming Li Department of.
Stacks, Queues, and Deques
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
CSE 143 Lecture 7 Stacks and Queues reading: "Appendix Q" (see course website) slides created by Marty Stepp and Hélène Martin
Stacks and Queues Introduction to Computing Science and Programming I.
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Stacks and Queues Pepper. Why History Simplicity Operating Systems – Function Call Stack.
Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
CMSC 202 Stacks and Queues. What’s a Queue? A queue is a linear collection of homogeneous data in which items added to the queue must be placed at the.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
COP INTERMEDIATE JAVA Data Structures. A data structure is a way of organizing a collection of data so that it can be manipulated effectively. A.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Arrays and Collections Tonga Institute of Higher Education.
1 Working with Pointers An exercise in destroying your computer.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ Stack ◦ Queue & Priority Queue 2.
Question of the Day  Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.
CS1020E Lab 4 (Stack and Queue)
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
I NTERFACES, A BSTRACT C LASSES, AND D ATA S TRUCTURES.
1. What is it? It is a queue that access elements according to their importance value. Eg. A person with broken back should be treated before a person.
Give Eg:? Queues. Introduction DEFINITION: A Queue is an ordered collection of element in which insertions are made at one end and deletions are made.
Question of the Day  How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
CS1020 – Data Structures And Algorithms 1 AY Semester 2
4. Java language basics: Function
Queues.
QueueStack CS1020.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Arrays & Functions Lesson xx
Popping Items Off a Stack Lesson xx
Stacks and Queues CLRS, Section 10.1.
slides created by Alyssa Harding
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Stacks and Queues.
More Data Structures (Part 1)
Lab4 problems More about templates Some STL
Generics, Stack, Queue Based on slides by Alyssa Harding
Presentation transcript:

H OMEWORK 2 Q UIZ E XPLANATIONS, N OTES A BOUT L INKED L ISTS I NSIDE O BJECTS, AND A B IT ABOUT S TREAMS

W HEN YOU CHECKOUT AT A STORE, YOU LINE UP AND WAIT YOUR TURN. W HICH OF THE DATA STRUCTURES FROM THIS ASSIGNMENT BEST CAPTURES THE ORDER IN WHICH PEOPLE ENTER AND EXIT THE CHECKOUT LINE ? Stack Queue Priority Queue

W HEN YOU CHECKOUT AT A STORE, YOU LINE UP AND WAIT YOUR TURN. W HICH OF THE DATA STRUCTURES FROM THIS ASSIGNMENT BEST CAPTURES THE ORDER IN WHICH PEOPLE ENTER AND EXIT THE CHECKOUT LINE ? Stack Stacks are last in, first out, meaning the last one added to the stack is the first one that is then removed. In this example, that would mean whoever showed up to the check out line would immediately go to the front of the line, not the back, and would be helped next. Queue Priority Queue

W HEN YOU CHECKOUT AT A STORE, YOU LINE UP AND WAIT YOUR TURN. W HICH OF THE DATA STRUCTURES FROM THIS ASSIGNMENT BEST CAPTURES THE ORDER IN WHICH PEOPLE ENTER AND EXIT THE CHECKOUT LINE ? Stack Queue Priority Queue Priority queue means that if someone showed up (who was maybe considered “more important” than the other people in the line), they would go to the front, and everyone else would queue up in line the normal way. That’s not what was described above, so this is incorrect.

W HEN YOU CHECKOUT AT A STORE, YOU LINE UP AND WAIT YOUR TURN. W HICH OF THE DATA STRUCTURES FROM THIS ASSIGNMENT BEST CAPTURES THE ORDER IN WHICH PEOPLE ENTER AND EXIT THE CHECKOUT LINE ? Stack Queue is correct. Queues are “FIFO” (first in, first out), meaning that whoever shows up first is the first one to then be removed. Priority Queue

E ACH OF THE THREE DATA STRUCTURES ON THIS ASSIGNMENT HAS OPERATIONS THAT ADD ELEMENTS, INSPECT THE " FIRST " ELEMENT, AND CHECK FOR ELEMENTS. A SSUME YOU DID THE FOLLOWING SEQUENCE OF OPERATIONS ( RENAMED TO MATCH THOSE IN THE INTERFACE, SO WOULD BE ONE OF THE CONCRETE Q UEUE, S TACK, ETC CLASSES, " INSERT " WOULD BE ONE OF ENQUEUE, PUSH, ETC ): NEW. INSERT (5). INSERT (7). INSERT (4). FIRST () I F THIS SEQUENCE RETURNED 4, WHICH DATA STRUCTURE WAS BEING USED ? We don't know -- it could be any of them Must be a stack Must be a queue Must be a priority queue Either a stack or a priority queue Either a queue or a priority queue Either a stack or a queue

E ACH OF THE THREE DATA STRUCTURES ON THIS ASSIGNMENT HAS OPERATIONS THAT ADD ELEMENTS, INSPECT THE " FIRST " ELEMENT, AND CHECK FOR ELEMENTS. A SSUME YOU DID THE FOLLOWING SEQUENCE OF OPERATIONS ( RENAMED TO MATCH THOSE IN THE INTERFACE, SO WOULD BE ONE OF THE CONCRETE Q UEUE, S TACK, ETC CLASSES, " INSERT " WOULD BE ONE OF ENQUEUE, PUSH, ETC ): NEW. INSERT (5). INSERT (7). INSERT (4). FIRST () I F THIS SEQUENCE RETURNED 4, WHICH DATA STRUCTURE WAS BEING USED ? We don't know -- it could be any of them No, because this wouldn’t be a queue. Queues are first in, first out, and since 4 was added last, there’s no way for it to be the first. Must be a stack Must be a queue Again, can’t be a queue. Must be a priority queue Either a stack or a priority queue Either a queue or a priority queue Again, can’t be a queue. Either a stack or a queue Again, can’t be a queue.

E ACH OF THE THREE DATA STRUCTURES ON THIS ASSIGNMENT HAS OPERATIONS THAT ADD ELEMENTS, INSPECT THE " FIRST " ELEMENT, AND CHECK FOR ELEMENTS. A SSUME YOU DID THE FOLLOWING SEQUENCE OF OPERATIONS ( RENAMED TO MATCH THOSE IN THE INTERFACE, SO WOULD BE ONE OF THE CONCRETE Q UEUE, S TACK, ETC CLASSES, " INSERT " WOULD BE ONE OF ENQUEUE, PUSH, ETC ): NEW. INSERT (5). INSERT (7). INSERT (4). FIRST () I F THIS SEQUENCE RETURNED 4, WHICH DATA STRUCTURE WAS BEING USED ? We don't know -- it could be any of them Must be a stack Must be a queue Must be a priority queue Either a stack or a priority queue This is the correct answer. It could be a priority queue since 4 is the smallest number in the set (4 < 5 and 4 < 7), and it could be a stack since 4 was added last. Either a queue or a priority queue Either a stack or a queue

(S AME SETUP AS PREVIOUS QUESTION, BUT DIFFERENT SEQUENCE OF OPERATIONS ) E ACH OF THE THREE DATA STRUCTURES ON THIS ASSIGNMENT HAS OPERATIONS THAT ADD ELEMENTS, INSPECT THE " FIRST " ELEMENT, AND CHECK FOR ELEMENTS. A SSUME YOU DID THE FOLLOWING SEQUENCE OF OPERATIONS ( RENAMED TO MATCH THOSE IN THE INTERFACE, SO WOULD BE ONE OF THE CONCRETE Q UEUE, S TACK, ETC CLASSES, " INSERT " WOULD BE ONE OF ENQUEUE, PUSH, ETC ): NEW. INSERT (5). INSERT (2). REM F IRST (). INSERT (8). FIRST () I F THIS SEQUENCE RETURNED 8, WHICH DATA STRUCTURE WAS BEING USED ? We don't know -- it could be any of them Must be a stack Must be a queue Must be a priority queue Either a stack or a priority queue Either a queue or a priority queue Either a stack or a queue

(S AME SETUP AS PREVIOUS QUESTION, BUT DIFFERENT SEQUENCE OF OPERATIONS ) E ACH OF THE THREE DATA STRUCTURES ON THIS ASSIGNMENT HAS OPERATIONS THAT ADD ELEMENTS, INSPECT THE " FIRST " ELEMENT, AND CHECK FOR ELEMENTS. A SSUME YOU DID THE FOLLOWING SEQUENCE OF OPERATIONS ( RENAMED TO MATCH THOSE IN THE INTERFACE, SO WOULD BE ONE OF THE CONCRETE Q UEUE, S TACK, ETC CLASSES, " INSERT " WOULD BE ONE OF ENQUEUE, PUSH, ETC ): NEW. INSERT (5). INSERT (2). REM F IRST (). INSERT (8). FIRST () I F THIS SEQUENCE RETURNED 8, WHICH DATA STRUCTURE WAS BEING USED ? We don't know -- it could be any of them Cannot be a queue. See below. Must be a stack. Must be a queue This is incorrect because if it were a queue, 2 would be returned, not 8. We added 5, then 2, removed first (5, if this were a queue), then added 8. If it were a queue, 2 is next in line because it was added before 8. Must be a priority queue Either a stack or a priority queue Already explained above – can’t be a priority queue. Either a queue or a priority queue Already explained above – can’t be a queue. Either a stack or a queue – Already explained above – can’t be a queue.

(S AME SETUP AS PREVIOUS QUESTION, BUT DIFFERENT SEQUENCE OF OPERATIONS ) E ACH OF THE THREE DATA STRUCTURES ON THIS ASSIGNMENT HAS OPERATIONS THAT ADD ELEMENTS, INSPECT THE " FIRST " ELEMENT, AND CHECK FOR ELEMENTS. A SSUME YOU DID THE FOLLOWING SEQUENCE OF OPERATIONS ( RENAMED TO MATCH THOSE IN THE INTERFACE, SO WOULD BE ONE OF THE CONCRETE Q UEUE, S TACK, ETC CLASSES, " INSERT " WOULD BE ONE OF ENQUEUE, PUSH, ETC ): NEW. INSERT (5). INSERT (2). REM F IRST (). INSERT (8). FIRST () I F THIS SEQUENCE RETURNED 8, WHICH DATA STRUCTURE WAS BEING USED ? We don't know -- it could be any of them Cannot be a priority queue or a queue. See below. Must be a stack. This is the correct answer. If you add 5, then add 2, remove first, and add 8, the only way to get 8 back again is if it’s a stack (because stacks are last-in, first out) Must be a queue Must be a priority queue This is incorrect because if it were a priority queue, 2 would be returned, not 8. We added 5, then 2, removed first (5, if this were a queue), then added 8. If it were a priority queue, 2 is next in line because 2 < 8. Either a stack or a priority queue Already explained above – can’t be a priority queue. Either a queue or a priority queue Already explained above – can’t be a queue or a priority queue. Either a stack or a queue

HW4: L INKED L IST I NSIDE OF A C LASS. H OW TO A DD TO I T AND A CCESS I T Ex: You have a class called “Dillo,” which has: A string for its name An int for its length A LinkedList of Dillos, which are its friends Let’s say you want to go through the list of a dillo’s friends and print to the console the name of each friend, if its length is greater than 5

HW4: L INKED L IST I NSIDE OF A C LASS class Dillo { String name; int length; LinkedList friends; public Dillo (String name, int length, LinkedList friends){ this.name = name; this.length = length; this.friends = friends; }

A DDING TO A L INKED L IST I NSIDE OF A C LASS Examples { Examples() {} Dillo christinaDillo = new Dillo(“Christina”, 5, new LinkedList ()); // List of friends starts empty (  ) Dillo nicoleDillo = new Dillo(“Nicole”, 5, new LinkedList ()); // Empty list of friends Dillo karaDillo = new Dillo(“Kara”, 6, new LinkedList ()); // Again, empty friend list // Let’s add these two new Dillos to Christina’s list of friends: christinaDillo.friends.add(nicoleDillo); // Format is “name of object”.“name of list”.add(“item to add”) christinaDillo.friends.add(karaDillo); // christinaDillo is the object name. friends is the list name. karaDillo is who we’re adding. }

A CCESSING A L INKED L IST I NSIDE OF A C LASS public void printFriendsLongerThanFive() { for(Dillo aDilloFriend : this.friends) { if(aDilloFriend.length > 5){ System.out.println(aDilloFriend.name); } Let’s break down each piece of this method. What is this method doing?

public void printFriendsLongerThanFive() { for(Dillo aDilloFriend : this.friends) { if(aDilloFriend.length > 5){ System.out.println(aDilloFriend.name); } “void” means return nothing. We have no return statement and aren’t returning anything. We’re just doing an action (printing to the console). A CCESSING A L INKED L IST I NSIDE OF A C LASS

public void printFriendsLongerThanFive() { for(Dillo aDilloFriend : this.friends) { if(aDilloFriend.length > 5){ System.out.println(aDilloFriend.name); } This says, “For each Dillo in the list that is this.friends, refer to that object (that friend) as ‘aDilloFriend.’” Let’s break that down a bit more… A CCESSING A L INKED L IST I NSIDE OF A C LASS

public void printFriendsLongerThanFive() { for(Dillo aDilloFriend : this.friends) { if(aDilloFriend.length > 5){ System.out.println(aDilloFriend.name); } This says, “ For each item in the list that is this.friends, refer to that object (that friend) as “aDilloFriend.”

A CCESSING A L INKED L IST I NSIDE OF A C LASS public void printFriendsLongerThanFive() { for(Dillo aDilloFriend : this.friends) { if(aDilloFriend.length > 5){ System.out.println(aDilloFriend.name); } This says, “For each Dillo in the list that is this.friends, refer to that object (that friend) as “aDilloFriend.”

A CCESSING A L INKED L IST I NSIDE OF A C LASS public void printFriendsLongerThanFive() { for(Dillo aDilloFriend : this.friends) { if(aDilloFriend.length > 5){ System.out.println(aDilloFriend.name); } This says, “For each Dillo in the list that is this.friends, each is a Dillo and refer to that object (that friend) as “aDilloFriend.”

A CCESSING A L INKED L IST I NSIDE OF A C LASS public void printFriendsLongerThanFive() { for(Dillo aDilloFriend : this.friends) { if(aDilloFriend.length > 5){ System.out.println(aDilloFriend.name); } Refer to that object (that friend) as “aDilloFriend.” This means that every time you write “aDilloFriend” inside of the for loop, what is inside of that object is the current item in the list that you’re looking at.

I TERATING T HROUGH A L INKED L IST I NSIDE OF A C LASS Examples { Examples() {} Dillo christinaDillo = new Dillo(“Christina”, 5, new LinkedList ()); // List of friends starts empty (  ) Dillo nicoleDillo = new Dillo(“Nicole”, 5, new LinkedList ()); // Empty list of friends Dillo karaDillo = new Dillo(“Kara”, 6, new LinkedList ()); // Again, empty friend list // Let’s add these two new Dillos to Christina’s list of friends: christinaDillo.friends.add(nicoleDillo); christinaDillo.friends.add(karaDillo); // We can call that method we wrote: christinaDillo.printFriendsLongerThanFive(); }

I TERATING T HROUGH A L INKED L IST I NSIDE OF A C LASS public void printFriendsLongerThanFive() { for(Dillo aDilloFriend : this.friends) { if(aDilloFriend.length > 5){ System.out.println(aDilloFriend.name); } If I call this method in this way: christinaDillo.printFriendsLongerThanFive(); then “ this.friends ” will specifically be christinaDillo’s list of friends. Notice how we don’t need to pass in her list of friends because the object already has access to it with the “this” keyword. Passing in the list of friends would be redundant because it already has access to this list.

I TERATING T HROUGH A L INKED L IST I NSIDE OF A C LASS public void printFriendsLongerThanFive() { for(Dillo aDilloFriend : this.friends) { if(aDilloFriend.length > 5){ System.out.println(aDilloFriend.name); } christinaDillo’s list of friends contains: nicoleDillo (“Nicole”, 5, empty list of friends ); karaDillo (“Kara”, 6, empty list of friends );

I TERATING T HROUGH A L INKED L IST I NSIDE OF A C LASS public void printFriendsLongerThanFive() { for(Dillo aDilloFriend : this.friends) { if(aDilloFriend.length > 5){ System.out.println(aDilloFriend.name); } At the line in blue, aDilloFriend would first be populated with nicoleDillo. Remember that nicoleDillo’s length was 5, and since 5 is not greater than 5, her name will not be printed. We don’t go inside of this if statement this time.

I TERATING T HROUGH A L INKED L IST I NSIDE OF A C LASS public void printFriendsLongerThanFive() { for(Dillo aDilloFriend : this.friends) { if(aDilloFriend.length > 5){ System.out.println(aDilloFriend.name); } Now at the line in blue, aDilloFriend would be populated with karaDillo.

I TERATING T HROUGH A L INKED L IST I NSIDE OF A C LASS public void printFriendsLongerThanFive() { for(Dillo aDilloFriend : this.friends) { if(aDilloFriend.length > 5){ System.out.println(aDilloFriend.name); } Remember that karaDillo’s length was 6, and since 6 is greater than 5, her name WILL be printed.

S TREAMS WITH S TOPPING C ONDITION List myStreamOfNumbers = new LinkedList<>(); numbers.stream().anyMatch(x -> { test1.add(x); return x == -999 ; }); The text in blue is your stopping condition.

I NTEGERS WITH S CANNER If you attempt to get an integer using a Scanner and do it in this form: Scanner keyboard = new Scanner(System.in); int x; keyboard.nextInt(x); It will throw a “PatternSyntaxException” exception. (If you pass a parameter to nextInt, it's interpreted as the radix of the entered integer. See here for more info: patternsyntaxexception) patternsyntaxexception Instead use: x = keyboard.nextInt();