Presentation is loading. Please wait.

Presentation is loading. Please wait.

EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019

Similar presentations


Presentation on theme: "EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019"— Presentation transcript:

1 EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Lecture 20: Array-based stacks

2 Announcements/reminders
Exam 2: Monday, 4/1, 3-5 PM, Ball 214 Use same poll as before to request alt. exam (I’ll repost link) Program 3 coming soon … Today’s lecture Review: Stack basics 5/14/2019 Data Structures: Lecture 19

3 Review: delete example
int *ptr; ptr = new int (100); ptr 100 delete ptr; //free the memory ptr ? delete frees space on heap ... ... but ptr still points to same address! Solution: assign freed pointers to NULL: ptr = NULL; 5/14/2019 Data Structures: Lecture 19

4 Review: delete with arrays
double *dptr; const int SIZE = 10; dptr = new double[SIZE]; //80 bytes for(int i=0; i<SIZE; ++i) cin >> dptr[i]; fun1(dptr, SIZE); // pass array to fun1 delete [] dptr; //free all 10 elements dptr = NULL; 5/14/2019 Data Structures: Lecture 19

5 Data Structures: Lecture 19
Review: List ADT A sequence of a finite number of data items, all of the same type Basic operations Construction: create empty list Empty: check if the list is empty Insert: add an item to the list Delete: remove an item from the list Traverse: go through part or all of list, accessing and processing elements in order Types of traversal include search, output (to screen or file), copy, rearrange (usually sort) 5/14/2019 Data Structures: Lecture 19

6 Data Structures: Lecture 19
Review: Stack ADT Stack is last-in, first-out (LIFO) data structure Definition Ordered collection of data items Can only be accessed at one end (top of stack) Operations Construction (start with empty stack) Check if stack is empty Push: add data to the top of the stack Pop: remove data from the top of the stack Read item at top of stack 5/14/2019 Data Structures: Lecture 19

7 Stack implementations
Very similar to list ADT Same basic idea: sequence of items Restrict access only to top element—simplifies implementation What data do we need in a stack object? Actual stack storage Location of top element Capacity (maybe) Implementation options Array-based stack Linked stack 5/14/2019 Data Structures: Lecture 21

8 Data Structures: Lecture 21
Array-based stacks Where should “top of stack” be in array? Highest-indexed element—no need to shift contents when pushing or popping elements Array-based list tracks capacity, current size Does stack object need to track capacity? Yes—must make sure we don’t overfill array Why doesn’t stack need to track current size? Top of stack = highest-indexed element Implicitly tracks size! 5/14/2019 Data Structures: Lecture 21

9 Array-based stack examples
Describe, in code or pseudo-code, how to write the following Stack member functions, assuming each element is a double Constructor: Stack(unsigned maxSize); Destructor: ~Stack(); Check if empty: bool empty() const; Add new element: void push(const double &val); Remove top element: void pop(); Read top element: double top(); Assume stack has following members double *list; // The actual data stored on the stack int tos; // Index for top of stack unsigned cap; // Capacity (max size) of stack 5/14/2019 Data Structures: Lecture 21

10 const variables, arguments, methods
const keyword  value won’t be changed Define constant values const int CAPACITY = 1024; Indicate function argument won’t be modified Used with reference arguments when pass-by-reference used to save space ElementType f(const Stack &myStack); const methods won’t modify calling object bool empty() const; Given Stack S, if I write: S.empty(); S is the calling object  empty() accesses member(s) of object S 5/14/2019 Data Structures: Lecture 14

11 Default function arguments
Prototypes can specify default values Example: Stack constructor in .h file Stack(unsigned maxSize = 1024); If argument provided, maxSize = argument Otherwise, maxSize = 1024 This constructor: default & parameterized! Stack S1(256);  creates stack w/max size 256 Stack S2;  creates stack w/max size 1024 Can be generalized to any function Given prototype int f(int a, int b=10); x = f(5, 15);  a = 5, b = 15 y = f(-1);  a = -1, b = 10 5/14/2019 Data Structures: Lecture 16

12 Dynamic allocation and constructors
5/14/2019 Data Structures: Lecture 19

13 Array-based stack example solutions
Stack::Stack() : myTop(-1) { // if dynamic array, allocate array } bool Stack::isEmpty() { return (myTop == -1); void Stack::push(const StackElement &val) { if (myTop < CAPACITY – 1) { myTop++; myArray[myTop] = val; else // handle error appropriately 5/14/2019 Data Structures: Lecture 21

14 Data Structures: Lecture 21
void Stack::pop() { if (!isEmpty()) myTop--; else // handle error } StackElement Stack::top() { if (!isEmpty()) return myArray[myTop]; else { // return garbage value cerr << "Can’t read from empty stack\n"; return myArray[CAPACITY-1]; 5/14/2019 Data Structures: Lecture 21

15 Data Structures: Lecture 19
Final notes Next time: more on stacks Details of array-based implementation Dynamic allocation, constructors, and destructors Operator overloading Reminders: Program 2 due 3/21 Exam 2: Monday, 4/1, 3-5 PM, Ball 214 Use same poll as before to request alt. exam (I’ll repost link) 5/14/2019 Data Structures: Lecture 19


Download ppt "EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019"

Similar presentations


Ads by Google