Winter 2006CISC121 - Prof. McLeod1 Stuff Solution to midterm is posted. Marking has just started… Lab for this week is not posted (yet?). Final exam (full.

Slides:



Advertisements
Similar presentations
Stack & Queues COP 3502.
Advertisements

Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can be instantiated.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
CS252: Systems Programming Ninghui Li Program Interview Questions.
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.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
Stacks  a data structure which stores data in a Last-in First-out manner (LIFO)  has a pointer called TOP  can be implemented by either Array or Linked.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
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.
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
CS 206 Introduction to Computer Science II 10 / 15 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
TCSS 342, Winter 2005 Lecture Notes
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
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. 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.
Stacks, Queues, and Deques
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Stacks and queues Basic operations Implementation of stacks and queues Stack and Queue in java.util Data Structures and Algorithms in Java, Third EditionCh04.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Stacks and Queues Introduction to Computing Science and Programming I.
Winter 2006CISC121 - Prof. McLeod1 Stuff Deadline for assn 3 extended to Monday, the 13 th. Please note that the testing class for assn 3 has changed (last.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
Chapter 15: Advanced Topics: Introducing Data Structures and Recursion Visual Basic.NET Programming: From Problem Analysis to Program Design.
Linked Structures See Section 3.2 of the text.. First, notice that Java allows classes to be recursive, in the sense that a class can have an element.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
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.
Summer 2007CISC121 - Prof. McLeod1 CISC121 – Lecture 7 Last time: –(A midterm!) –Invariants –Started Linked Lists.
LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack.
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.
Foundation of Computing Systems Lecture 3 Stacks and Queues.
Stacks And Queues Chapter 18.
Arrays and Collections Tonga Institute of Higher Education.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
Winter 2006CISC121 - Prof. McLeod1 Stuff Deadline for assn 3 extended to Monday, the 13 th. Please note that the testing class for assn 3 has changed.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Lab 6 Stack ADT. OVERVIEW The stack is one example of a constrained linear data structure. In a stack, the elements are ordered from most recently added.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
Winter 2006CISC121 - Prof. McLeod1 Stuff No stuff today!
Lecture 8: Advanced OOP Part 2. Overview Review of Subtypes Interfaces Packages Sorting.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
CS 46B: Introduction to Data Structures July 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
Lecture 10 b Stacks b Queues. 2 Stacks b A stack ADT is linear b Items are added and removed from only one end of a stack b It is therefore LIFO: Last-In,
Winter 2016CISC101 - Prof. McLeod1 Today Numeric representation (or “How does binary and hexadecimal work?”). How can a CPU understand instructions written.
Click to edit Master text styles Stacks Data Structure.
 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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
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.
Week 4 - Friday CS221.
Week 15 – Monday CS221.
Stack and Queue APURBO DATTA.
HW-6 Deadline Extended to April 27th
CMSC 341 Lecture 5 Stacks, Queues
Stacks, Queues, and Deques
Stacks, Queues, and Deques
Lesson Objectives Aims
Winter 2018 CISC101 11/29/2018 CISC101 Reminders
CISC101 Reminders Labs start this week. Meet your TA! Get help with:
Stacks, Queues, and Deques
CSCS-200 Data Structure and Algorithms
Chapter 4 Stacks and Queues
Presentation transcript:

Winter 2006CISC121 - Prof. McLeod1 Stuff Solution to midterm is posted. Marking has just started… Lab for this week is not posted (yet?). Final exam (full of good news today!) is scheduled on April 27 (Thursday) at 2pm in Jock Hardy (Clergy). 3 hours long.

Winter 2006CISC121 - Prof. McLeod2 Last Time Replacing sparse tables with linked lists. Started Stacks and Queues

Winter 2006CISC121 - Prof. McLeod3 Today Finish looking at stacks Queues

Winter 2006CISC121 - Prof. McLeod4 Stacks – Cont. Another example are those plate dispensers in cafeterias. A stack follows the “Last In, First Out” or “LIFO” principle. A stack would have the following operations: –clear() – clear the stack. –isEmpty() – check to see if the stack is empty. –isFull() – check to see if the stack is full. –push(element) - put the element on top of the stack. –pop() – take the topmost element from the stack. –peek() – return the topmost element in the stack without removing it.

Winter 2006CISC121 - Prof. McLeod5 Stacks – Cont. See IntStack.java for a linked list implementation. See IntStackSmaller.java for an even smaller linked list implementation! Some features of IntStackSmaller: –An inner class for the node. –Only public methods are: clear () boolean isEmpty () push (int) int pop () int peek ()

Winter 2006CISC121 - Prof. McLeod6 Stacks – Cont. A stack can also be implemented with an ArrayList or even an array (But not as well, IMHO). Note that none of the stack operations require iteration through the linked list.

Winter 2006CISC121 - Prof. McLeod7 ArrayList Stack Implementation See ALStack.java (Note that we are assuming the user will check to see if the stack is empty before calling a “pop()” operation. What else could we do?) “Features”: –We need to keep track of position in the ArrayList. –We could use (but did not) the automatic “un-boxing” and boxing feature in Java 5.0 (huh?).

Winter 2006CISC121 - Prof. McLeod8 Array Stack Implementation See ArrayStack.java A bit clumsy? Of the three implementations, which is the best?

Winter 2006CISC121 - Prof. McLeod9 A Stack in Use How to add numbers that are too large to be stored in a long type variable? See LongAddition.java

Winter 2006CISC121 - Prof. McLeod10 The Stack Class in java.util java.util has a “ Stack ” class that implements the above methods using a Vector as the storage object. (A Vector is like an ArrayList…) Stack is a sub-class of Vector, and as such, inherits all the Vector methods.

Winter 2006CISC121 - Prof. McLeod11 The Stack Class – Cont. Unique Stack methods: boolean empty() // same as isEmpty Object peek() Object pop() Object push(element) int search(target) // returns position of target in stack, if not found –1 is returned. Stack() // constructor

Winter 2006CISC121 - Prof. McLeod12 The Stack Class – Cont. In Stack, “ push ” also returns a reference to the Object added to the stack, so both peek and push can change that topmost object on the stack. Since Stack “is a” Vector, methods like “ setElementAt ” and “ removeElementAt ” can be used on stack elements – but these methods would be illegal by our definition of what a stack is! Also, when Vector ’s are used to implement the stack, re-sizing of the Vector can greatly slow down the push method.

Winter 2006CISC121 - Prof. McLeod13 The Stack Class – Cont. For these reasons it is better to implement a stack as we have done above, using a private linked list (best) or a private array(next best) as a data object within the definition of the class. Implementing a stack using a linked list defined using an inner class for the node provides better information hiding than the Stack class.

Winter 2006CISC121 - Prof. McLeod14 Queues A queue is just a lineup, like at your favorite movie theatre. It would use the “FIFO” principle – “First In, First Out”. It would have the following operations: –clear() – clear the queue. –isEmpty() – check to see if the queue is empty. –isFull() – check to see if the queue is full. –enqueue(element) - put the element at the end of the queue. –dequeue() – take the first element from the queue. –firstEl() – return the first element in the queue without removing it.

Winter 2006CISC121 - Prof. McLeod15 Aside: “isFull()” Our implementations of stacks and queues do not have an “ isFull() ” method, because they do not need one. If you did need such a method to model a stack or queue that is limited in size, how would you do it? Suppose the maximum size is provided when the stack or queue is created (in the constructor).

Winter 2006CISC121 - Prof. McLeod16 Queues - Cont. Does a linked list implementation of a queue require both head and tail links? If a singly linked list is used would you enqueue to the head or the tail? Why? YUP! - enqueue to tail! - easier to remove head node than to remove tail node in singly linked list for dequeue operation

Winter 2006CISC121 - Prof. McLeod17 Queues – Cont. A queue can easily be implemented using a singly linked list with a head and tail. (See IntQueue.java, for example) A queue, in code, is often used as part of a model of a real-world process. In the “real-world” queues are everywhere – Airport runways, McDonalds, banks, assembly lines, etc.

Winter 2006CISC121 - Prof. McLeod18 Queues – Cont. How would you implement a queue with an ArrayList or an array? Not so easy, right? A “circular array” can be used, with variables that point to the first and last occupied positions in the array. If “last” moves to the end of the array, it can be wrapped back to the beginning. Linked lists really are the best way to implement a queue.

Winter 2006CISC121 - Prof. McLeod19 Queues – Priority Queue A Priority Queue is just a queue where the elements are also given a priority. In this case, an element with a higher priority can be removed before the element that is “first” in the the queue, when the first element is of a lower priority. (Like how an ambulance gets through traffic…)

Winter 2006CISC121 - Prof. McLeod20 Back to Stacks - Activation Frames Stacks are an integral part of computer architecture. For example, Java byte code is run by the “Java Virtual Machine”, or “JVM”. The JVM is stack – based. Each thread (or process…) in the JVM has its own private run-time stack in memory (our programs are “single threaded”). Each run-time stack contains “activation frames” – one for each method that has been activated. Only one activation frame (or method) can be active at a time for each thread. An activation frame is created, or “pushed”, every time a method is invoked. When the method is completed, the frame is popped off the stack.

Winter 2006CISC121 - Prof. McLeod21 Activation Frames An activation frame (or “stack frame” or “activation record”) contains (among other things): –All local variables. –A link to the frame of the calling method, so that control can be returned to line of code in the caller immediately after the method call. –Information for catching exceptions. –An “operand stack”, which is another stack that is used by the JVM as a source of arguments and a repository of results. An understanding of how this particular stack works will help to explain how recursive methods work…

Winter 2006CISC121 - Prof. McLeod22 Demonstration Run “RunTimeStackDemo.java” in debug mode, stepping though method calls and observe thread stack display.

Winter 2006CISC121 - Prof. McLeod23 Summary - Stacks & Queues We will use the thread stack to help explain how recursion works - later... Stacks & Queues are often used to model real- world processes. Providing an easy-to-use stack or queue object makes the modeling effort easier. Now we have to move on to something completely different!