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

Slides:



Advertisements
Similar presentations
Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
Advertisements

Lists: An internal look
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
Lecture 5 Sept 15 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
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.
Lecture 7 Sept 16 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Stacks and Queues COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Stacks.
Stacks  Standard operations: IsEmpty … return true iff stack is empty Top … return top element of stack Push … add an element to the top of the stack.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
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.
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.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
Pointers OVERVIEW.
1 8. Run-Time Arrays — Intro. to Pointers For declarations like double doubleVar; char charVar = 'A'; int intVar = 1234; the compiler____________the object.
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
1 Recall Definition of Stack l Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of.
1 8. Run-Time Arrays — Intro. to Pointers For declarations like double doubleVar; char charVar = 'A'; int intVar = 1234; the compiler constructs the object.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
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.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters.
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
1 Classes classes and objects - from object-oriented programming point of view class declaration class class_name{ data members … methods (member functions)
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
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.
ECE Application Programming
Linked Lists Chapter 6 Section 6.4 – 6.6
Data Structures and Algorithms
Dr. Bernard Chen Ph.D. University of Central Arkansas
CISC181 Introduction to Computer Science Dr
Chapter 1-4 CSc 212 Data Structures, Sec AB CCNY, Spring 2012
Stack and Queue APURBO DATTA.
Stacks Chapter 4.
Chapter 4 Linked Lists
This pointer, Dynamic memory allocation, Constructors and Destructor
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Dynamically Allocated Memory
CMSC 341 Lecture 5 Stacks, Queues
Function and class templates
Array Lists Chapter 6 Section 6.1 to 6.3
An Introduction to Pointers
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Linked Lists.
Stacks as Linked lists top_node typedef Stack_entry Node_entry;
Indirection.
Data Structures and Algorithms
EECE.2160 ECE Application Programming
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2019 Lecture 29: Linked queues
Dynamic allocation (continued)
Stacks, Queues, and Deques
Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Getting queues right … finally (?)
Instructor: Dr. Michael Geiger Spring 2019 Lecture 34: Exam 3 Preview
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2017
Abstract Data Types Stacks CSCI 240
Chapter 1-4 CSc 212 Data Structures, Sec FG CCNY, 2009
Lecture 4 – Data collection List ADT
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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