Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cinda Heeren / Geoffrey Tien

Similar presentations


Presentation on theme: "Cinda Heeren / Geoffrey Tien"— Presentation transcript:

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

2 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

3 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

4 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

5 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

6 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: * + 3 – 17 12 3 Infix: 5 + (( ) * 4 ) – 3 = 14 September 28, 2017 Cinda Heeren / Geoffrey Tien

7 RPN example * + 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

8 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

9 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

10 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

11 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

12 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

13 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

14 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

15 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

16 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

17 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

18 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

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


Download ppt "Cinda Heeren / Geoffrey Tien"

Similar presentations


Ads by Google