CMPSCI 187: Programming With Data Structures

Solutions to First Midterm Exam

David Mix Barrington

9 October 2012

Directions:

  Q1: 20 points
  Q2: 10 points
  Q3: 15 points
  Q4: 15 points
  Q5: 10 points
  Q6: 10 points
  Q7: 20 points
 Total: 100 points

     public class AADL {
         LLNode<Dog> top;
         public AADL ( ) {
            top = null;}
         public void add (Dog d) {
            if (isEmpty( ) || d.getAge( ) <= top.getAge( )) {
               LLNode<Dog> newNode = new LLNode<Dog> (d);
               newNode.setLink(top);
               top = newNode;}}
         public void canAdd (Dog d) {
            return (isEmpty( ) || d.getAge( ) <= top.getAge( ));}
         public Dog remove( ) {
         // throws NullPointerException if list is empty
            Dog out = top.getInfo( );
            top = top.getLink( );
            return out;}
         public boolean isEmpty( ) {
            return (top == null);}
         public void insert (Dog d) {
            DADL storage = new DADL (dogs.length);
            while (!canAdd(d)) 
               storage.add(remove( ));
            add(d);
            while (!storage.isEmpty( ))
               add(storage.remove( ));}}
         
           
      public class DADL {
         LLNode<Dog> top;
         public DADL ( ) {
            top = null;}
         public void add (Dog d) {
            if (isEmpty( ) || d.getAge( ) >= top.getAge( )) {
               LLNode<Dog> newNode = new LLNode<Dog> (d);
               newNode.setLink(top);
               top = newNode;}}
         public void canAdd (Dog d) {
            return (isEmpty( ) || d.getAge( ) >= top.getAge( ));}
         public Dog remove( ) {
         // throws NullPointerException if list is empty
            Dog out = top.getInfo( );
            top = top.getLink( );
            return out;}
         public boolean isEmpty( ) {
            return (top == null);}
         public void insert (Dog d) {
            AADL storage = new AADL (dogs.length);
            while (!canAdd(d)) 
               storage.add(remove( ));
            add(d);
            while (!storage.isEmpty( ))
               add(storage.remove( ));}}
         

Last modified 28 September 2014