DATA STRUCTURES 1st exam solution. Question 1(1/3) Suppose we have the ADT Bag which has the following operations: bool isEmpty(): tests whether Bag is.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Stacks Chapter 11.
Lec 7 Sept 17 Finish discussion of stack infix to postfix conversion Queue queue ADT implementation of insert, delete etc. an application of queue.
Stacks Example: Stack of plates in cafeteria.
Lecture 5 Stack Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
Lecture 5 Sept 15 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
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.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Announcements.
© 2006 Pearson Addison-Wesley. All rights reserved6-1 Chapter 6 Recursion as a Problem- Solving Technique.
Stacks Chapter Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,
Lecture 11 Sept 26, 2011 Goals convert from infix to postfix.
Chapter 3 Array-Based Implementations CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
1 Applications of Recursion (Walls & Mirrors - Chapter 5)
Stack Data Structure By : Imam M Shofi. What is stack? A stack is a limited version of an array. A stack is a limited version of an array. New elements,
Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO.
Chapter 6 Stacks CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
Stacks 1. Stack  What is a stack? An ordered list where insertions and deletions occur at one end called the top. Also known as last-in-first-out (LIFO)
Data Structures -1 st test- March 30, 2015 授課教授:李錫智.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
EXAMPLES OF RECURSION Towers of Hanoi Writing Linked Lists Backwards Recursive Insert 8 Queens Recognizing Simple Languages Prefix Expressions Conversion.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Data Structures & Algorithms
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
1 CSC 222: Computer Programming II Spring 2004 Stacks and recursion  stack ADT  push, pop, top, empty, size  vector-based implementation, library 
Array-Based Implementations Chapter 3 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
11/07/141 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: –Data stored –Operations on the.
Sorted Lists Chapter Chapter Contents Specifications for the ADT Sorted List Using the ADT Sorted List A Linked Implementation The Method add The.
Data Structures -1 st exam- 授課教師 : 李錫智 教授. 1.[10] Suppose we have the ADT Bag which has the following operations: bool isEmpty(): tests whether Bag is.
Computer Science Department Data Structure and Algorithms Lecture 3 Stacks.
Lists and Sorted Lists: various implementations Slides by Nadia Al-Ghreimil adopted from Steve Armstrong LeTourneau University Longview, TX  2007, 
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
1 CS162: Introduction to Computer Science II Abstract Data Types.
Data Abstraction: The Walls
Chapter 4 Stacks
Computing with C# and the .NET Framework
Stacks Stacks.
Doubly Linked List Review - We are writing this code
Stacks.
Stacks and Queues.
Stacks.
Stack and Queue APURBO DATTA.
Stacks.
Algorithms and Data Structures
Stack.
CMSC 341 Lecture 5 Stacks, Queues
A Bag Implementation that Links Data
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Introduction to C++ Programming
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Popping Items Off a Stack Lesson xx
Adapted from Pearson Education, Inc.
Queues: Implemented using Arrays
Copyright ©2012 by Pearson Education, Inc. All rights reserved
CSC 143 Stacks [Chapter 6].
Stacks and Queues 1.
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Array-Based Implementations
Stacks.
Stacks CS-240 Dick Steflik.
Stack.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Recitation 2 CS0445 Data Structures
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Lecture 20 – Practice Exercises 4
Presentation transcript:

DATA STRUCTURES 1st exam solution

Question 1(1/3) Suppose we have the ADT Bag which has the following operations: bool isEmpty(): tests whether Bag is empty. bool add(newEntry): adds the object newEntry into Bag. bool remove(anEntry): remove the first occurrence of anEntry from Bag. bool isFull(): tests whether Bag is full. bool contains(anEntry): tests whether Bag contains the object anEntry. void clear(): removes all objects from Bag. Note that add(newEntry) just stores without checking duplication. That is, no checking on duplication is done for this operation. Also, remove(anEntry) removes only the first occurrence of anEntry from Bag.

Question 1(2/3) A) Suppose we want to create another operation, add-U(newEntry), which won’t allow duplicates to be stored. That is, at most one occurrence of an entry is stored in Bag. Please describe how to apply the functions isEmpty, add, remove, isFull, contains, or clear to implement add-U. ANS:

Question 1(3/3) B) Suppose we want to create another operation, remove-A(anEntry), which will remove all the occurrences of anEntry from Bag. Please describe how to apply the functions isEmpty, add, remove, isFull, contains, or clear to implement remove-A. ANS:

Question 2 (1/4) Suppose we use an array A of size 8 and an integer variable itemCount, recording the number of objects currently stored in A, to implement the Bag structure. Note that objects are stored in no particular order and objects may be duplicated. The objects we are concerned with are integers. Please answer the following questions:

Question 2 (2/4) A) Suppose we add five integers 10, 5, 5, 15, and 20 successively in the Bag. Please show the content of itemCount and A after each addition. ANS: 1,A[10] ;itemCount=1 2,A[10,5] ;itemCount=2 3,A[10,5,5] ;itemCount=3 4,A[10,5,5,15] ;itemCount=4 5,A[10,5,5,15,20] ;itemCount=5

Question 2 (3/4) B) Suppose we delete the first 5 from the Bag as fast as possible. How do you do it? Please describe. Also, please show the content of itemCount and A after execution. ANS: 從陣列循序搜尋找到第一個 5 後,以最後的 元素覆蓋,之後 itemcount=itemcount-1; A[10,20,5,15];itemCount=4

Question 2 (4/4) C) We want to see whether the Bag is empty. How do you do it? Please describe. ANS: 看 itemCount 的值是否等於 0 D) We want to clear the Bag, i.e., making the Bag empty. How do you do it? Please describe. ANS: 設定 itemCount=0;

Question 3(1/4) Suppose we use a link-based structure and a pointer variable headPtr, pointing to the first object of the chain, to implement the Bag structure. Note that objects are stored in no particular order and objects may be duplicated. We would like to store integers in the Bag. Please answer the following questions:

Question 3 (2/4) A) Suppose we add five integers 10, 5, 5, 15, and 20 successively in the Bag. Note that we do the additions as fast as possible. Please draw the headPtr and the chain after each addition. ANS:

Question 3 (3/4) B) Suppose we delete the first 5 from the Bag. How do you do it? Please describe. Also, please draw the headPtr and the chain after execution. ANS: 將指向第一個 5 的指標,改為指向第一個 5 的 下一個元素

Question 3 (4/4) C) We want to see whether the Bag is empty. How do you do it? Please describe. ANS: 看 headPtr 是否為 NULL D) We want to clear the Bag, i.e., making the Bag empty. How do you do it? Please describe. ANS: 1. 由起始點逐點回收記憶體 2. 設定 headPtr=NULL

Question 4(1/3) Suppose we want to write a function sum(x1, x2), where x1 and x2 are integers and x2 ≧ x1, which returns the sum x1+(x1+1)+(x1+2)+…+(x2-1)+x2. Please answer the following questions:

Question 4(2/3) A) Please describe how to implement sum(x1, x2) iteratively.

Question 4(3/3) B) Please describe how to implement sum(x1,x2) recursively.

Question 5 (1/4) Suppose we have the following function: void showOff(s:string) { cout << “Enter showOff with string: “ << s << endl; if(s is not an empty string) { showOff(s minus its first character); cout << “About to show the first character of string: “ << s << endl; } cout << “Leave showOff with string: “ << s << endl; }

Question 5 (2/4) Note that cout will print out the subsequent strings or variables onto the sreen., and endl is the end-of-line indicator. For example, if s stores “NSYSU”, then cout << “Leave showOff with string: “ << s << endl;” will print out “Leave showOff with string: NSYSU” on the screen and the cursor will be positioned at the beginning of the next line. Now we call showOff(“Hello”). Please answer the following questions:

Question 5 (3/4) A) How many calls to showOff will have been performed before showOff(“Hello”) finishes? ANS: 6 次

Question 5 (4/4) B.) What are the lines of print-out on the screen produced by showOff(“Hello”) eventually? ANS:

Question 6 (1/3) Consider the following function: Integer tryTry(n: non-negative integer){ If(n==0 or n==1) return 1; else return tryTry(n-2)+tryTry(n-1)+2; } Now we call tryTry(5). Please answer the following questions:

Question 6 (2/3) A) How many calls to tryTry will have been performedbefore tryTry(5) finishes? ANS: 15 次 B) How many calls to tryTry(2) will have been performed before tryTry(5) finishes? ANS: 3 次

Question 6 (3/3) C) What is the returned value of tryTry(5)? ANS: 22 D) How many additions have been performed before tryTry(5) finishes? ANS: 14 次

Question 7(1/1) Consider the language: = | | = X = Y A) Is the string XXXXYY in this language? Prove it by showing how it is derived. ANS: 此單詞不存在此語言,因無法以 Y 結尾 B) Write all five-character strings that contain more Y’s than X’s in this language. ANS: YYYYX 、 YYYXX

Question 8(1/2) Consider the language: = | a a | b b | c c | … | y y | z z = a | b | c | d |….| x | y | z. Let c(n) be the number of such strings of length n.

Solution of Q8(2/2)

Question 9(1/5) Please answer the following questions: A) If we implement the Bag with the array- based structure without the itemCount variable, can it work? Please explain clearly, otherwise you won’t get any points for your answer. ANS: No ,無法得知矩陣是否還有空間或是 否有資料。

Question 9(2/5) B) What advantages can we have if implementing the Bag with the array-based structure? Please explain clearly, otherwise you won’t get any points for your answer. ANS: 1. 節點儲存於連續記憶體,可以快速訪問節點。

Question 9(3/5) C) What disadvantages can we have if implementing the Bag with the array-based structure? Please explain clearly, otherwise you won’t get any points for your answer. ANS: 1. 儲存空間固定,無法彈性變動

Question 9(4/5) D) What disadvantages can we have if implementing the Bag with the link-based structure? Please explain clearly, otherwise you won’t get any points for your answer. ANS: 1. 節點只能循序走訪,較花費時間。 2. 需多花費記憶體空間儲存指標。

Question 9(5/5) E) If we implement the Bag with the link-based structure with the itemCount variable, what advantages can we get? Please explain clearly, otherwise you won’t get any points for your answer. ANS: 可以快速得知資料總節點數。

Question 10(1/5) Please answer the following questions: A) Given an ordinary algebraic expression (x+y)*(z- w)/(y-z)+x, please show the corresponding postfix expression for it. Note that w, x, y, and z are operands. ANS: xy+zw-*yz-/x+

Question 10(2/5) B) Given an ordinary algebraic expression (x+y)*(z- w)/(y-z)+x, please show the corresponding prefix expression for it. Note that w, x, y, and z are operands. ANS: +/*+xy-zw-yzx

Question 10(3/5) C. Given an expression xy+zw*-z+, where w, x, y, and z are operands, please prove whether or not it is a legal postfix expression by showing how it is derived from the following rules for : = | = a|b|c|…|x|y|z = +|-|*|/

Question 10(4/5) ANS: 1. = = x = = y 2. = =xy+ 3. = = z = = w 4. = =zw* 5. = =(xy+)(zw*)- 6. = =(xy+zw*-)z+

Question 10(5/5) D) Please convert the postfix expression, xyz-w/+y*, where w, x, y, and z are operands, to an ordinary algebraic expression. ANS: (x+(y-z)/w)*y E) Please convert the prefix expression, /*+xy-zwx, where w, x, y, and z are operands, to an ordinary algebraic expression. ANS: (x+y)*(z-w)/x