CPSC 327 Data Structures and Algorithms Spring 2024

Homework 3
due Fri 2/2 in class

  1. A partial class definition for an array-based implementation of a list of integers is given below. Fill in the body of the remove method with an iterative implementation (in Java) of deletion from an array.

        public class IntArrayList {
    
          private int[] elements_;
          private int size_;  // number of elements in the list
    
          ...
        
          /**
           * Remove the element in position pos.
           *
           * @param pos position of element to remove
           * @return the element removed
          */
          public int remove ( int pos ) { ... }
        }
      
  2. Fill in the body of the function below with an iterative implementation (in Java) of deletion from a doubly-linked list.

        /**
         * Remove the node todel from the list.
         *
         * @param head head of the list
         * @param todel node to remove
         * @return the head of the list after the removal
        */
        public DoubleListNode remove ( DoubleListNode head,
                                       DoubleListNode todel ) { ... }
      

    Use the following definition for DoubleListNode:

    public class DoubleListNode {
      public DoubleListNode ( int value ) { ... }
      public DoubleListNode ( int value,
                              DoubleListNode prev, DoubleListNode next ) { ... }
    
      public int getValue () { ... }
      public void setValue ( int value ) { ... }
    
      public DoubleListNode getPrev() { ... }
      public DoubleListNode getNext() { ... }
    
      public void setPrev ( DoubleListNode prev ) { ... }
      public void setNext ( DoubleListNode next ) { ... }
    }