0 of 36 Andries van Dam  2015 11/05/15 Reminder: TA Hour Policies If you hit a bug, you must be able to show significant effort of fixing it on your own.

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

Chapter 24 Lists, Stacks, and Queues
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
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.
0 of 37 Stacks and Queues Lecture of 37 Abstract Data Types To use a method, need to know its essentials: signature and return type o additionally,
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.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
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.”
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Queue Overview Queue ADT Basic operations of queue
CS Data Structures II Review COSC 2006 April 14, 2017
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Stacks and Queues COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
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.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
Stacks, Queues, and Deques
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
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 373 Data Structures and Algorithms Lecture 2: Queues.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Stacks and Queues Introduction to Computing Science and Programming I.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
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,
September 05 Kraemer UGA/CSCI 2720 Lists – Part I CSCI 2720 Eileen Kraemer The University of Georgia.
1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException.
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’
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
Data structures Abstract data types Java classes for Data structures and ADTs.
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
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.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
QUEUES What are Queues? Creating a Queue Enqueuing and Dequeuing Testing for an Empty Queue.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Click to edit Master text styles Stacks Data Structure.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Review Array Array Elements Accessing array elements
Queues 5/11/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
CC 215 Data Structures Queue ADT
Week 3 - Friday CS221.
Stacks and Queues.
Queues Queues Queues.
HW-6 Deadline Extended to April 27th
CMSC 341 Lecture 5 Stacks, Queues
Pointers and Linked Lists
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Lecture 2: Stacks and Queues
Stacks, Queues, and Deques
CSCS-200 Data Structure and Algorithms
Presentation transcript:

0 of 36 Andries van Dam  /05/15 Reminder: TA Hour Policies If you hit a bug, you must be able to show significant effort of fixing it on your own before seeing a TA -if not, TAs will turn you away TAs will only help you with the question entered in SignMeUp -the question should be specific (e.g., doodlejump platforms is bad) To be fair to other students in line, each TA hours visit is capped at 15 minutes If you do not show up within 5 minutes of your name being called, you will lose your spot in line -You can remove yourself from the line by logging into SignMeUp and clicking “remove” -Doing so is in your best interest because if you don’t, you will get turned away and have to wait an hour to sign up again (as per the SignMeUp policy)

1 of 36 Andries van Dam  /05/15 Reminder: CS15 Collaboration Policy We have caught more collaboration policy violations -MOSS is excellent at detecting it If you are ever in doubt about what is or isn’t allowed, ask a TA Remember: labs are collaborative (while you are in your lab section), no other form of assignment is

2 of 36 Andries van Dam  /05/15 Stacks and Queues Lecture 17

3 of 36 Andries van Dam  /05/15 Abstract Data Types (1/2) ●To use a method, need to know its essentials: signature and return type o additionally, documentation tells us purpose, error conditions, what resources (such as classes and packages) the method needs, etc. o set of signatures and return types for an entire class designed to store and manage data is called an Abstract Data Type 1 (ADT) – in Java, ADT’s are supported by the interface feature of Java o we don’t know anything about its implementation – encapsulation 1. This is an informal definition. ADT also has a mathematical definition.

4 of 36 Andries van Dam  /05/15 Abstract Data Types (2/2) ●In these linked list lectures, show both how to implement various list ADTs using linked nodes, and then use those ADTs with simple programs to demonstrate their use. o ArrayList s and NodeList s are ADTs that adhere to Java’s list interface ●Note: full description of an ADT is sometimes called an API (Application Program Interface) o term “Application Program Interface” coined by former undergraduate Ira Cotton in 1968

5 of 36 Andries van Dam  /05/15 Stacks ● Stack has special requirements for insertion and deletion called o push and pop ●Instead of being able to insert and delete nodes from anywhere in the list, can only add and delete nodes from top of Stack o LIFO (Last In, First Out) ●W e’ll implement a stack with a linked list and then use it in a simple demo app

6 of 36 Andries van Dam  /05/15 Stack Constructor ●When generic Stack is instantiated, it contains an empty MyLinkedList ●When using a stack, you will fill in with whatever type of object your Stack will hold – enforces homogeneity public class Stack { private MyLinkedList _list; public Stack() { _list = new MyLinkedList (); } /* other methods elided */ }

7 of 36 Andries van Dam  /05/15 Methods of a Stack ●Add element to top of stack ●Remove element from top of stack ●Returns whether stack has elements ●Returns number of elements in stack public void push(Type el) { //elided } public Type pop() { //elided } public boolean isEmpty() { //elided } public int size() { //elided }

8 of 36 Andries van Dam  /05/15 Pushing an Object ●When an element is pushed, it is always added to front of list ●Let’s see what this does... //in the Stack class... public Node push(Type newData) { return _list.addFirst(newData); } 123

9 of 36 Andries van Dam  /05/15 Popping an Object //in the Stack class... public Type pop() { return _list.removeFirst(); } ●When popping an element, element is always removed from top of Stack, so call removeFirst on MyLinkedList ● removeFirst returns element removed, and Stack in turn returns it ●Remember that removeFirst method of MyLinkedList first checks to see if list is empty ●Let’s see what this does

10 of 36 Andries van Dam  /05/15 isEmpty ● Stack will be empty if _list is empty ●Returns a boolean that is true if Stack is empty and false otherwise //in the Stack class... public boolean isEmpty() { return _list.isEmpty(); }

11 of 36 Andries van Dam  /05/15 size ●Size of Stack will be the size of the Linked List that it contains ●Size is updated whenever a Node is added to or deleted from _list during push and pop methods //in the Stack class... public int size() { return _list.size(); }

12 of 36 Andries van Dam  /05/ push(1)push(2)push(3)pop() push(4)pop()

13 of 36 Andries van Dam  /05/15 First Example: Execution Stacks ●Each method has an Activation Record (AR) o contains an execution pointer to instruction to be executed next in method o also contains all local variables and parameters of method ●When methods execute and call other methods, Java uses a Stack to track these calls o when a method calls another method, Java adds activation record of called method to Stack o when new method is finished, its AR is removed from Stack, and previous method is continued o method could be different or a recursively called clone

14 of 36 Andries van Dam  /05/15 Execution Stacks AR of Method E AR of Method D AR of Method C AR of Method B AR of Method A Top of Stack A E B DC A calls B B calls C … etc.

15 of 36 Andries van Dam  /05/15 Stack Trace ●When an exception is thrown in a program, get a long list of methods and line numbers known as a stack trace o Exception in thread “main” at. (.java: )... ●A stack trace prints out all methods currently on execution stack ●If exception is thrown during execution of recursive method, prints all calls to recursive method

16 of 36 Andries van Dam  /05/15 Bootstrapping ADT’s ●In effect, this stack ADT is implemented as a thin wrapper over a Linked List ADT, but user has no knowledge of that ●Could also implement it with an array or ArrayList, but that would be more inefficient due to constant data movement ●We’ll use the same technique to implement a Queue

17 of 36 Andries van Dam  /05/15 What are Queues? ●Similar to stacks, but elements are removed in different order o information retrieved in the same order it was stored o FIFO: First In, First Out (as opposed to stacks, which are LIFO: Last In, First Out) ●Examples: o standing on line at the checkout counter or movie theater o waitlist for TA hours Server at Seattle restaurant reminding herself what order customers get served in

18 of 36 Andries van Dam  /05/15 Enqueuing and Dequeuing Before Enqueuing head of queue tail of queue 4 student to add After Enqueuing head of queue tail of queue ●Enqueuing: adds a node ●Dequeuing: removes a node

19 of 36 Andries van Dam  /05/15 Enqueuing and Dequeuing 1 student removed from queue Before Dequeuing head of queue tail of queue After Dequeuing head of queue tail of queue ●Enqueuing: adds a node ●Dequeuing: removes a node

20 of 36 Andries van Dam  /05/15 Enqueuing and Dequeuing Before Dequeuing head of queue tail of queue After Dequeuing head of queue tail of queue ●Enqueuing: adds a node ●Dequeuing: removes a node

21 of 36 Andries van Dam  /05/15 Our Queue ●Let’s use another Linked List to help us make our Queue ●Contain a MyLinkedList within Queue class o enqueue will add to the end of MyLinkedList o dequeue will remove the first element in MyLinkedList public class Queue { private MyLinkedList _list; public Queue() { _list = new MyLinkedList (); } // Other methods elided }

22 of 36 Andries van Dam  /05/15 Our Queue ●Need following methods: o enqueue o dequeue public class Queue { private MyLinkedList _list; public Queue() { _list = new MyLinkedList (); } public void enqueue(Type newNode) { // code to follow... } public void dequeue() { // code to follow... } }

23 of 36 Andries van Dam  /05/15 enqueue ●Just call _list ’s addLast method! ●This will add node to end of _list public void enqueue(Type newNode) { _list.addLast(newNode); }

24 of 36 Andries van Dam  /05/15 dequeue ●We want first node in _list ●Use _list ’s removeFirst method! ●What if _list is empty? There will be nothing to dequeue! ●Our MyLinkedList class’s removeFirst() method returns null in this case, so dequeue does as well public Type dequeue() { return _list.removeFirst(); }

25 of 36 Andries van Dam  /05/15 isEmpty() and size() ●As with Stack s, very simple methods; just delegate to MyLinkedList public int size() { return _list.size(); } public boolean isEmpty() { return _list.isEmpty(); }

26 of 36 Andries van Dam  /05/15 Exercise 1 (1/5) ●How can we use a Stack to reverse a Linked List? ●Linked List: Troy, Gabriella, Ryan, Sharpay tail head

27 of 36 Andries van Dam  /05/15 Exercise 1 (2/5) ●Solution: o while Linked List is not empty, remove from Linked List and push elements onto Stack o then, while Stack is not empty, pop elements from Stack and add to Linked List

28 of 36 Andries van Dam  /05/15 Exercise 1 (3/5) while(!_list.isEmpty()) { stack.push(_list.removeFirst()); } Stack SharpayRyanGabriella Troy tail head Null

29 of 36 Andries van Dam  /05/15 Exercise 1 (4/5) Stack Troy Gabriella Ryan Sharpay tailhead while(!stack.isEmpty()){ _list.addLast(stack.pop()); } tail Null head tail

30 of 36 Andries van Dam  /05/15 Exercise 2 (1/2) ●Check for balanced parentheses in a given string ●Balanced: [()()]{[()]} ●Not balanced: [(])

31 of 36 Andries van Dam  /05/15 Exercise 2 (2/2) ●Go through every character, if it is a starting bracket, push it onto the stack ●If it is a closing bracket, pop from the stack ●The bracket you pop should be the opening bracket that corresponds to the closing bracket you are looking at o if it is not, return false ●If you get through every character and you haven’t returned false, check if stack is empty ●If it is, the brackets are balanced!

32 of 36 Andries van Dam  /05/15 Problem 2 Pseudocode for each bracket in string : if stack is empty: return true if it is a starting bracket: push it onto stack if the popped character is not the matching opening bracket: return false if it is a closing bracket: pop from the stack

33 of 36 Andries van Dam  /05/15 for each character: if it is a starting bracket: push it onto stack if it is a closing bracket: pop from the stack if the popped character is not the matching opening bracket: return false if stack is empty return true [ Stack () ] )][(

34 of 36 Andries van Dam  /05/15 for each bracket in string: if it is a starting bracket: push it onto stack if it is a closing bracket: pop from the stack if the popped character is not the matching opening bracket: return false if stack is empty return true [ Stack ()])][(

35 of 36 Andries van Dam  /05/15 for each character: if it is a starting bracket: push it onto stack if it is a closing bracket: pop from the stack if the popped character is not the matching opening bracket: return false if stack is empty return true [ Stack ( )] Match! Keep going… [(

36 of 36 Andries van Dam  /05/15 for each character: if it is a starting bracket: push it onto stack if it is a closing bracket: pop from the stack if the popped character is not the matching opening bracket: return false if stack is empty return true [ Stack )] Match! Keep going… [(

37 of 36 Andries van Dam  /05/15 for each character: if it is a starting bracket: push it onto stack if it is a closing bracket: pop from the stack if the popped character is not the matching opening bracket: return false if stack is empty return true [ Stack )][(

38 of 36 Andries van Dam  /05/15 for each character: if it is a starting bracket: push it onto stack if it is a closing bracket: pop from the stack if the popped character is not the matching opening bracket: return false if stack is empty return true Stack )][(

39 of 36 Andries van Dam  /05/15 Announcements DoodleJump is due tomorrow at 10:00pm -Late handin Sunday at 10:00pm Tetris goes out today -on-time handin 11/22 Homework 3 goes out today -on-time handin 11/15 (no early or late handin) Start EARLY -TA hours get very long right before the deadline