Homework #2

advertisement
Data Structures and Algorithms I
Spring 2014
Homework #2
(1) For each of the following descriptions of something stored in memory when a program that
was coded in C++ is running, state whether the memory is part of static memory, an
activation record in the stack, or the heap (i.e., dynamic memory). For those entities that are
stored in an activation record, state whether the memory resides in the first, second, or third
section of the activation record, according to the breakdown discussed in class. For some of
these parts, you will have to speculate about the most likely way that a class is implemented.
(a)
(b)
(c)
(d)
(e)
A char that is a global variable
A double that is a local variable of a regular function
A double that is a private data member of a global object
A int that is a parameter of a static private member function of a class
An int that is a public data member of an object that is a local variable of a public member
function of a class
(f) A bool that is a parameter of a regular function
(g) A character that is stored in a C++ string object if the object is a local variable of a public
member function of a class
(h) A character that is stored in a C++ string object if the object is a global variable
(i) A character stored in an old C-style string if the string is stored in a standard fixed-size array
of characters declared as a local variable of a protected member function of a class
(j) A characters stored in an old C-style string if the string is pointed to by a pointer to char and
the pointer is declared as a global variable and it points to memory that is stored in a standard
fixed-sized array of characters declared as a local variable of a regular function
(k) An int that is part of a C++ vector of integers if the vector is a local variable of a constructor
of a class
(l) The memory address stored by a pointer to int (i.e., the value of the pointer, not the value of
the thing that it points to) if the pointer is a global variable and it points to a single
dynamically allocated int
(m) The memory address stored by a pointer to double (i.e., the value of the pointer, not the value
of the thing that it points to) if the pointer is a parameter of a public member function of a
class and it points to a double in a fixed-size global array of doubles
(n) The previous value of a register that has been backed up before the currently active regular
function was called (which needs to be restored when the currently active function returns)
(o) The value of an int that is a static protected data member of a class
(p) A bool that is part of a dynamically allocated array of Boolean values if the dynamically
allocated array is pointed to by a pointer that is a public data member of an object that is a
parameter of a private member function of a class
(q) An int that is a private data member of an object if the object is part of a dynamically
allocated array of objects and the dynamically allocated array of objects is pointed to by a
pointer that is a parameter of a protected member function of a class
(2) Answer the following questions concerning lists, stacks, and queues:
(a) For each of the following applications, explain whether a stack or a queue makes more sense
as a data structure. Briefly explain your answers.
I.
II.
III.
IV.
V.
A data structure to store jobs sent to a printer.
A data structure being used by a calculator application to computer the value of
expressions including numbers, multiplication, division, addition, subtraction, and
parentheses (accounting for typical notions of precedence).
A data structure used by a non-recursive function that displays the items of a singlylinked list in reverse order.
A data structure used by a web-server that replies to concurrent requests for static
web pages.
A data structure used by an operating system to store characters in an output buffer
that will be written to a text file.
(b) You are going to consider two implementations of a list of lists of integers. The first
implementation relies on a singly-linked list of singly-linked lists of integers (where each
linked list stores pointers to the first and last nodes within the linked list). The second
implementation relies on a dynamically allocated array of pointers where each pointer points
to a dynamically allocated array of integers. Assume that both implementations have been
coded well. Assume also that K lists of up to N integers each have already been created.
Using big-Theta notation, specify the worst case running time for each of the following
operations for each of the two implementations in terms of K, N, i, and j (when appropriate):
I.
II.
III.
IV.
V.
VI.
A lookup of the ith integer in the jth list, assuming that this element exists.
An insertion of an item after a specific element in one of the lists, assuming that a
pointer to the element after which the new item is to be inserted is provided.
An insertion of an item at the start of the jth list, assuming that the list exists, and
assuming that only a single pointer to the start of the entire data structure is provided.
A deletion of an item at the end of the jth list, assuming that this list exists, and
assuming that only a single pointer to the start of the entire data structure is provided.
An insertion of a new list, initialized with one provided integer, at the start of the
entire data structure, assuming that only a single pointer to the start of the entire data
structure is provided.
A deletion of the jth list (including freeing the memory associated with it), assuming
that only a single pointer to the start of the entire data structure is provided.
Download