1 / 20

1. (a) How to define an abstract data type? (2) How to implement an abstract data type?

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)

jbales
Download Presentation

1. (a) How to define an abstract data type? (2) How to implement an abstract data type?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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. • Stack: first-in last-out. Queue: first-in first-out. ACS-2947 Yangjun Chen

  2. (c) header p trailer … … Baltimore Rome Seattle Toronto DLNode x = new DLNode(); x.setElement(new String(“Toronto”)); ACS-2947 Yangjun Chen

  3. Update the links: trailer p header … … Baltimore Rome Seattle Toronto x.setPrev(p); x.setNext(p.getNext()); (p.getNext()).setPrev(x); p.setNext(x); x.prev  header; x.next  header.next; header.next.prev  x; header.next  x; ACS-2947 Yangjun Chen

  4. (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; ACS-2947 Yangjun Chen

  5. 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);} • class Instrument { • public void play(Note n) { • System.out.println(“Instrument.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);}} • } Answer: “Wind.play() 0” ACS-2947 Yangjun Chen

  6. (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); } ACS-2947 Yangjun Chen

  7. 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);} } } } ACS-2947 Yangjun Chen

  8. 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);} • } ACS-2947 Yangjun Chen

  9. ACS-2947 Yangjun Chen

  10. Sample trace: step1: step4: step3: 1 step2: 4 5 6 7 3 4 5 2 3 visit(3) visit(2) visit(1) step6: step7: step5: 6 7 8 9 10 11 7 8 9 10 11 12 13 5 6 7 8 9 visit(5) visit(6) visit(4) step16: step8: … … empty 8 9 10 11 12 13 14 15 visit(15) visit(7) ACS-2947 Yangjun Chen

  11. 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 maximumnumber 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) ACS-2947 Yangjun Chen

  12. si = i – h(i) ACS-2947 Yangjun Chen

  13. ACS-2947 Yangjun Chen

  14. 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) ACS-2947 Yangjun Chen

  15. 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 ACS-2947 Yangjun Chen

  16. 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 ACS-2947 Yangjun Chen

  17. 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; } ACS-2947 Yangjun Chen

  18. 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; }}} ACS-2947 Yangjun Chen

  19. 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++; } ACS-2947 Yangjun Chen

  20. 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); }} ACS-2947 Yangjun Chen

More Related