An ordered list is a list in which an element that appears later in the list is
ID: 3599770 • Letter: A
Question
An ordered list is a list in which an element that appears later in the list is never smaller than an element that appears earlier in the list, where the notion of "smaller than" is given by a specific chosen relation. For example, the list
is an ordered list relative to the usual < relation on numbers, the list
is an ordered list relative to the > relation on numbers but not relative to the < order and the list
is an ordered list relative to neither the usual < nor the usual > relation on numbers.
1. Clearly, whether or not a list is ordered is dependent also on the ordering relation in use. Define a type constructor olist that is like the list type constructor except that values of this type carry enough information to determine whether or not they are ordered lists relative to the intended ordering relation.
2. Define a function
that takes an ordering relation over a given type and returns an empty olist of that type.
3. Using let declarations, bind list1 and list2 to two different values of int olist type that use different ordering relations and that satisfy the invariant based on that ordering relation. Then bind list3 to a value of int olist that does not satisfy the necessary invariant.
4. Define a function
that takes an ordered list and determines whether or not it satisfied the required ordering invariant based on the intended ordering relation. Test this function using the identifiers list1, list2 and list3.
5. Define a function
that takes an element and an ordered list and returns an ordered list that has all the original elements plus the new one and that continues to satisfy the invariant based on the relevant ordering property.
6. Define a function
that takes an ordered list and converts it into an ordinary list with the same elements and in the same order as in the given olist.
Language: Ocaml
Explanation / Answer
package com;
public class DList<T> {
private DNode<T> header, trailer;
private int size;
public DList() {
size = 0;
header = new DNode<T>(null, null, null);
trailer = new DNode<T>(null, header, null);
header.setNext(trailer);
}
// utility methods
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
// give clients access to nodes, but not to the header or trailer
public DNode<T> getFirst() throws Exception {
if (isEmpty())
throw new Exception("Empty");
return header.getNext();
}
public DNode<T> getLast() throws Exception {
if (isEmpty())
throw new Exception("Empty");
return trailer.getPrev();
}
public DNode<T> getNext(DNode<T> v) throws Exception {
DNode<T> ans = v.getNext();
if (ans == null || ans == trailer)
throw new Exception("No such node");
return ans;
}
public DNode<T> getPrev(DNode<T> v) throws Exception {
DNode<T> ans = v.getPrev();
if (ans == null || ans == header)
throw new Exception("No such node");
return ans;
}
// methods to change the list
public void addBefore(T d, DNode<T> v) {
DNode<T> u = v.getPrev();
DNode<T> x = new DNode<T>(d, u, v);
u.setNext(x);
v.setPrev(x);
size++;
}
public void addAfter(T d, DNode<T> v) {
DNode<T> w = v.getNext();
DNode<T> x = new DNode<T>(d, v, w);
v.setNext(x);
w.setPrev(x);
size++;
}
public void addFirst(T d) {
addAfter(d, header);
}
public void addLast(T d) {
addBefore(d, trailer);
}
public T remove(DNode<T> v) throws Exception {
if (v == header || v == trailer)
throw new Exception("Sentinel");
DNode<T> u = v.getPrev();
DNode<T> w = v.getNext();
w.setPrev(u);
u.setNext(w);
size--;
return v.getData();
}
// LinkedList testing methods:
public String toString() {
String ans = "";
DNode<T> n = header;
ans += "(H)<-->";
do {
n = n.getNext();
if (n == trailer)
ans += "(T)";
else
ans += (n.getData() + "<-->");
} while (n != trailer);
return ans;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.