1 / 24

Vectors, Lists, and Sequences

This article discusses the implementation of the Vector Abstract Data Type (ADT) in Java, including the realization of a deque using a vector and the array-based implementation of the Vector ADT. It also explores the running times of vector methods, such as insertAtRank(r,e) and removeAtRank(r), which are proportional to the size of the vector. The article provides a Java implementation of the Vector interface and includes exception classes for handling invalid ranks and empty containers.

equade
Download Presentation

Vectors, Lists, and Sequences

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. Vectors, Lists, and Sequences • Ed. 2 and 3.: Chapter 5 • Ed. 4: Chapter 6

  2. Vectors, Lists, and Sequences • (Chapter 5) • Vectors • - Vector ADT and implementation • Lists • - List ADT and implementation • Sequences • - Sequence ADT and implementation • Iterators

  3. Vectors

  4. The Vector Abstract Data Type

  5. Realization of a Deque with a Vector

  6. A Simple Array-Based Implementation rank Vector ADT index Array

  7. A Simple Array-Based Implementation

  8. The Running Times of Vector Methods insertAtRank(r,e) and removeAtRank(r): Running time is proportional to n, the size of the vector. running time = O(n).

  9. Java Implementation public interface Vector { public int size(); public boolean isEmpty(); public Object elemAtRank(int r); public Object replaceAtRank(int r, Object e); public void insertAtRank(int r, Object e); public Object removeAtRank(int r); }

  10. class InvalidRankException extends Exception { public InvalidPositionException() {super();} public InvalidPositionException(String s) { super(s);} } class EmptyContainerExeption extends Exception { public EmptyContainerExeption() {super();} public EmptyContainerExeption(String s) { super(s);} } class BoundaryViolationExeption extends Exception { public BoundaryViolationExeption() {super();} public BoundaryViolationExeption(String s) { super(s);} }

  11. Java Implementation public class ArrayVector implements Vector { private Object[] A; // array storing the elements of the vector private int capacity = 16; // initial length of array A private int size = 0; // number of elements stored in the vector /** Creates the vector with initial capacity 16. */ public ArrayVector() { A = new Object[capacity]; }

  12. public Object elemAtRank(int r) throws BoundaryViolationException { checkRank(r, size()); return a[r]; } public int size() {return size;} public boolean isEmpty {return size()==0;}

  13. /** Inserts an element at the given rank. */ public void insertAtRank(int r, Object e) throws BoundaryViolationException { checkRank(r, size() + 1); if (size == capacity) { // an overflow capacity *= 2; Object[] B = new Object[capacity]; for (int i=0; i<size; i++) B[i] = A[i]; A = B;} for (int i=size-1; i>=r; i--) // shift elements up A[i+1] = A[i]; A[r] = e; size++; }

  14. /** Removes the element stored at the given rank. */ public Object removeAtRank(int r) throws BoundaryViolationException { checkRank(r, size()); Object temp = A[r]; for (int i=r; i<size-1; i++) // shift elements down A[i] = A[i+1]; size--; return temp; }

  15. public Object replaceAtRank(int r, Object e) throws BoundaryViolationException { checkRank(r, size()); Object temp = A[r]; A[r] = e; return temp; } public void checkRank(int r, int s) throws BoundaryViolationException { if (0 > r) || (r > s) throw new BoundaryViolationException; } }

  16. Data Structure Exercises 7.2

More Related