CMPS 12B Introduction to Data Structures Midterm 2 Review Problems

Similar documents
DATA STRUCTURE - STACK

Stacks. Linear data structures

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

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

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

10CS35: Data Structures Using C

7.1 Our Current Model

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

Stack Allocation. Run-Time Data Structures. Static Structures

Chapter 13 Storage classes

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

DATA STRUCTURES USING C

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Object Oriented Software Design II

PROGRAMMING CONCEPTS AND EMBEDDED PROGRAMMING IN C, C++ and JAVA: Lesson-4: Data Structures: Stacks

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

PES Institute of Technology-BSC QUESTION BANK

Common Data Structures

Analysis of a Search Algorithm

The C Programming Language course syllabus associate level

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Data Structures. Level 6 C Module Descriptor

Introduction to Object-Oriented Programming

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

Habanero Extreme Scale Software Research Project

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:

Linked Lists Linked Lists, Queues, and Stacks

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

Functions Recursion. C++ functions. Declare/prototype. Define. Call. int myfunction (int ); int myfunction (int x){ int y = x*x; return y; }

16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

CmpSci 187: Programming with Data Structures Spring 2015

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Sample Questions Csci 1112 A. Bellaachia

Memory Allocation. Static Allocation. Dynamic Allocation. Memory Management. Dynamic Allocation. Dynamic Storage Allocation

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

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Introduction to Data Structures

C Compiler Targeting the Java Virtual Machine

Data Structures and Algorithms V Otávio Braga

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

1 Abstract Data Types Information Hiding

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

C++ Overloading, Constructors, Assignment operator

CSE 8B Midterm Fall 2015

Lecture 12 Doubly Linked Lists (with Recursion)

Unit Write iterative and recursive C functions to find the greatest common divisor of two integers. [6]

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

Master of Sciences in Informatics Engineering Programming Paradigms 2005/2006. Final Examination. January 24 th, 2006

Introduction to Programming

1. Relational database accesses data in a sequential form. (Figures 7.1, 7.2)

Lecture 11: Tail Recursion; Continuations

Recursion. Slides. Programming in C++ Computer Science Dept Va Tech Aug., Barnette ND, McQuain WD

Bypassing Browser Memory Protections in Windows Vista

Simple C Programs. Goals for this Lecture. Help you learn about:

Module 2 Stacks and Queues: Abstract Data Types

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!

Lecture 11 Array of Linked Lists

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

Chapter 3: Restricted Structures Page 1

C Interview Questions

Basics of Java Programming Input and the Scanner class

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

Recursion. Definition: o A procedure or function that calls itself, directly or indirectly, is said to be recursive.

STACKS,QUEUES, AND LINKED LISTS

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Algorithms and Data Structures Written Exam Proposed SOLUTION

FEEG Applied Programming 5 - Tutorial Session

CS 111 Classes I 1. Software Organization View to this point:

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

Dalhousie University CSCI 2132 Software Development Winter 2015 Lab 7, March 11

CS 2412 Data Structures. Chapter 2 Stacks and recursion

arrays C Programming Language - Arrays

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Data Structures and Data Manipulation

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

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

ADTs,, Arrays, Linked Lists

Graduate Assessment Test (Sample)

Chapter 5. Recursion. Data Structures and Algorithms in Java

Data Structures. Algorithm Performance and Big O Analysis

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

CMSC 202H. ArrayList, Multidimensional Arrays

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

CompSci-61B, Data Structures Final Exam

Abstract Data Types. Chapter 2

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

C++ INTERVIEW QUESTIONS

Object Oriented Software Design II

Computer Programming I

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Elemental functions: Writing data-parallel code in C/C++ using Intel Cilk Plus

Introduction to Stacks

Transcription:

CMPS 12B Introduction to Data Structures Midterm 2 Review Problems 1. Write a recursive Java function called product(), which given a head reference to a linked list based on the Node class defined below, returns the product of the items in the list. The product of an empty list is defined to be one. class Node{ int item; Node next; Node(int x){ item = x; next = null; // In some class in the same package as Node: static int product(node H){ // Your code goes here

2. Write functions push() and pop() for the Java implementation of an integer stack outlined below. The stack is implemented as a singly linked list with a top Node reference. Function push() inserts a new item onto the stack by inserting a new Node at the head of the list. class Stack{ private class Node{ int item; Node next; Node(int item){ this.item = item; this.next = null; private Node top; private int numitems; public Stack(){top = null; numitems = 0; void push(int x){ int pop(){ // other Stack methods would follow

3. Trace the following C program and place the output on the lines below exactly as it would appear on the screen. #include<stdio.h> #include<stdlib.h> int f(int x, int y){ int u; u = x*y; printf("in f\n"); return( x+u+y ); int g(int* p, int* q){ int v; v = *p + *q; printf("in g, before f\n"); *q = f(v, *p); printf("in g, after f\n"); return( v-*q ); int main(void){ int a=1, b=2, c=3; printf("in main, before f and g\n"); a = f(a, b); b = g(&b, &c); printf("in main, after f and g\n"); printf("a = %d, b = %d, c = %d\n", a, b, c); return(exit_success);

4. The following C program includes a global variable called time. Since it is declared outside of all functions (on line 3), its scope is the entire file. Notice that time is incremented before each of the functions a, b, and c return. Show the state of the function call stack when time=4 (i.e. at the instant time becomes equal to 4). Each stack frame should show the values of all function arguments and local variables, as well as the line to which execution will transfer when the function returns. If a local variable has not yet been assigned a value at time=4, indicate that fact by stating its value as undef. Also determine the program output, and print it on the line below exactly as it would appear on the screen. 1.) #include<stdio.h> 2.) #include<stdlib.h> 3.) int time; 4.) int a(int x){ 5.) int i; 6.) i = x*x; 7.) time++; 8.) return(i); 9.) 10.) int b(int y){ 11.) int j; 12.) j = y+a(y); 13.) time++; 14.) return(j); 15.) 17.) int c(int z){ 18.) int k; 19.) k = a(z)+b(z); 20.) time++; 21.) return(k); 22.) 23.) int main(void){ 24.) int q, r; 25.) time = 0; 26.) q = b(5); 27.) r = c(2); 28.) printf("q=%d, r=%d, time=%d\n", q, r, time); 29.) return(exit_success); 30.) Output: State of the function call stack when time=4:

5. Consider the following C program. #include<stdio.h> #include<stdlib.h> int main(void){ int i, j; double x = 4.2, y; double * A = calloc(4, sizeof(double)); double B[] = {1.2, 5.3, 2.1, 3.4; double *p, *q; p = malloc(sizeof(double)); y = x+2; q = &y; *p = *q + 2.5; for(i=0; i<4; i++){ j = 3-i; *(A+i) = B[j] + i; printf("%f, %f, %f, %f\n", *A, *B, *p, *q); A = B; printf("%f, %f, %f, %f\n", *A, *(A+1), *(A+2), *(A+3) ); return(exit_success); a. Write the output of this program exactly as it would appear on the screen: b. List the pointer variables in this program, and for each one, state whether it points to stack memory or heap memory. If at some point in the program the pointer changes from stack to heap or heap to stack, note the point in the program where that happens. c. Does this program contain any memory leaks? If so, what alteration(s) would be needed to eliminate those leaks?

6. Write a C function called search() with the protype below that takes as input a null terminated char array S (i.e. a string) and a single char c, and returns the leftmost index in S at which the target c appears, or returns -1 if no such index exists. int search(char* S, char c){ 7. Write a C function called diff() with the prototype below that takes as input two null terminated char arrays (i.e. strings) A and B and returns the difference string consisting of all chars in A that are not in B. The returned chars should be stored in a null terminated char array in the same order they appeared in A, possibly with repetitions. The returned array should be allocated from heap memory to be the same length as A (although the null terminator may not appear at the end of this array.) You may use the strlen() function found in the library string.h to determine this length. You may also assume the existence of a C function called search() as described in problem 6. char* diff(char* A, char* B){

8. Consider the C function below called wastetime(). Your goal is to determine how much time wasttime() wastes. The stared (*) lines below are to be considered basic operations, which do nothing but waste a multiple of some unspecified time unit. Determine the total amount T(n) of time wasted on the input n. Find the asymptotic runtime of this algorithm, i.e. T(n) = Θ(some simple function of n). void wastetime(int n){ int i, j, k; * waste 2 units of time; for(i=0; i<n; i++){ * waste 5 units of time; for(j=0; j<n j++){ * waste 12 units of time; for(k=0; k<n; k++){ * waste 3 units of time;