ADTs Stack and Queue. Outline

Similar documents
Chapter 5. ADTs Stack and Queue

Propedéutico de Programación

Definition of Stack. 5 Linked Structures. Stack ADT Operations. ADT Stack Operations. A stack is a LIFO last in, first out structure.

ADT Unsorted List. Outline

ADTs Stack and Queue. Outline

ADT Sorted List Operations

CS 308 Data Structures Spring Dr. George Bebis Final Exam

What is a List? elements, with a linear relationship. first) has a unique predecessor, and. unique successor.

Data Structures And Algorithms

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5

CS 216 Exam 1 Fall SOLUTION

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

Extra Credit: write mystrlen1 as a single function without the second parameter int mystrlen2(char* str)

! A data type for which: ! In fact, an ADT may be implemented by various. ! Examples:

Programming with Recursion. What Is Recursion?

SCJ2013 Data Structure & Algorithms. Queue. Nor Bahiah Hj Ahmad & Dayang Norhayati A. Jawawi

Associate Professor Dr. Raed Ibraheem Hamed

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

moretosearch = (location < length);

Ch. 18: ADTs: Stacks and Queues. Abstract Data Type

What can we do with Coin dispenser?

The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.

Queue with Array Implementation

Data Structures G5029

Chapter 8. Binary Search Trees. Fall 2013 Yanjun Li CISC Trees. Owner Jake. Waitress Waiter Cook Helper Joyce Chris Max Len

DATA STRUCTURES AND ALGORITHMS LECTURE 08 QUEUES IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD

More Group HW. #ifndef Stackh #define Stackh. #include <cstdlib> using namespace std;

Stacks. Gaddis 18.1, Molly A. O'Neil CS 2308 :: Spring 2016

! A data type for which: ! An ADT may be implemented using various. ! Examples:

Data Structures using OOP C++ Lecture 9

Stacks Stack Examples:

Chapter 18: Stacks And Queues

Data Structures (INE2011)

Chapter 18: Stacks And Queues. Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Queues. Gaddis 18.4, Molly A. O'Neil CS 2308 :: Spring 2016

IT 4043 Data Structures and Algorithms. Budditha Hettige Department of Computer Science

Lecture Data Structure Stack

STACKS AND QUEUES. Problem Solving with Computers-II

Containers: Stack. Jordi Cortadella and Jordi Petit Department of Computer Science

Chapter 18: Stacks And Queues

Containers: Stack. The Stack ADT. The Stack ADT. The Stack ADT

[CS302-Data Structures] Homework 2: Stacks

CSC 1052 Algorithms & Data Structures II: Queues

n Data structures that reflect a temporal relationship q order of removal based on order of insertion n We will consider:

Lecture 3: Stacks & Queues

Data Structures and Algorithms for Engineers

Abstract Data Types. Stack. January 26, 2018 Cinda Heeren / Geoffrey Tien 1

An Introduction to Queues With Examples in C++

8/28/12. Outline. Part 2. Stacks. Linear, time ordered structures. What can we do with Coin dispenser? Stacks

CSCD 326 Data Structures I Stacks

1 P age DS & OOPS / UNIT II

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. elements of the same type. Week 9. Gaddis: Chapter 18

! Mon, May 5, 2:00PM to 4:30PM. ! Closed book, closed notes, clean desk. ! Comprehensive (covers entire course) ! 30% of your final grade

CS24 Week 4 Lecture 2

BBM 201 DATA STRUCTURES

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

Computer Science 9608 (Notes) Chapter: 4.1 Computational thinking and problem-solving

The Abstract Data Type Queue. Module 8. The Abstract Data Type Queue. The Abstract Data Type Queue. The Abstract Data Type Queue

Chapter 4. ADT Sorted List

Fun facts about recursion

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Dynamic Data Structures

mywbut.com Exception Handling

Computer Science CS221 Test 2 Name. 1. Give a definition of the following terms, and include a brief example. a) Big Oh

LIFO : Last In First Out

1. Stack Implementation Using 1D Array

Stacks and Queues. Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms

Object Oriented Programming COP3330 / CGS5409

Stacks. Access to other items in the stack is not allowed A LIFO (Last In First Out) data structure

Data Structures Week #3. Stacks

PROBLEM 1 : (Vocabulary: 8 points) For each of the words/phrases below, circle the denition that is the best description as it pertains in the context

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

CS302 - Data Structures using C++

Stacks. Manolis Koubarakis. Data Structures and Programming Techniques

ADTs: Stacks and Queues

Lists, Stacks and Queues in C. CHAN Hou Pong, Ken CSCI2100A Data Structures Tutorial 4

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. Week 9. elements of the same type.

Lecture No.04. Data Structures


csci 210: Data Structures Stacks and Queues

Problem Solving with C++

Lecture Notes CPSC 122 (Fall 2014) Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S.

CSE 143. Complexity Analysis. Program Efficiency. Constant Time Statements. Big Oh notation. Analyzing Loops. Constant Time Statements (2) CSE 143 1

cs Java: lecture #6

Data Structure (CS301)

Apply to be a Meiklejohn! tinyurl.com/meikapply

First Examination. CS 225 Data Structures and Software Principles Spring p-9p, Tuesday, February 19

Stack and Queue. Stack:

Name CPTR246 Spring '17 (100 total points) Exam 3

Stack Implementation

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof.

Link-Based Implementations. Chapter 4

BBM 201 DATA STRUCTURES

Another common linear data structure similar to. new items enter at the back, or rear, of the queue. Queue is an ADT with following properties

CS302 - Data Structures using C++

CSC 1052 Algorithms & Data Structures II: Linked Queues

CMPT 225. Lecture 9 Stack

Outline and Reading. The Stack ADT ( 4.2.1) Applications of Stacks ( 4.2.3) Array-based implementation ( 4.2.2) Growable array-based stack.

Transcription:

Chapter 5 ADTs Stack and Queue Fall 2017 Yanjun Li CS2200 1 Outline Stack Array-based Implementation Linked Implementation Queue Array-based Implementation Linked Implementation Comparison Fall 2017 Yanjun Li CS2200 2

Stacks of Coins and Bills Fall 2017 Yanjun Li CS2200 3 Stacks of Boxes and Books TOP OF THE STACK TOP OF THE STACK Fall 2017 Yanjun Li CS2200 4

Stacks Logical level What do these composite objects all have in common? Fall 2017 Yanjun Li CS2200 5 Stacks Stack An abstract data type in which elements are added and removed from only one end. A last in, first out (LIFO) structure. Fall 2017 Yanjun Li CS2200 6

Stacks Logical level What operations would be appropriate for a stack? Fall 2017 Yanjun Li CS2200 7 Stacks Transformers Push Pop Observers IsEmpty IsFull Top change state observe state What about an iterator? Fall 2017 Yanjun Li CS2200 8

Application Level Stacks For what type of problems would stacks be useful? LIFO: A stack is great for reversing data. Operating system function calls Finding palindromes Expression evaluation & syntax parsing Fall 2017 Yanjun Li CS2200 9 Stack Applications Operating system function calls void DrawSquare(int x, int y, int edge) DrawLine(x, y, edge, HORIZONTAL); DrawLine(x, y, edge, VERTICAL); DrawLine(x+edge, y, edge, VERTICAL); DrawLine(x, y+edge, edge, HORIZONTAL); int main () DrawSquare(1,2,3); return 0; Fall 2017 Yanjun Li CS2200 10

Stack Applications Finding palindromes (Lab!) Fall 2017 Yanjun Li CS2200 11 Stack Applications Help Converting Decimal to Binary (pseudocode) 1) Read (number) 2) Loop (number > 0) 1) digit = number modulo 2 2) print (digit) 3) number = number / 2 // from Data Structures by Gilbert and Forouzan Problem: The binary numbers are printed backwards. 19 becomes 11001 instead of 10011 Solution: push each binary number onto the stack and pop the digit out of the stack and print it at the end. Fall 2017 Yanjun Li CS2200 12

Stacks class StackType public: StackType(); ~StackType(); bool IsEmpty() const; bool IsFull() const; void Push(ItemType item); void Pop( ); ItemType Top() const; Fall 2017 Yanjun Li CS2200 13 Stacks Implementation Level Array-based Implementation Linked Structure Fall 2017 Yanjun Li CS2200 14

Array-Based Implementation private: int top; ItemType items[max_items]; ; stack [0] [1] [2]. [M..].items.top Fall 2017 Yanjun Li CS2200 15 Array-Based Implementation Give a series of operations that could produce this situation Fall 2017 Yanjun Li CS2200 16

Array-Based Implementation To what do we initialize data member top? 0 or -1 When push, do we increment or store first? 0: store and increment -1: increment and store Which is better? Think about member function Top() StackType::StackType() top = -1; Fall 2017 Yanjun Li CS2200 17 Array-Based Implementation Before we code, we must consider error conditions Stack overflow The condition that results from trying to push an element on to a full stack Stack underflow The condition that results from trying to pop an empty stack Fall 2017 Yanjun Li CS2200 18

Array-Based Implementation //pre: Stack has been initialized. //post: function value = (stack is empty) bool StackType::IsEmpty() const return ( top == -1); //pre: stack has been initialized. //post: function value = (stack is full) bool StackType:: IsFull() const return ( top == MAX_ITEMS); What does const mean? Fall 2017 Yanjun Li CS2200 19 Array-Based Implementation //pre: stack has been initialized and is not full //post: newitem is at the top of the stack. bool StackType::Push(ItemType newitem) top++; items[top] = newitem; Fall 2017 Yanjun Li CS2200 20

Array-Based Implementation //pre: stack has been initialized. //post: if stack is full, throw an exception; //Else newitem is at the top of the stack. bool StackType::Push(ItemType newitem) throw (FullStack) if (IsFull()) throw FullStack(); top++; items[top] = newitem; What is FullStack( )? Fall 2017 Yanjun Li CS2200 21 Array-Based Implementation //pre: stack has been initialized and is not empty. //post: top element has been removed from stack. void StackType::Pop() top--; //pre: stack has been initialized and is not empty. //post: A copy of the top element is returned. ItemType StackType:: Top() const return (items[top]); Fall 2017 Yanjun Li CS2200 22

Array-Based Implementation //pre: stack has been initialized. //post: if stack is empty, throw an exception; else top element //has been removed from stack. void StackType::Pop() throw (EmptyStack) if (IsEmpty()) throw EmptyStack(); top--; //pre: stack has been initialized. //post: if stack is empty, throw an exception; else a copy of the //top element is returned. ItemType StackType::Top() const throw (EmptyStack) if (IsEmpty()) throw EmptyStack(); return (items[top]); What is EmptyStack? Fall 2017 Yanjun Li CS2200 23 Test Plan Clear-box strategy to check each operation. Push() & Pop() while it is empty or full. Fall 2017 Yanjun Li CS2200 24

Linked Implementation The logical level (public part of the class declaration) stays the same; class StackType public: StackType(); ~StackType(); bool IsEmpty() const; bool IsFull() const; void Push(ItemType item); void Pop( ); ItemType Top() const; Fall 2017 Yanjun Li CS2200 25 Linked Implementation The implementation level (private part of the class declaration) changes private: NodeType* topptr; ;.info.next Stack.topPtr D Pointer to the next node in the stack Can we borrow code from UnsortedType for Push and Pop? Fall 2017 Yanjun Li CS2200 26

Linked Implementation //pre: the stack is not full //post: the new item is added on top of the stack void StackType::Push(ItemType newitem) NodeType* location; location = new NodeType; location->info = newitem; location>next = topptr; topptr = location;.info.next.info.next newitem D.topPtr location Fall 2017 Yanjun Li CS2200 27 Linked Implementation //pre: the stack is not empty //post: the top item is removed from the top of the //stack void StackType::Pop() NodeType* tempptr; tempptr = topptr; topptr = topptr->next; delete tempptr; Does this work for stacks of one item? More than one item? Fall 2017 Yanjun Li CS2200 28

Linked Implementation More than one item Fall 2017 Yanjun Li CS2200 29 Linked Implementation One item Fall 2017 Yanjun Li CS2200 30

Linked Implementation What about the constructor, destructor, and observer functions? We can borrow all but Top() from class UnsortedType List //pre: the stack is not empty //post: the item on the top of the stack is //returned. ItemType StackType::Top() return topptr->info; Fall 2017 Yanjun Li CS2200 31 Other Member Functions Constructor Destructor Free all the node spaces. isempty isfull Fall 2017 Yanjun Li CS2200 32

Array vs. Linked Structure A serious drawback of array-based implementation: the size of a stack must be determined when a stack object is declared. Size is not enough or Space is wasted. Fall 2017 Yanjun Li CS2200 33 Big-O Comparison Time Class constructor Class destructor IsFull() IsEmpty() Push() Pop() Array-Based Implementation Linked Implementation O(n) Fall 2017 Yanjun Li CS2200 34

Reference Reproduced from C++ Plus Data Structures, 4 th edition by Nell Dale. Reproduced by permission of Jones and Bartlett Publishers International. Fall 2017 Yanjun Li CS2200 35