[CS302-Data Structures] Homework 2: Stacks

Similar documents
CS302 Data Structures using C++

STACKS AND QUEUES. Problem Solving with Computers-II

Stacks and their Applications

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

Stack Abstract Data Type

Stack and Its Implementation

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

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

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

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

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.

Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1

Lecture Data Structure Stack

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

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

Data Structures & Algorithm Analysis. Lecturer: Souad Alonazi

CSE143 Exam with answers Problem numbering may differ from the test as given. Midterm #2 February 16, 2001

Lecture No.04. Data Structures

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

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

The Stack and Queue Types

CSCI 204 Introduction to Computer Science II. Lab 6: Stack ADT

March 13/2003 Jayakanth Srinivasan,

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

Data Structures And Algorithms

Lecture 12 ADTs and Stacks

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

Stacks. Revised based on textbook author s notes.

Linear Data Structure

Project 1: Implementation of the Stack ADT and Its Application

Formal Languages and Automata Theory, SS Project (due Week 14)

IV. Stacks. A. Introduction 1. Consider the 4 problems on pp (1) Model the discard pile in a card game. (2) Model a railroad switching yard

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)

CSE 230 Intermediate Programming in C and C++

Introduction. Problem Solving on Computer. Data Structures (collection of data and relationships) Algorithms

CSC 222: Computer Programming II. Spring 2004

Stacks Fall 2018 Margaret Reid-Miller

Problem with Scanning an Infix Expression

-The Hacker's Dictionary. Friedrich L. Bauer German computer scientist who proposed "stack method of expression evaluation" in 1955.

Data Structures and Algorithms

CS 171: Introduction to Computer Science II. Stacks. Li Xiong

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

Stacks (Section 2) By: Pramod Parajuli, Department of Computer Science, St. Xavier s College, Nepal.

Lab 7 1 Due Thu., 6 Apr. 2017

Stacks, Queues (cont d)

CS 211 Programming Practicum Spring 2017

CSC 222: Computer Programming II. Spring 2005

CMPT 225. Lecture 9 Stack

BBM 201 DATA STRUCTURES

1. Stack Implementation Using 1D Array

CS 211 Programming Practicum Fall 2018

BBM 201 DATA STRUCTURES

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

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

Lab 2: ADT Design & Implementation

Top of the Stack. Stack ADT

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

Data Structures Week #3. Stacks

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

infix expressions (review)

CS 211 Programming Practicum Spring 2018

Topic Binary Trees (Non-Linear Data Structures)

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Stack. Data structure with Last-In First-Out (LIFO) behavior. Out

Top of the Stack. Stack ADT

DATA STRUCTURES AND ALGORITHMS

CS 206 Introduction to Computer Science II

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

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

ADT Sorted List Operations

Lists are great, but. Stacks 2

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

ADT Stack. Inserting and deleting elements occurs at the top of Stack S. top. bottom. Stack S

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

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

SimpleCalc. which can be entered into a TI calculator, like the one on the right, like this:

CSE P 501 Exam 8/5/04

12 Abstract Data Types

ADTs Stack and Queue. Outline

Special Section: Building Your Own Compiler

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

VTU NOTES QUESTION PAPERS NEWS RESULTS FORUMS THE STACK

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Stacks. Manolis Koubarakis. Data Structures and Programming Techniques

Largest Online Community of VU Students

Collections Chapter 12. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Operators & Expressions

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

CSCI 200 Lab 4 Evaluating infix arithmetic expressions

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Stacks CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008

BBM 201 DATA STRUCTURES

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

1. In C++, reserved words are the same as predefined identifiers. a. True

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

CS 247: Software Engineering Principles. ADT Design

Transcription:

[CS302-Data Structures] Homework 2: Stacks Instructor: Kostas Alexis Teaching Assistants: Shehryar Khattak, Mustafa Solmaz, Bishal Sainju Fall 2018 Semester Section 1. Stack ADT Overview wrt Provided Code [supports explanation of provided code] Section 2. Implementation Approaches [supports explanation of provided code] Section 3. Compilation Directions wrt Provided Code [supports explanation of provided code] Section 4. Testing [supports explanation of provided code] Section 5. Programming Exercise 1 Section 6. Programming Exercise 2 Section 1. Stack ADT Overview wrt Provided Code Data Items The data items in a stack are of generic DataType. Structure The stack data items are linearly ordered from the most recently added (the top) to the least recently added (the bottom). This is a LIFO scheme. Data items are inserted onto (pushed) and removed from (popped) the top of the stack. Operations Stack (int maxnumber = MAX_STACK_SIZE) Constructor. Creates an empty stack. Allocates enough memory for a stack containing maxnumber data items (if necessary). Stack (const Stack& other) Copy constructor. Initializes the stack to be equivalent to the other Stack object parameter. Stack& operator= (const Stack& other)

Overloaded assignment operator. Sets the stack to be equivalent to the other Stack object parameter and returns a reference to the modified stack. ~Stack() Destructor. Deallocates the memory used to store the stack. void push (const DataType& newdataitem) throw ( logic_error ) Stack is not full. Inserts newdataitem onto the top of the stack. DataType pop() throw ( logic_error ) Stack is not empty Removes the most recently added (top) data item from the stack and returns the value of the deleted item. void Clear() Removes all the data items in the stack. bool isempty() const Returns true if the stack is empty. Otherwise, returns false. bool isfull () const

Returns true if the stack is full. Otherwise, returns false. void showstructure() const Outputs the data items in a stack. If the stack is empty, it outputs Empty stack. Note that this operation is intended for testing/debugging purposes only. It only supports stack data items that are one of c++ predefined data types (int, char, etc) or other data structures with overridden ostream operator<<. Section 2. Implementation Approaches As in the class, we consider both Array-based implementations Linked-List implementations For studying we refer you to the course slides. Section 3. Compilation Directions wrt Provided Code Edit config.h and change the value of LAB6_TEST1 to 1 to test the link-based implementation. If set to 0, then the array-based implementation is tested. Recompile test6.cpp. Section 4. Testing Test your implementation of the linked list Stack ADT using the program in the file test6.cpp. Section 5. Programming Exercise 1 We commonly write arithmetic expressions in the so-called infix form. That is, with each operator placed between its operand, as below (3+4)*(5/2) Although we are comfortable writing expressions in this form, infix form has the disadvantage that parentheses must be used to indicate the order in which operators are to be evaluated. These parentheses, in turn, complicate the evaluation process.

Evaluation is much easier if we can simply evaluate operators from left to right. Unfortunately, this evaluation will not work with the infix form of arithmetic expressions. However, it will work if the expression is in postfix form. In the postfix form of an arithmetic expression, each operator is placed immediately after its operands. The expression above is written in postfix form as 34+52/* Note that both forms place the numbers in the same order (reading from left to right). The order of the operators is different, however, because the operators in the postfix form are positioned in the order that they are evaluated. The resulting postfix expression is hard to read at first, but it is easy to evaluate programmatically. We will do so with stacks. Suppose you have an arithmetic expression in postfix form that consists of a sequence of single digit, nonnegative integers and the four basic arithmetic operators (+,-,*,/). This expression can be evaluated using the following algorithm in conjuction with a stack of floating-point numbers. Read the expression character-by-character. As each character is read in: If the character corresponds to a single digit number (characters 0 to 9 ), then push the corresponding floating-point number onto the stack. If the character corresponds to one of the arithmetic operators (characters +, -, *, / ), then o Pop a number off of the stack. Call it operand1. o Pop a number off of the stack. Call it operand 2. o Combine these operands using the arithmetic operartor, as follows Result = operand2 operator operand1 o Puhs result onto the stack. When the end of the expression is reached, pop the remaining number off the stack. This number is the value of the expression. Applying this algorithm to the arithmetic expression 17.5 as expected. 34+52/* Exercise 1. Assignment 1. Create a program that reads the postfix form of an arithmetic expression, evaluates it, and outputs the result. Assume that the expression consists of single-digit, nonnegative integers ( 0 to 9 ) and the four basic arithmetic operators ( +, -, *, / ). Further assume that the arithmetic expression is input from the keyboard with all the characters separated by white space on one line. Save your program in a file called postfix.cpp

Exercise 1. Assignment 2. Create a test plan involving the execution of 5 expressions for which you must provide the infix and postfix notations, alongside their result in your report document. Section 6. Programming Exercise 2 One of the tasks that compilers and interpreters must frequently perform is deciding whether some pair of expression delimeters are properly paired, even if they are embedded multiple pairs deep. Consider the following C++ expression. a=(f(b)-(c+d))/2; The compiler has to be able to determine which pairs of opening and closing parentheses go together and whether the whole expression is correctly parenthesized. A number of possible errors can occur because of incomplete pairs of parentheses more of one than the other or because of improperly placed parentheses. For instance, the expression below lacks a closing parenthesis. a=(f(b)-(c+2)/2; A stack is extremely helpful in implementing solutions to this type of problem because of its LIFO behavior. A closing parenthesis needs to be matched with the most recently encountered opening parenthesis. This is handled by pushing opening parentheses onto a stack as they are encountered. When a closing parenthesis is encountered, it should be possible to pop the matching opening parenthesis off the stack. If it is determined that every closing parenthesis had a matching opening parenthesis, then the expression is valid. bool delimetersok ( const string& expression ) Returns true if all the parentheses and braces in the string are legally paired. Otherwise, returns false. Exercise 2. Assignment 1. Save a copy of the delimiters.cs as delimeters.cpp. Implement the delimetersok operation inside the delimeters.cpp program. Exercise 2. Assignment 2. Add test 5 cases that check whether your implementation of the delimtersok operation correctly detects improperly paired delimeters in input expressions.