Transcript Document

Analysis of Midterm-Examination
1. (a) How to define an abstract data type?
(2)
How to implement an abstract data type?
(b) Describe the main character of Stacks.
(2)
Describe the main character of Queues.
(c) How to insert a new node into a doubly linked list after a certain node
p?
(3)
(d) How to remove a node from a doubly linked at the tail?
(3)
Answer:
(a) To define an abstract data type, we define an interface for that data type.
To implement an abstract data type, we define a class, in which the
methods defined in the interface for that data type are implemented.
(b) Stack: first-in last-out. Queue: first-in first-out.
Sept. 2015
ACS-2947 Yangjun Chen
1
Analysis of Midterm-Examination
(c)
header
p
trailer
……
Baltimore
Rome
Seattle
Toronto
DLNode x = new DLNode();
x.setElement(new String(“Toronto”));
Sept. 2015
ACS-2947 Yangjun Chen
2
Analysis of Midterm-Examination
Update the links:
header
trailer
p
……
Baltimore
Rome
Seattle
Toronto
x.setPrev(p);
x.setNext(p.getNext());
(p.getNext()).setPrev(x);
p.setNext(x);
Sept. 2015
x.prev  header;
x.next  header.next;
header.next.prev  x;
header.next  x;
ACS-2947 Yangjun Chen
3
Analysis of Midterm-Examination
(d)
header
trailer
Toronto
Baltimore
Rome
Seattle
((trailer.getPrev()).getPrev()).setNext(trailer);
trailer.setPrev((trailer.getPrev()).getPrev());
trailer.prev.prev.next  trailer;
trailer.prev  trailer.prev.prev;
Sept. 2015
ACS-2947 Yangjun Chen
4
Analysis of Midterm-Examination
(e)Analyze the following program and show the outputs of the program. (10)
class Note {
int value;
Note(int val) {value = val;}
public static final Note
middle_c = new Note(0),
c_sharp = new Note(1),
b_flat = new Note(2);}
Answer:
class Instrument {
public void play(Note n) {
System.out.println(“Instrument.play()”);}}
“Wind.play()
class Wind extends Instrument {
public void play(Note n) {
System.out.println(“Wind.play()” + “ “ + n.value);}
public static void tune(Instrument i) {
i.play(Note.middle_c);}
public static void main(String[] args) {
Wind flute = new Wind();
tune(flute);}}
0”
}
Sept. 2015
ACS-2947 Yangjun Chen
5
Analysis of Midterm-Examination
(2)Write a Java program to compute the following function
(20)
h(N) = f(N) + g(N),
where f(N) = 3*g(N-3) + 4*g(N-2) - 5*g(N-1) with f(0) = 1, f(1) = 1, and f(2) = 3,
and g(N) = f(N-3) *f(N-2) - 5*f(N-1) with g(0) = 1, g(1) = 2, and g(2) = 4.
public class Computing-H {
static void main(String args[]) {
int n = 10;
int h = computing-f(n) + computing-g(n);
System.out.println(“result:” + “ ” + h);
}
Sept. 2015
ACS-2947 Yangjun Chen
6
Analysis of Midterm-Examination
public int computing-f(int n) {
if (n==0 || n==1) return 1;
else {if (n==2) return 3;
else return 3*computing-g(n-3)+4*computing-g(n-2
-5*computing-g(n-1);
}
}
public int computing-g(int n) {
if (n==0 )return 1;
else {if (n==1) return 2;
else {if (n==2) return 4;
else return computing-f(n-3)*computing-f(n-2)
-5*computing-f(n-1);}
}
}
}
Sept. 2015
ACS-2947 Yangjun Chen
7
Analysis of Midterm-Examination
3. Please give an algorithm (not a java program) to search a binary
tree in the breadth-first manner using a Queue data structure to
control the process.
(7)
Algorithm Breadth-first-search(T, root)
establish queue S;
S.enqueue(root);
while (the S is not empty) do
{x := S.dequeue; print(x);
let x1, x2, …, xk be the children of x;
for (i = 1 to k) do {S.enqueue(xi);}
}
Sept. 2015
ACS-2947 Yangjun Chen
8
Analysis of Midterm-Examination
1
2
3
4
8
Sept. 2015
5
9
10
6
11
12
7
13
ACS-2947 Yangjun Chen
14
15
9
Analysis of Midterm-Examination
Sample trace:
step1:
1
step5:
step2:
step3:
step4:
2 3
3 4 5
4 5 6 7
visit(1)
visit(2)
visit(3)
step6:
step7:
5 6 7 8 9
6 7 8 9 10 11
7 8 9 10 11 12 13
visit(4)
visit(5)
visit(6)
step8:
8 9 10 11 12 13 14 15
visit(7)
Sept. 2015
……
step16:
empty
visit(15)
ACS-2947 Yangjun Chen
10
Analysis of Midterm-Examination
4. Assume that interface Stack and class ArrayStack are available.
Please give a java program to compute the stock spans with
Stack and ArrayStack being used. The span of the stock’s price
on a given day is defined to be the maximum number of the
consecutive days up to the current day such that the stock price
on each of those days has been less than or equal to the price on
the current day.
(25)
Sept. 2015
ACS-2947 Yangjun Chen
11
Analysis of Midterm-Examination
public void computeDailyHighSpan( Quote Q[]) {
int prevHigh;
Stack D = new ArrayStack();
for( int i = 0; i < Q.length; i++ ) {
while( !D.isEmpty() &&
Q[ i ].getPrice() >= ((Quote)D.top()).getPrice() )
D.pop();
if( D.isEmpty() )
prevHigh = -1;
else
prevHigh = (( Quote )D.top()).getDay();
Q[ i ].setSpan( i - prevHigh );
D.push( Q[ i ]);
}
}
si = i – h(i)
Sept. 2015
ACS-2947 Yangjun Chen
12
Analysis of Midterm-Examination
public class Quote {
private int day, price, span;
public Quote( int d, int p ) {
setDay( d );
setPrice( p );
}
public void setDay( int d ) { day = d; }
public int getDay() { return day; }
public void setPrice( int p ) { price = p; }
public int getPrice() { return price; }
public void setSpan( int s ) { span = s; }
public int getSpan() { return span; }
}
Sept. 2015
ACS-2947 Yangjun Chen
13
Analysis of Midterm-Examination
5. Please give a java program to calculate the following expression:
“1+2+3-4-5+6-7+8-9”.
It is required to use ArrayStack to control the computation. (20)
Sept. 2015
ACS-2947 Yangjun Chen
14
Analysis of Midterm-Examination
public class Expression-computation{ //start class
public static void main( String args[] )//start main body
{ String s = "1+2+3-4-5+6-7+8+9";
Stack data = new ArrayStack();
int temp; char operator;
int a = 0;
data.push (new Integer(1));
for (int x = 1; x < s.length(); x++) {
if (s.charAt(x) == ‘+’ || s.charAt(x) == ‘-’)
data.push(new Character(s.charAt(x)));
else { //else it is a number
Sept. 2015
ACS-2947 Yangjun Chen
15
Analysis of Midterm-Examination
operator = (Character) data.pop();
a = ((Integer)data.pop()).intValue();
if (operator == ‘+’)
temp = a + charAt(x) – ‘0’;
else temp = a – (charAt(x) – ‘0’);
data.push(new Integer(temp));
}
System.out.println("The answer is: " +
((Integer) data.pop()).intValue());
} // end method main
}// end class
Sept. 2015
ACS-2947 Yangjun Chen
16
Analysis of Midterm-Examination
6. Define a class to implement Queue data structure, using a
singly linked list as the physical element container. (Assume
that the interface Queue and the class Node are available.
You only need to give class ListQueue.) (8)
public interface Queue {
public void enqueue( Object element );
throws QueueFullException;
public Object dequeue()
throws QueueEmptyException;
public int size();
public boolean isEmpty();
public Object front()
throws QueueEmptyException;
}
Sept. 2015
ACS-2947 Yangjun Chen
17
Analysis of Midterm-Examination
public class Node {
private Object element;
private Node next;
public Node() {
this( null, null )
}
public Node (object e, Node n) {
element = e;
next = n;
}
Object getElement()
return element;
}
Node getNext() {
return next;
}
void setElemnt(Object newElem) {
element = newElem;
}
void setNext(Node newNext) {
Next = newNext;
}}}
Sept. 2015
ACS-2947 Yangjun Chen
18
Analysis of Midterm-Examination
public class ListQueue implements Queue {
protected Node front, rear; //reference to the front and rear node
protected int size; // number of elements in the queue
public ListStack() { // constructs an empty queue
front = null; rear = null;
size = 0;
}
public int size() {
return size;
}
public boolean isEmpty() {
if (front == null)
return true;
return false;
}
public void enqueue(Object elem) {
Node v = new Node(elem, null);//create and link-in a new node
if (size == 0) {front = v; rear = v;}
else {rear.setNext(v); rear = v;
size++;
}
Sept. 2015
ACS-2947 Yangjun Chen
19
Analysis of Midterm-Examination
public Object front() throws QueueEmptyException {
if (isEmpty())
throw new QueueEmptyException("Stack is empty.");
return front.getElement();
}
public Object dequeue() throws QueueEmptyException {
if (isEmpty())
throw new QueueEmptyException(“Queue is empty.");
Object temp = front.getElement();
front = front.getNext(); // link-out the former front node
size--;
return temp;
}
}
/**
* Runtime exception thrown when one tries to perform operation
* front or dequeue on an empty queue.
*/
public class QueueEmptyException extends RuntimeException {
public QueueEmptyException(String err) {
super(err);
}}
Sept. 2015
ACS-2947 Yangjun Chen
20