1 / 10

Data Structures CSCI 132, Spring 2019 Lecture 14 Review for Exam 1

Data Structures CSCI 132, Spring 2019 Lecture 14 Review for Exam 1. Problem 1. Write a function that checks if a given word is a palindrome or not.

brownroger
Download Presentation

Data Structures CSCI 132, Spring 2019 Lecture 14 Review for Exam 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data StructuresCSCI 132, Spring 2019Lecture 14Review for Exam 1

  2. Problem 1. Write a function that checks if a given word is a palindrome or not. Hint: A palindrome word is a word that can be read the same way from left to right as from right to left. Implement the function using a proper data structure and explain your reasons for making that decision Example: RADAR can be read the same way from both directions…

  3. Problem 2. 2) Consider the following code: int *y; int *x; x = new int; *x = 15; *x += 2; y = x; *y *= 2; a) Draw a diagram to represent the pointers as each line of this code is executed. b) What are the values of *x and *y after executing this code?

  4. Problem 3. What is the output of the following Code? typedef int Stack_entry; typedef int Queue_entry; int main(void) { Stack pile; Queue lineUp; int number; for (int i = 0; i < 5; i++ ) { pile.push(i * 2); lineUp.append(i + 2); } while (!pile.empty()) { pile.top(number); pile.pop( ); cout << number << " "; } cout << endl; while (!lineUp.empty()) { lineUp.retrieve(number); lineUp.serve(); cout << number << " "; } cout << endl; return 0; }

  5. myList 6 3 Problem 4. Consider the following linked list: Assume a Node is represented by the following struct: struct Node { int myNumber; Node *nextNode; Node(); Node(Node_entry item, Node *add_on = NULL); }; • Write an expression to refer to the integer in the second node of the list. • What is the value of the following expression? myList - > nextNode - > nextNode

  6. Problem 5. Consider the following class declarations: class StudentYear : public Student { public: void SetUp( char name[ ], int idNum, int year); void Write ( ) const; void incrementYear( ); StudentYear ( ); StudentYear (char initName[ ], int initId, int initYear); private: int year; }; class Student { public: void SetUp( char name[ ], int idNum); void Write( ) const; void SetGpa(float gpa); Student ( ); Student (char initName[ ], int initId); private: char name [25]; int id; float gpa; };

  7. Problem 5 (continued) • Which class is the parent class and which is the child class? • Which function is the same for both the Student and the StudentYear class? • Which private data members can be accessed directly by the member functions of each class? • What specific kind of function is StudentYear( )?

  8. Problem 6. Write a function to replace the serve() function of the Queue class with the function, Error_code ServeTwo( ), to delete the first two items in the queue and return an appropriate error code. Assume the Queue representation we developed in class, using a circular array implementation. Note that you are replacing the serve( ) function, so you may not invoke the serve( ) function in your implementation of ServeTwo( ). Recall the Queue specification: const int maxqueue =10 ; //small value for testing class Queue { public: Queue(); bool empty()const; Error_code serve(); Error_code retrieve(Queue_entry &item) const; Error_code append(const Queue_entry &item); private: int count, front, rear; Queue_entry entry [maxqueue]; };

  9. Also Prepare for... In addition to being able to solve the above problems, you should be able to describe, in your own words, the concepts learned in the course. You may use diagrams, examples, pseudo code to support your answer. For example: Describe the Queue Physical Model. Use Diagram to clarify your answer. What is the importance of data hiding? etc.

  10. How to answer Essay Questions Sample question: Explain why data hiding is important for object-oriented programming. How is data hiding accomplished in C++? Give a specific example. Answer each part of the question as thoroughly as you can: • Data hiding is important because it keeps the client code from altering data in a way that may affect the correct operation of class functions. Instead of altering data directly, the client code uses public class functions that act as a user interface for the client code. The class functions make sure variables that depend on one another get altered in a consistent way. • Data hiding is accomplished in C++ by declaring class data variables as private (or protected). This keeps the client code from accessing the data directly. • An example of data hiding is in the Stack class, in which the entry array and the count variables are kept private and are only updated when the client code calls the push( ) or pop( ) functions. The push( ) function adds an item to the entry array and increments count. Conversely the pop( ) function decrements count.

More Related