Data Structures, Practice Homework 2, with Solutions (not to be handed in)



Similar documents
Data Structures, Practice Homework 3, with Solutions (not to be handed in)

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

Pointers and Linked Lists

Cpt S 223. School of EECS, WSU

MS Visual C++ Introduction. Quick Introduction. A1 Visual C++

Analysis of a Search Algorithm

Course: Programming II - Abstract Data Types. The ADT Queue. (Bobby, Joe, Sue, Ellen) Add(Ellen) Delete( ) The ADT Queues Slide Number 1

Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

7.1 Our Current Model

Linked Lists Linked Lists, Queues, and Stacks

CSI33 Data Structures

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

The C++ Language. Loops. ! Recall that a loop is another of the four basic programming language structures

Last not not Last Last Next! Next! Line Line Forms Forms Here Here Last In, First Out Last In, First Out not Last Next! Call stack: Worst line ever!

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

CmpSci 187: Programming with Data Structures Spring 2015

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

Appendix K Introduction to Microsoft Visual C++ 6.0

Passing 1D arrays to functions.

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

What is a Stack? Stacks and Queues. Stack Abstract Data Type. Java Interface for Stack ADT. Array-based Implementation

Chapter 8 Selection 8-1

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

PIC 10A. Lecture 7: Graphics II and intro to the if statement

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Basics of I/O Streams and File I/O

6. Control Structures

Answers to Review Questions Chapter 7

Ch 7-1. Object-Oriented Programming and Classes

Stacks. Stacks (and Queues) Stacks. q Stack: what is it? q ADT. q Applications. q Implementation(s) CSCU9A3 1

Queue Implementations

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

While Loop. 6. Iteration

C++ Outline. cout << "Enter two integers: "; int x, y; cin >> x >> y; cout << "The sum is: " << x + y << \n ;

Common Data Structures

Chapter 3: Restricted Structures Page 1

Outline. Computer Science 331. Stack ADT. Definition of a Stack ADT. Stacks. Parenthesis Matching. Mike Jacobson

Comp151. Definitions & Declarations

1. The First Visual C++ Program

Stacks. Linear data structures

Programming with Data Structures

Basic Data Structures and Algorithms

13 Classes & Objects with Constructors/Destructors

Curriculum Map. Discipline: Computer Science Course: C++

Appendix M: Introduction to Microsoft Visual C Express Edition

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

DATA STRUCTURE - STACK

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Queues and Stacks. Atul Prakash Downey: Chapter 15 and 16

Sample Questions Csci 1112 A. Bellaachia

Data Structures Using C++

CS 2412 Data Structures. Chapter 3 Queues

! stack, queue, priority queue, dictionary, sequence, set e.g., a Stack is a list implements a LIFO policy on additions/deletions.

LINKED DATA STRUCTURES

EP241 Computer Programming

An Incomplete C++ Primer. University of Wyoming MA 5310

Creating a Simple Visual C++ Program

Outline. The Stack ADT Applications of Stacks Array-based implementation Growable array-based stack. Stacks 2

Queues. Manolis Koubarakis. Data Structures and Programming Techniques

Constructor, Destructor, Accessibility and Virtual Functions

Cours de C++ Utilisations des conteneurs

Calling the Function. Two Function Declarations Here is a function declared as pass by value. Why use Pass By Reference?

CISC 181 Project 3 Designing Classes for Bank Accounts

1.1.3 Syntax The syntax for creating a derived class is very simple. (You will wish everything else about it were so simple though.

Object Oriented Software Design II

For the next three questions, consider the class declaration: Member function implementations put inline to save space.

Linked List as an ADT (cont d.)

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

STACKS,QUEUES, AND LINKED LISTS

COSC 181 Foundations of Computer Programming. Class 6

Help on the Embedded Software Block

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Compiler Construction

CS193D Handout 06 Winter 2004 January 26, 2004 Copy Constructor and operator=

Introduction to C++ Introduction to C++ Week 7 Dr Alex Martin 2013 Slide 1

COMPUTER SCIENCE 1999 (Delhi Board)

CompuScholar, Inc. Alignment to Utah's Computer Programming II Standards

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Object-Oriented Programming

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

7.7 Case Study: Calculating Depreciation

Deitel Dive-Into Series: Dive Into Microsoft Visual C++ 6

CSCI 123 INTRODUCTION TO PROGRAMMING CONCEPTS IN C++

C++FA 5.1 PRACTICE MID-TERM EXAM

C++ Input/Output: Streams

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

Algorithms and Data Structures Written Exam Proposed SOLUTION

HW3: Programming with stacks

C++ Support for Abstract Data Types

Cpt S 223. School of EECS, WSU

Transcription:

Data Structures, Practice Homework 2, with Solutions (not to be handed in) 1. Carrano, 4th edition, Chapter 7, Exercise 4. Consider a function int Queue::getNumberOfElements() const that returns the number of elements in a queue without changing the queue. (a) Write this function as a member function to the array-based ADT (b) Write this function as a member function to the pointer-based ADT 2. Carrano, 4th edition, Chapter 7, Exercise 10. An operation that displays the contents of a queue can be useful during program debugging. Add a display operation to the ADT queue such that (a) display uses only ADT queue operation, and so it is independent of the queue s implementation. (So write this one in pseudocode.) (b) display assumes and uses the pointer-based implementation of the ADT queue. (So write this one in C++, as another member function of pointer-based implementation of class Queue.) 3. Carrano, 4th edition, Chapter 7, Exercise 12. With the following data, hand-trace the execution of the bank-line simulation that this chapter describes. Each line of data contains an arrival time and a transaction time. Show the state of the queue and the event list at each step. 5 9 7 5 14 5 30 5 32 5 34 5 Note that at time 14 there is a tie between the execution of an arrival event and a. 4. Carrano, 4th edition, Chapter 8, Exercise 11. The section Class Templates describes a class template for List. Using this template, write C++ statements that define an ADT list of integers, and prompt the user to enter those integers into the list.

5. Carrano, 4th edition, Chapter 8, (part of) Exercise 12. Overload the assignment operator = for the pointer-based implementation of the class Stack. Recall two stacks are considered to be equal if they have the same number of elements, and exactly the same element in each place from the top to the bottom. (Hint: Study the copy constructors, and the implementation for class List on page 426.)

Solutions 1. Carrano, 4th edition, Chapter 7, Exercise 4. Consider a function int Queue::getNumberOfElements() const that returns the number of elements in a queue without changing the queue. (a) Write this function as a member function to the array-based ADT int Queue::getNumberOfElements() const{ return count; (b) Write this function as a member function to the pointer-based ADT int Queue::getNumberOfElements() const{ int i=0; for (QueueNode* tempptr = frontptr; tempptr!= NULL; tempptr = tempptr->next) ++i; return i; 2. Carrano, 4th edition, Chapter 7, Exercise 10. An operation that displays the contents of a queue can be useful during program debugging. Add a display operation to the ADT queue such that (a) display uses only ADT queue operation, and so it is independent of the queue s implementation. (So write this one in pseudocode.) (The queue whose items are to be displayed is called aqueue.) tempqueue.createqueue(); // dequeue all items from aqueue, print them, and store them in // tempqueue while (!aqueue.isempty()){ aqueue.dequeue(x) cout << x << " " tempqueue.enqueue(x)

cout << endl; // put items back into aqueue while (!tempqueue.isempty()){ tempqueue.dequeue(x) aqueue.enqueue(x) tempqueue.destroyqueue() (b) display assumes and uses the pointer-based implementation of the ADT queue. (So write this one in C++, as another member function of pointer-based implementation of class Queue.) void Queue::display() const{ for (QueueNode* tempptr = frontptr; tempptr!= NULL; tempptr = tempptr->next) cout << tempptr->item << " "; cout << endl; 3. Carrano, 4th edition, Chapter 7, Exercise 12. With the following data, hand-trace the execution of the bank-line simulation that this chapter describes. Each line of data contains an arrival time and a transaction time. Show the state of the queue and the event list at each step. 5 9 7 5 14 5 30 5 32 5 34 5 Note that at time 14 there is a tie between the execution of an arrival event and a.

Time Action bankqueue aneventlist 0 Read file, place event in aneventlist (empty) A-5-9 5 Update aneventlist and bankqueue: 5-9 (empty) Customer 1 enters bank Customer 1 begins transaction, create 5-9 D-14 Read file, place event in aneventlist 5-9 A-7-2 D-14 7 Update aneventlist and bankqueue: 5-9 7-5 D-14 Customer 2 enters bank Read file, place event in aneventlist 5-9 7-5 D-14 A-14-5 14 Update aneventlist and bankqueue: 7-5 A-14-5 Customer 1 departs Customer 2 begins transaction, create 7-5 A-14-5 D-19 Update aneventlist and bankqueue: 7-5 14-5 D-19 Customer 3 enters bank Read file, place event in aneventlist 7-5 14-5 D-19 A-30-5 19 Update aneventlist and bankqueue: 14-5 A-30-5 Customer 2 departs Customer 3 begins transaction, create 14-5 D-24 A-30-5 24 Update aneventlist and bankqueue: (empty) A-30-5 Customer 3 departs 30 Update aneventlist and bankqueue: 30-5 (empty) Customer 4 enters bank Customer 4 begins transaction, create 30-5 D-35 Read file, place event in aneventlist 30-5 A-32-5 D-35 32 Update aneventlist and bankqueue: 30-5 32-5 D-35 Customer 5 enters bank Read file, place event in aneventlist 30-5 32-5 A-34-5 D-35 34 Update aneventlist and bankqueue: 30-5 32-5 34-5 D-35 Customer 6 enters bank Read file, no more customers 30-5 32-5 34-5 D-35 35 Update aneventlist and bankqueue: 32-5 34-5 (empty) Customer 4 departs Customer 5 begins transaction, create 32-5 34-5 D-40 40 Update aneventlist and bankqueue: 34-5 (empty) Customer 5 departs Customer 6 begins transaction, create 34-5 D-45 45 Update aneventlist and bankqueue: Customer 6 departs (empty) (empty)

4. Carrano, 4th edition, Chapter 8, Exercise 11. The section Class Templates describes a class template for List. Using this template, write C++ statements that define an ADT list of integers, and prompt the user to enter those integers into the list. List<int> alist; for (int i=1; i<=5; ++i){ int x; cout << "Enter an integer: "; cin >> x; alist.insert(i,x); 5. Carrano, 4th edition, Chapter 8, (part of) Exercise 12. Overload the assignment operator = for the pointer-based implementation of the class Stack. (Hint: Study the copy constructors.) Note this solution does not contain any error handling in the case of a new failure. Stack& Stack::operator= (const Stack& rhs){ // remove contents of lhs stack while (!isempty()) pop(); // this is the code from the copy constructor if (rhs.topptr == NULL) topptr = NULL; else{ // copy first node topptr = new StackNode; topptr->item = rhs.topptr->item; // copy rest of stack StackNode* newptr = topptr; // new stack pointer for (StackNode* origptr = rhs.topptr->next; origptr!= NULL; origptr = origptr->next){ newptr->next = new StackNode; newptr = newptr->next; newptr->item = origptr->item; // end for newptr->next = NULL; // end else return *this; // end operator= function