- Suppose you have a program that uses the following files:
- The source files you wrote are in maze.c,
maze-helper.c, and list.c.
- The include files are maze.h, which is found in
a directory named /home/bvz/maze/include, and fields.h,
which is found in /home/bvz/libraries/include
- The object file for fields.h can be found in
/home/bvz/libraries/objs
Write the command that compiles this program into an executable named
maze and makes it suitable for debugging with gdb.
- Write a short program that simulates pushing and popping functions from
a stack. Your input consists of lines
from standard input that begin with either
the keyword "call" or "return". If a line starts with "call" it
also has a second field which is the name of a function. If a line
starts with return it has an optional second field which is the name
of a function. You are to write a program that uses the fields library
to read these lines. Your program perform the following actions:
- If a line starts with the keyword "call" then
you should push the function name onto a stack.
- If a line starts with the keyword "return" but does not have
a second field you should pop the most recent function name
from the stack.
- If a line starts with the keyword "return" and has a second
field then you should pop the stack until the top of the
stack has the indicated function name.
The following example shows some sample input and what the stack
would look like after each line is processed. Of course the function
names would be pointed to by character strings and the actual values
on the stack would be Jvals that pointed to the character strings.
call a // stack = a
call b // stack = b a
call a // stack = a b a (a was pushed onto the front)
return // stack = b a (b was popped)
call c // stack = c b a
call d // stack = d c b a
call a // stack = a d c b a
return c // stack = c b a (a and d got popped)
Do not worry about printing anything, checking for error conditions,
or compiling/executing your code.
Assume that you are using a pre-defined stack library that has
the following interface:
- void push(void *value, void *stack): push the given value onto stack
- void *pop(void *stack): pop the top element off the stack and return
its value. Remember to delete the node holding the top element.
If the stack is empty return 0.
- void *top(void *stack): return the value of the top element on the stack
but do not pop it.
- void *create_stack(): create a record for a stack and return it as a
void *.
- bool is_empty(void *stack): return true if the stack is empty and false
otherwise.
-
a. array
b. singly-linked list
c. doubly-linked list
d. queue
e. stack
For each of the following questions choose the best answer from the
above list. Sometimes it may seem as though two or more choices would
be equally good. In those cases think about the operations that the
data structures support and choose the data structure whose operations
are best suited for the problem.
You may have to use the same answer for more than one
question:
- ____________ The most space-efficient data structure you could use
to implement a
stack in which there is an upper limit, max,
on the number of elements that can be stored on the
stack.
- ____________ The most space-efficient data structure you could use
to implement a
stack in which the the number of elements that can be
stored on the stack is unlimited.
- ____________ The data structure which allows any of its elements to
be accessed in constant time.
- ____________ The data structure you would use if you needed to be
able to insert an element anywhere in the data
structure.
- ____________ The data structure you would use to ensure that the
last item added
to the data structure is always the first item removed
from the data structure.
- ____________ The data structure that allows you to traverse its
elements in either forward or reverse order.
- ____________ The easiest data structure you could use to implement
a queue with an unbounded number of elements.
- ____________ The data structure you would use to ensure that items
are always added to the end of the data structure and
removed from the front of the data structure.
- Question 2.6.c in Weiss. Only answer the first 4 questions and compute
both T(n) and O(n). For question 2.6.c.4 choose the most pessimistic
value of i for the inner loop. Remember that in computing T(N) we
want the most pessimistic estimate, not the best or average estimate
of running time.
-
Programs A and B are analyzed and found to have worst-case running times
no greater than 150N log2N and N2, respectively.
Answer the following questions (Note:
you should not need a calculator to answer any of the following
questions):
- Which program has the better guarantee on the running time, for
large values of N (N > 10,000)?
- Which program has the better guarantee on the running time, for small
values of N (N < 100)?
- Which program will run faster on average for N = 1,000?
- If you did not know the size of the input, which program would
you prefer to use? Why?
- Behold the following function:
int max_sum(int *array, int size, int *save_i, int *save_j) {
int max = 0;
int i, j;
for (i = 0; i < size-1; i++) {
for (j = i+1; j < size; j++) {
if (array[j] > array[i]) {
if ((array[j] + array[i]) > max) {
max = array[j] + array[i];
*save_i = i;
*save_j = j;
}
}
}
}
return max;
}
- What is T(size) for the inner-most loop? You are allowed to use
i in your mathematical expression for T(n). Note that size
represents the size of the input for max_sum. Hint: when trying
to figure out how many times the inner loop will iterate, choose
the most pessimistic value of i possible. Remember that we are
looking for the worst case, not the best case or even the average
case.
- What is T(size) for max_sum as a whole?
- What is the big-O running time of max_sum?
- Suppose main has the following declarations:
#define SIZE 10
int a[SIZE];
int max;
int index1;
int index2;
Write a line of code that will call max_sum and save the return
result in max, the value of save_i in index1 and
the value of save_j in index2.
- What is Big-O of each of the following functions:
- f(n) = 10n + 3n log n + 6
- f(n) = 6n3 - 4n2 - 3n - 1000
- f(n) = 23
- f(n) = .000005n2 + 5000000n - 20