Week 3 – Wednesday.  What did we talk about last time?  ADTs  List implementation with a dynamic array.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Stack & Queues COP 3502.
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
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.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
Stacks. 2 Outline and Reading The Stack ADT (§4.2.1) Applications of Stacks (§4.2.3) Array-based implementation (§4.2.2) Growable array-based stack.
Stacks A stack is a data structure that only allows items to be inserted and removed at one end We call this end the top of the stack The other end is.
Data Structures & Algorithms
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Applications of Stacks (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5)
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
Tirgul 3 Topics of this Tirgul: Lists Vectors Stack Queue.
TCSS 342, Winter 2005 Lecture Notes
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
Week 3 - Wednesday.  What did we talk about last time?  Started linked lists.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Stacks and Queues Introduction to Computing Science and Programming I.
Problem of the Day  What do you get when you cross a mountain climber and a grape?
Information and Computer Sciences University of Hawaii, Manoa
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.
Week 6 - Friday.  What did we talk about last time?  Recursive running time  Fast exponentiation  Merge sort  Introduced the Master theorem.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Week 12 - Wednesday.  What did we talk about last time?  Asymptotic notation.
Week 3 - Friday.  What did we talk about last time?  Stacks  Array implementation of a 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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
Week 8 - Monday.  What did we talk about last time?  BST traversals  Get range implementation.
Week 13 - Friday.  What did we talk about last time?  Sorting  Insertion sort  Merge sort  Started quicksort.
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.
Week 5 - Wednesday.  What did we talk about last time?  Recursion  Definitions: base case, recursive case  Recursive methods in Java.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
April 27, 2017 COSC Data Structures I Review & Final Exam
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Symbol tables.
Week 4 - Friday.  What did we talk about last time?  Continued doubly linked list implementation  Linked lists with iterators.
Week 4 - Wednesday.  What did we talk about last time?  Started linked lists.
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in.
An Array-Based Implementation of the ADT List
Chapter 4 Stacks
Week 4 - Monday CS221.
Week 4 - Friday CS221.
September 29 – Stacks and queues
MEMORY REPRESENTATION OF STACKS
Week 3 - Friday CS221.
Stacks.
Cinda Heeren / Geoffrey Tien
Week 15 – Monday CS221.
COMPUTER 2430 Object Oriented Programming and Data Structures I
CMSC 341 Lecture 5 Stacks, Queues
Stacks.
CSE 214 – Computer Science II Stacks
COMPUTER 2430 Object Oriented Programming and Data Structures I
More Data Structures (Part 1)
Stacks CS-240 Dick Steflik.
Presentation transcript:

Week 3 – Wednesday

 What did we talk about last time?  ADTs  List implementation with a dynamic array

Bitmap Manipulator

 How long did each method we implemented last week take, in Θ notation, where n is the number of items already in the lists?  Constructor  Θ(1)  Add: Insert element at the end of the list Θ(n)Θ(n)  Add at index: Insert element at an arbitrary location Θ(n)Θ(n)  Get: Retrieve element from arbitrary location  Θ(1)  Size: Get the current number of elements stored  Θ(1)

 Amortized analysis is one way to consider average running time  The amortized cost per operation of n operations is the total cost divided by n  If an operation is expensive only once in a while, the amortized running time might be much less than the worst case running time  A random search could make you take a long time to get through airport security  Since you don't usually get stopped, your average time isn't much different than when you don't get stopped

 In the Add operation, it usually only takes Θ(1) time to put an element at the end of the array  The only time it takes Θ(n) time is when you have to resize the array  To simplify the analysis, let's assume:  It takes 1 operation to add, ignoring the resize  It takes n operations (where n is the number of things already in the array) to resize  We are only adding, no other operations  The amortized running time depends on your strategy for resizing

 The most space efficient approach is to keep the array completely full  Thus, you have to extend the array each time you add an item  To add n items, you'll have the following resize costs: … + (n – 1) = n(n - 1)/2  Plus, it would have cost an additional n to do the adds themselves  Total cost: n(n - 1)/2 + n  Amortized cost per operation: (n - 1)/2 + 1, which is Θ(n)  Yuck.

Impromptu Student Lecture

 A stack is a simple (but useful) ADT that has three basic operations:  Push Put an item on the top of the stack  Pop Remove an item from the top of the stack  TopReturn the item currently on the top of the stack (sometimes called peek)

 When are stacks used?  Implicitly, in recursion (or in any function calls)  Explicitly, when turning recursive solutions into iterative solutions  When parsing programming languages  When converting infix to postfix

 Advantages:  Pop is Θ(1)  Top is Θ(1)  Disadvantages  Push is Θ(n) in the very worst case, but not in the amortized case

public class ArrayStack { private int[] data; private int size; public ArrayStack() {} public void push(int value) {} public int pop() {} public int peek() {} //instead of top public int size() {} } public class ArrayStack { private int[] data; private int size; public ArrayStack() {} public void push(int value) {} public int pop() {} public int peek() {} //instead of top public int size() {} }

 Queues

 Keep reading section 1.3  Finish Assignment 2  Due this Friday by 11:59pm  Keep working on Project 1  Due next Friday, September 18 by 11:59pm