Abstract Data Types Stack October 01 2018 Cinda

Abstract Data Types Stack October 01, 2018 Cinda Heeren / Geoffrey Tien 1

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 October 01, 2018 Cinda Heeren / Geoffrey Tien 2

ADT operators 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 October 01, 2018 Cinda Heeren / Geoffrey Tien 3

ADT operators 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 October 01, 2018 Cinda Heeren / Geoffrey Tien 4

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 … October 01, 2018 Cinda Heeren / Geoffrey Tien 5

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 October 01, 2018 Cinda Heeren / Geoffrey Tien = 14 6

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

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() October 01, 2018 Cinda Heeren / Geoffrey Tien 8

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 October 01, 2018 Cinda Heeren / Geoffrey Tien 9

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 October 01, 2018 Cinda Heeren / Geoffrey Tien 10

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 Must also have constructor(s) and destructor • 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 October 01, 2018 Cinda Heeren / Geoffrey Tien 11

Call stack. . . is really a stack (mostly)! (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 October 01, 2018 Cinda Heeren / Geoffrey Tien 12

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 October 01, 2018 Cinda Heeren / Geoffrey Tien 13

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) October 01, 2018 Cinda Heeren / Geoffrey Tien 14

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 October 01, 2018 Cinda Heeren / Geoffrey Tien 15

Array stack example index of top is current size – 1 6 1 7 8 0 1 2 3 October 01, 2018 4 5 Stack st(); st. push(6); st. push(1); st. push(7); st. push(8); st. pop(); Cinda Heeren / Geoffrey Tien //top //top = = = 0 1 2 3 2 16

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 October 01, 2018 Cinda Heeren / Geoffrey Tien 17

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 18 27 27 52 52 34 34 October 01, 2018 Cinda Heeren / Geoffrey Tien 18

Readings for this lesson • Carrano & Henry – Chapter 6 (Stack ADT) – Chapter C 1. 3 (Templates) • Next class: – Carrano & Henry, Chapter 13. 1 -13. 2 (Queue ADT) October 01, 2018 Cinda Heeren / Geoffrey Tien 19
- Slides: 19