Exam 3 study guide

Covering Abstract Data Structures – labs 5 and 6

Generics:

What is a java Generic, give an example.

What is wrong with the above?

iv must hold a Double

Why are java Generics useful when dealing with Abstract Data Types?

Given a class definition that uses a Generic, be able to instantiate an object from this class, or given a statement instantiating an object that supplies a generic, supply the class definition.

The example above does both

ADTs:

Stack:

Given:

/**

 * Stack of positive numbers

 * @author nordquip

 *

 */

public class Stack

{    

    private int[] st;

    private int top = 0;

  

       /**

     * Create the array holding the stack values

     * @param maxLen - the length of the array

     */

    public Stack(int maxLen) {     

 

    }

    /**

     * push a positive int onto the stack

     * will not push a negative int

     * @param value - the int to push

     * @return - the int pushed on the stack or a negative number if the stack was

     * full or a negative number was passed.

     */

    public int push(int value) {

 

    }

    /**

     * pop the int on the top of the stack

     * @return - the int on the top of the stack, -1 if there are no values on the stack

     */

    public int pop () {

 

    }

  

    public static void main(String[] args) {

             Stack st = new Stack(3);

             System.out.println(st.push(7));

             System.out.println(st.push(-15));

             System.out.println(st.pop());

             System.out.println(st.push(23));

             System.out.println(st.push(47));

             System.out.println(st.push(59));

             System.out.println(st.push(62));

             System.out.println(st.pop());

    }

}

Complete this class using an array to implement the stack.  You may modify only the code inside the constructor and the push and pop methods.

Running main should print the following and not throw an exception:
7
-1

7

23

47

59

-1

59

 

Singly-linked list:

Complete the inner Node class, instance variables, and method signatures necessary to implement the following class:

public class SinglyLinkedList<D extends Comparable<D>> {

 

// D extends Comparable<D> means D implements the Comparable interface, so in

// SinglyLinkedList, objects of type D can call the compareTo() method to

// compare themselves to another object of type D

      

       /**

        * add newElement after the first occurrence of existingElement in the list

        * If existingElement is not in the list, add newElement

        * at the head of the list.

        * @param newElement

        * @param existingElement

        * @return existingElement or null if existing element was not in the list

        * before the call to add

        */

 

      

       /**

        * removes the first occurrence of element in the list

        * @return true if element was found and removed, false otherwise

        */

 

      

       /**

        * find the first occurrence of element in the list

        * @param element

        * @return true if element is in the list

        */

 

}

XXXXXX Answer XXXXXXX

public class SinglyLinkedList<D extends Comparable<D>> {

       private class Node {

             private D data;

             private Node next;

       }

       private Node head;

      

       /**

        * add newElement after the first occurrence of existingElement in the list

        * If existingElement is not in the list, add newElement

        * at the head of the list.

        * @param newElement

        * @param existingElement

        * @return existingElement or null if existing element was not in the list

        * before the call to add

        */

       public D addAfter(D newElement, D existingElement) {

            

       }

      

       /**

        * removes the first occurrence of element in the list

        * @return true if element was found and removed, false otherwise

        */

       public boolean remove(D element) {

            

       }

      

       /**

        * find the first occurrence of element in the list

        * @param element

        * @return true if element is in the list

        */

       public boolean find (D element) {

            

       }

}

Assuming there was a private method:

       /**

        * Uses compareTo() to determine equivalence between objects of type D

        * @param element

        * @return the first node holding a value equivalent to element

        */

       private Node searchFor(D element) {

       }

Implement this method:

XXXXXX Answer XXXXXXX

       /**

        * Uses compareTo() to determine equivalence between objects of type D

        * @param element

        * @return the first node holding a value equivalent to element

        */

       private Node searchFor(D element) {

             Node n = head;

             while (n != null && n.data.compareTo(element) != 0)

                    n = n.next;

             return n;

       }

Notice this implementation does not assume the nodes are in sorted order.

Think about how to write a method to replace addAfter() that takes a new element and inserts this element into the list where it needs to go to keep the list in sorted order.   

General questions:

How is a stack implemented differently than a queue?

 

Why are trees not allowed to have cycles?

 

Binary Search Tree:

Given the sequence of numbers: {50, 20, 40, 75, 30}, show the resulting binary search tree.

Answer:

Given the tree above, rebalance it to make searches more efficient.

Answer:

Give a number where the number of nodes visited was less with the balanced tree. 

30

For this number how many nodes were visited in the unbalance tree?

4  (50, 20, 40, 30)

How many in the balanced tree?

2 (40, 30)

Reorder the sequence of numbers above, so they would produce the balanced tree.

40, 30, 20, 50, 75