Data Structures Midterm Name: __________________________
1) Draw a picture of a Doubly Linked List, with nodes holding values of 4, 5, and 7. You should not only have a head and tail, but also a header and a trailer.
2) Draw and describe the steps involved in removing the middle element (5).
3) Deleting from the end of a singly linked list is O(n). Demonstrate the steps in deleting from the end of such a list and explain why it is O(n).
4) Deleting an element from the middle of a singly linked list (the node is passed in) is O(n). Explain, in a sentence or two, why.
5) Deleting from the end of a doubly Linked list, where we have a reference to the tail, is on the order of what? Explain why.
6) Draw a circular array with a couple of elements and a front and rear.
7) How would you implement (in words) removal from the end of the circular array? What data members would you modify? How would you handle boundary conditions?
8) How would you implement removal from the middle of a vector based on a normal array, rather than a circular array? Explain in words.
9) Give one reason it might be better to use an array rather than a singly-linked list to implement a stack.
10) f(n) = 15n^2 + 10n + 1. This is O of what? Give a proof with c and n_0.
12) Write a Java class for a node for a singly-linked list.
13) Write code for a Singly Linked List class, which makes use of that node. Make a head, a tail, and a size. Include the following methods (others may exist, but you need not write them), and make certain to handle the boundary conditions and to check for error conditions:
insertBefore(Node n, int x);
insertAfter(Node n, int x);
remove(int value);
removeSpan(Node start, Node finish);
swap(Node n, Node m);
Thursday, March 8, 2012
Tuesday, March 6, 2012
Thursday, March 1, 2012
class ArrayStack<T> implements Iterable<T>
{
// ...
// various methods we have
Iterator<T> iterator()
{
// will take snapshot approach MyNiftyArrayStackIterator<T> mnasi= new MyNiftyArrayStackIterator<T>(a.clone(), size);
return mnasi;
}
}
class MyNiftyArrayStackIterator<T> implements Iterator<T>
{
int currElem = 0;
MyNiftyArrayStackIterator(T b[], int size) { save to local vars; }
boolean hasNext() { return currElem < size; }
T next() { return b[currElem++]; }
}
Thursday, February 23, 2012
HW: Postfix notation, redux. This time, instead of simply evaluating, we want to build the expression tree.
Homeworks as they are due:
Binary expression tree (part one) -- next Tuesday, Feb 28
Binary expression tree (part two) -- next Thursday, Mar 1
Your stack implements Queue, Mar 6
Array-based Queue, Mar 8
Homeworks as they are due:
Binary expression tree (part one) -- next Tuesday, Feb 28
Binary expression tree (part two) -- next Thursday, Mar 1
Your stack implements Queue, Mar 6
Array-based Queue, Mar 8
Tuesday, February 14, 2012
Late Homework Guidelines
Each homework is graded on a scale from 0 to 5.
For each day late, 0.5 is deducted from the total possible score for that homework.
The first few (four) homeworks are due this Thursday.
For each day late, 0.5 is deducted from the total possible score for that homework.
The first few (four) homeworks are due this Thursday.
Monday, February 13, 2012
"It seems like there is a neater way to do this, without all the @supress warnings."
http://stackoverflow.com/questions/1025837/initialize-java-generic-array-of-type-generic
http://stackoverflow.com/questions/529085/java-how-to-generic-array-creation
http://stackoverflow.com/questions/1025837/initialize-java-generic-array-of-type-generic
http://stackoverflow.com/questions/529085/java-how-to-generic-array-creation
Tuesday, February 7, 2012
lecture 3
I described up to postfix using arrays assignment (#4).
int x[] = new int[100];
for (int i = 0; i< x.length; i++)
System.out.println(x[i]);
for (int p : x)
System.out.println(p);
interfaces
Iterable
Iterator
when I implement Iterable, I must provide a
method called iterator()
iterator returns Iterator
anything implementing Iterator must provide
boolean hasNext()
next()
int x[] = new int[100];
Iterator i = x.iterator();
while(i.hasNext())
{
int p = i.next();
System.out.println(p);
}
syntactic sugar
Subscribe to:
Posts (Atom)