Cinda Heeren / Geoffrey Tien

Slides:



Advertisements
Similar presentations
Stack & Queues COP 3502.
Advertisements

C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
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 Data Structures II Review COSC 2006 April 14, 2017
CMPT 225 Stacks.
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.
Stacks and Queues.  Abstract Data Types  Stacks  Queues  Priority Queues September 2004John Edgar2.
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 CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
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.
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top.
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.
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.
Stacks, Queues, and Deques
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
Topic 3 The Stack ADT.
Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Data Structures: CSCI 362 – Stack Implementation Data Structures: CSCI 362 – Stack Implementation lecture notes adapted from Data Structures with C++ using.
Data Structures and Algorithms
Arrays and Linked Lists.  Abstract Data Types  Stacks  Queues  Introducing Priority Queues John Edgar2.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
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.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
Week 15 – Monday.  What did we talk about last time?  Tries.
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.
Insertion sort Loop invariants Dynamic memory
Data Structures Using C++ 2E
Review Array Array Elements Accessing array elements
Stacks.
ADT description Implementations
Data Structure By Amee Trivedi.
Stacks The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
Cinda Heeren / Geoffrey Tien
Copy Constructor / Destructors Stacks and Queues
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
Dr. Bernard Chen Ph.D. University of Central Arkansas
Homework 4 questions???.
Hassan Khosravi / Geoffrey Tien
C++ Object-Oriented Programming
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Stacks.
Week 15 – Monday CS221.
Stacks Chapter 4.
Visit for more Learning Resources
Stacks and Queues.
CMSC 341 Lecture 5 Stacks, Queues
structures and their relationships." - Linus Torvalds
Pointers and Linked Lists
Stacks, Queues, and Deques
Stacks Chapter 5 Adapted from Pearson Education, Inc.
structures and their relationships." - Linus Torvalds
Stacks, Queues, and Deques
Stacks, Queues, and Deques
Stacks and Queues 1.
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Stacks Data structure Elements added, removed from one end only
Introduction to Data Structure
Stacks CS-240 Dick Steflik.
Stacks, Queues, and Deques
structures and their relationships." - Linus Torvalds
Abstract Data Types Stacks CSCI 240
Presentation transcript:

Cinda Heeren / Geoffrey Tien Abstract Data Types Stack September 28, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Abstract Data Types and Data Structures An Abstract Data Type (ADT) is: A collection of data Describes what data are stored but not how they are stored A set of operations on the data Describes precisely what effect the operations have on the data Does not specify how the operations are carried out An ADT is not an actual (concrete) structure A data structure can be used to implement an ADT September 28, 2017 Cinda Heeren / Geoffrey Tien

ADT Operators Mutators Accessors Constructors Other Object-oriented programming Mutators Often known as setters Operations that change the contents of an ADT, usually by: Adding data into a collection or Removing data from a collection Different ADTs allow data to be added and removed at different locations Accessors Constructors Other September 28, 2017 Cinda Heeren / Geoffrey Tien

ADT Operators Mutators Accessors Constructors Other Object-oriented programming Mutators Accessors Often known as getters Retrieve data from the collection e.g. the "first" item in the collection, or some arbitrary item Ask questions about the data collection Is it full? How many items are stored? … Constructors Other September 28, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Implementing ADTs Data structure choice Example: A “Dictionary” ADT Associates a short “key” with a longer description of the information Operations: add entry, remove entry, look up entry, etc. Can be implemented using many different data structures List, binary search tree, red-black tree, hash table, etc. Different data structures may make tradeoffs: Time vs space Generality vs simplicity One operation’s performance vs another’s … September 28, 2017 Cinda Heeren / Geoffrey Tien

ADT application – Postfix notation Reverse Polish Notation (RPN) Reverse Polish Notation (RPN) Also known as postfix notation A mathematical notation Where every operator follows its operands Example Infix: 5 + ((1 + 2) * 4) − 3 RPN: 5 1 2 + 4 * + 3 – 17 12 3 Infix: 5 + (( 1 + 2 ) * 4 ) – 3 = 14 September 28, 2017 Cinda Heeren / Geoffrey Tien

RPN example 5 1 2 + 4 * + 3 – To evaluate a postfix expression, read it from left to right Store ‘5’ Store ‘1’ Apply ‘+’ to last two operands Store ‘2’ Apply ‘*’ to last two operands Store ‘4’ Apply ‘+’ to last two operands Apply ‘–’ to last two operands Store ‘3’ 4 2 Note: the postfix string contains integers and characters, but the data collection contains only integers 12 1 3 14 17 5 Retrieve result September 28, 2017 Cinda Heeren / Geoffrey Tien

Calculating a Postfix Expression for each input symbol if symbol is operand store(operand) if symbol is operator RHS = remove() LHS = remove() result = LHS operator RHS store(result) result = remove() September 28, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Describing an ADT What are the storage properties of the data type that was used? Specifically how are items stored and removed? Note that items are never inserted between existing items The last item to be entered is the first item to be removed Known as LIFO (Last In First Out) This ADT is referred to as a stack September 28, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien The Stack ADT A stack only allows items to be inserted and removed at one end We call this end the top of the stack The other end is called the bottom Access to other items in the stack is not allowed A stack can be used to naturally store data for postfix notation Operands are stored at the top of the stack And removed from the top of the stack Notice that we have not (yet) discussed how a stack should be implemented Just what it does An example of an Abstract Data Type September 28, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Stack behaviour A stack ADT should support at least the first two of these operations: push – insert an item at the top of the stack pop – remove and return the top item peek – return the top item is_empty – does the stack contain any items ADT operations should be performed efficiently The definition of efficiency varies from ADT to ADT The order of the items in a stack is based solely on the order in which they arrive Must also have constructor(s) and destructor September 28, 2017 Cinda Heeren / Geoffrey Tien

Call stack... is really a stack! (aside: the heap memory is not a heap ADT) We have already seen a situation where stack-like behaviour has been demonstrated – the function call stack / stack memory! Declare a local variable – push it onto stack memory Call a function Push return value space, formal parameters to stack memory Pop any local function variables, and parameters Return value space is popped by the caller Pop local variables when out of scope September 28, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Stack Implementation Design notes Assume that we plan on using a stack that will store integers and have these methods (or we can use templates) void push(int) int pop() We can design other modules that use these methods Without having to know anything about how they, or the stack itself, are implemented We will use classes to encapsulate stacks Encapsulate – enclose in A class is a programming construct that contains Data for the class, and Operations of the class September 28, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Stack Implementation The stack ADT can be implemented using a variety of data structures, e.g. Arrays Linked Lists Both implementations must implement all the stack operations In constant time (time that is independent of the number of items in the stack) September 28, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Stack Implementation Using arrays We need to keep track of the index that represents the top of the stack When we insert an item increment this index When we delete an item decrement this index Insertion or deletion time is independent of the number of items in the stack September 28, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Array Stack Example index of top is current size – 1 Stack st(); st.push(6); //top = 0 st.push(1); //top = 1 st.push(7); //top = 2 st.push(8); //top = 3 st.pop(); //top = 2 6 1 7 8 1 2 3 4 5 September 28, 2017 Cinda Heeren / Geoffrey Tien

Array Stack Implementation Summary Easy to implement a stack with a (dynamic) array And push and pop can be performed in constant time Once the array is full No new values can be inserted or A new, larger, array can be created And the existing items copied to this new array This will take linear time, but should occur only rarely September 28, 2017 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Stack Implementation Using a Linked List Recall linked list construction from previous lessons New nodes added at the “null” end of the list Or inserted anywhere in the list Implement a linked-list stack by adding/removing from the front of the list a a 18 18 27 27 52 52 34 34 September 28, 2017 Cinda Heeren / Geoffrey Tien

Readings for this lesson Koffman: Chapter 5 (Stack ADT) Next class: Koffman Chapter 6 (Queue ADT) September 28, 2017 Cinda Heeren / Geoffrey Tien