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

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

ADTs: Stacks and Queues

Introduction to the C programming language

Stacks and Their Applications

Introduction to the C programming language

Programming Abstractions

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

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

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

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

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

CS 216 Exam 1 Fall SOLUTION

Data Structures (INE2011)

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

Overview of C++: Part 1

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

CSI33 Data Structures

CS302 Data Structures using C++

Data Structures using OOP C++ Lecture 9

This examination has 11 pages. Check that you have a complete paper.

Example Final Questions Instructions

CSCE 110 PROGRAMMING FUNDAMENTALS

COMP 2355 Introduction to Systems Programming

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

Datastructuren André Deutz

COSC 320 Exam 2 Key Spring Part 1: Hash Functions

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

CSCE 2014 Final Exam Spring Version A

CSC 222: Computer Programming II. Spring 2004

Chapter 18: Stacks And Queues

Name: 1 stack<int> s; 2 s.push(9) 3 s.push(5) 4 s.push(10) 5 s.push(1) 6 s.pop() 7 s.pop() 8 s.push(0) 9 s.pop() 10 s.pop() 11 s.

September 19,

Chapter 7: Stacks. Exercises 7.2

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

Chapter 11. Abstract Data Types and Encapsulation Concepts

CHAPTER 3 STACKS AND QUEUES. Iris Hui-Ru Jiang Fall 2008

Lecture Data Structure Stack

Algorithm and Data Structures, Supplemental Solution 2010

ADTs Stack and Queue. Outline

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Queue Implementations

Data Structures G5029

STACKS AND QUEUES. Problem Solving with Computers-II

Stacks. 1 Introduction. 2 The Stack ADT. Prof. Stewart Weiss. CSci 235 Software Design and Analysis II Stacks

C++ Exception Handling 1

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)

CSCI 102L - Data Structures Midterm Exam #2 Spring 2011

CMSC 341 Lecture 6 STL, Stacks, & Queues. Based on slides by Lupoli, Dixon & Gibson at UMBC

CS 216 Fall 2007 Midterm 1 Page 1 of 10 Name: ID:

Example Final Questions Instructions

Chapter 18: Stacks And Queues

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

September 10,

CLOSED-BOOK-CLOSED-NOTES

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

CPSC 427: Object-Oriented Programming

Two abstract data types. performed on them) Stacks Last In First Out (LIFO) Queues First In First Out (FIFO)

Chapter 3. Stack & Queue 1

== isn t always equal?

Linked List using a Sentinel

ECE 242 Fall 13 Exam I Profs. Wolf and Tessier

CS197c: Programming in C++

Chapter 5. ADTs Stack and Queue

Fundamentals of Programming. Lecture 19 Hamed Rasifard

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

CMSC 341. Deques, Stacks and Queues 9/22/04 1

Abstract Data Types 1

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

CMSC 341 Lecture 7. Announcements. Proj 2 up Project Preview tonight and tomorrow

CSE030 Fall 2012 Final Exam Friday, December 14, PM

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

Data Structures And Algorithms

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking

What can we do with Coin dispenser?

Networked Embedded System Patterns for C Developers. Overview of C (& C++) Programming Styles. Douglas C. Schmidt

Part I: Short Answer (12 questions, 65 points total)

Due Date: See Blackboard

Object Oriented Programming COP3330 / CGS5409

Abstract Data Types. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

1 Short Answer (7 Points Each)

More Data Structures (Part 1) Stacks

Abstract Data Types 1

Chapter 12: Searching: Binary Trees and Hash Tables. Exercises 12.1

UNIT-2 Stack & Queue

CSCE 110 PROGRAMMING FUNDAMENTALS

G Programming Languages - Fall 2012

CMSC 4023 Chapter 11

CS24 Week 4 Lecture 2

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:30-7:00

Largest Online Community of VU Students

1.00 Lecture 26. Data Structures: Introduction Stacks. Reading for next time: Big Java: Data Structures

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

/** * finds the index of v in a if present * * * * */ public static int findinsortedarray(int[] a, int v) {...

CS Data Structure Spring Answer Key- Assignment #4

Discrete Structures Lecture 11

EECE.3220: Data Structures Spring 2017

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

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

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

Transcription:

More Group HW The following code is contained in the file ex1stck.h. Fill in the blanks with the C++ statement(s) that will correctly finish the method. Each blank may be filled in with more than one statement. #ifndef Stackh #define Stackh #include <cstdlib> using namespace std; class Stack public: Stack (int MaxStackSize = 10); ~Stack() delete [] stack; bool IsEmpty() const return top == -1; bool IsFull() const return top == MaxTop; int Top() const; void push(int x); void pop(); private: int top; // current top of stack int MaxTop; // max value for top int *stack; ; Stack::Stack (int MaxStackSize) MaxTop = MaxStackSize - 1; stack = new int[maxstacksize]; top = -1; int Stack::Top( ) const if ( ) cerr << "Error"; exit(1); return ; void Stack::push(int x) if ( ) cerr << "error"; exit(1);

void Stack::pop() if ( ) cerr << "Error"; exit(1); ; #endif b) (5 points) What is printed by the following program segment given the stack header file ex1stck.h above. Stack s(5); int x; s.push(12); s.push(6); s.push(10); while (!s.isempty()) x = s.top(); cout << x << ' '; s.pop(); c) (5 points) Use the above stack class definition to write the code that would set an integer x to the second element from the top of the stack. Print an appropriate error statement if this is not possible (i.e. the stack has less than two elements). 9. (12 points) Fill in the blank a) LIFO stands for. b) In a linked stack, the top node points to.

What is printed by the following C++ program? #include <iostream> #include <stdexcept> #include "stack2.h" //array implementation of a stack using namespace std; int main () Stack<int> A(5); int i,x; try for (i = 0; i < 6 ;i++) A.push(i + 5); for (i = 0; i < 6; i++) x = A.Top(); A.pop(); cout << x << ' '; catch (logic_error le) cout << le.what() << endl; catch (...)cout << "An exception has occurred" << endl; return(0); /*Given the following header and methods for push and pop template <class StackItemType> class Stack public: Stack(int maxstack); bool isempty() const; void push(const StackItemType& newitem) throw(stackexception); void pop() throw(stackexception); void pop(stackitemtype& stacktop) throw(stackexception); void gettop(stackitemtype& stacktop) const throw(stackexception); private: /** Array of stack items */ StackItemType *items; int top; int MAX_STACK; ;

template <class StackItemType> void Stack<StackItemType>::push(const StackItemType& newitem) // if stack has no more room for another item if (top >= MAX_STACK-1) throw logic_error("stackexception: stack full on push"); else ++top; items[top] = newitem; // end if // end push template <class StackItemType> void Stack<StackItemType>::pop() throw(stackexception) if (isempty()) throw logic_error("stackexception: stack empty on pop"); else --top; // stack is not empty; pop top // end pop */ 1. Given two numbers, n and k, the binomial coefficient for these two numbers are equal to the number of combinations of n choices given k slots. The following code calculates binomial coefficients recursively. Show how the operating system stack changes when the recursive function binoc executes. The first call to binoc is done for you. n = 3 k = 2 p = q = result = #include <iostream> using namespace std; int binoc (int, int); int main() int n, k, result = 0; n = 3; k = 2; result = binoc (n,k); cout << "result = " << result << endl; return 0;

int binoc (int n, int k) int p, q, result; if (k == 0) return 1; if (k == n) return 1; p = binoc(n - 1, k - 1); q = binoc (n - 1, k); result = p + q; return result; 2. The power function can be written recursively as follows: pow (n, 1) returns n pow (n, 0) returns 1 pow (n, k) = n * pow (n, k-1) Write the code that implements recursive function pow. You are given the following queue, where each node class contains an integer variable called d and a pointer variable called ptr. Complete the code for the delete function which deletes the first node of a queue and places the value from the data portion of node into x. front ptr ptr ptr void Lqueue::Delete(int& x) if ( ) cout << "error"; exit(1); x = ; rear Node *p = ; front = ; delete ;

Assume full or empty situation will print some type of error message and will not exit the program. The function enque, adds an element to the queue, dequeue deletes an element form the queue, and print will print the entire queue starting at the front. Draw pictures to illustrate each step. front O.enqueue (z); Q.enqueue (f); Q.enqueue (g); Q.dequeue (x); Q.dequeue (x); Q.enqueue (m); Q.print(); More to do... Carrano pg 495-496 8,9,10,16,18 0 7 a 1 2 3 4 6 5 d b c rear See next two pages for optional questions Do in Malik, Edition 6-7 a, b, c 8 a, b, c 9 b, c 15 16

Optional GHW tree questions of the type that will be on the exam (we will go over these in class) ) Base your answers to each of the questions below on the following binary tree. A B C D E F G a) List all the leaves in the tree. b) List the nodes which are the children of node B c) List an ancestor of node C. d) What is the height or depth of the tree?

Consider the following binary trees: Tree A Tree B Tree C Tree D a) Which tree(s) are strictly binary? b) Which tree(s) are complete? c) Which tree(s) are full?