Tuesday, March 20, 2012

Solutions to sample midterm




12)
class Node
{
int val;
Node next;
}

13)
a) class SLL
{
Node head, tail;
int size;
// constructor present but not given here
// choose not to make header or trailer
}

b) void insertBefore(Node n, int x)
{
Node m = new Node(x);

if (n==head)
{
m.next = n;
head = m;
}
else
{
// traverse from head
for (Node cur = head; cur.next!=n; cur=cur.next)
; // do nothing
cur.next=m;
m.next=n;

}
size++;
}

c) void insertAfter(Node n, int x)
{
Node m=new Node(x);
m.next = n.next;
n.next = m;

if (n==tail)
m = tail;
size++;
}

d) void remove(int value)
{
if (size==0)
return;
Node prev = null;
for(Node cur = head; cur != null && cur.val != value; prev=cur, cur=cur.next)
; // do nothing
if (prev==null) // found item at head
{
head=head.next;
size--;
}
else if (cur != null && cur.val == value) // item found
{
prev.next = cur.next; // leapfrog
}
if (prev.next==null)
tail=prev;
}
d) void removeSpan(Node start, Node finish)
{
start.next = finish;
}

e) void swap(Node n, Node m)
{
int temp = n.val;
n.val = m.val;
m.val = temp;
}

Tuesday, March 13, 2012


Test: Thurs Mar 22

HW: Construct a Priority Queue
using the Adapter design pattern,
use a linked list or array (/vector)
to implement a PriorityQueue of ints.

Thursday, March 8, 2012

Vote!


joshwaxman@gmail.com
CS313 vote
Thu Mar 15 vs. Tue Mar 20 vs Thurs Mar 22

Sample midterm

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);

Tuesday, March 6, 2012


Queue based on a singly linked list, using the Adapter design pattern

size()
isEmpty()
enqueue()
dequeue()
front()

Queue based on a Vector, using the Adapter design pattern

size()
isEmpty()
enqueue()
dequeue()
front()

Thurs, Mar 15, make your StackArray into an iterable collection

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++]; }
}