1 / 12

Dynamic Arrays

Dynamic Arrays. CS580u - Spring 19. Motivation. Dynamic Array. Dynamic Arrays in C. Initializing a Vector. Classwork. Vector Implementation. typedef struct Vector{ int max_size; //initialize to 0 int current_size; //initialize to 0 Data * list; //initialize to NULL

kottley
Download Presentation

Dynamic Arrays

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. Dynamic Arrays CS580u - Spring 19

  2. Motivation

  3. Dynamic Array

  4. Dynamic Arrays in C

  5. Initializing a Vector Classwork

  6. Vector Implementation typedef struct Vector{ int max_size; //initialize to 0 int current_size; //initialize to 0 Data * list; //initialize to NULL void (*insert)(struct * Vector, Data d, int index); void (*remove)(struct * Vector, int index); Data * d (*read)(struct * Vector, int index); } Vector;

  7. How to grow

  8. Vector insert insert(vector, index, value){ if(index>=vector.max_size) vector.max_size = index * 2; //geometric expansion new_list = array[vector.max_size] copy vector.list -> new_list delete vector.list vector.list = new listif(index >= vector.current_size) vector.current_size = index + 1 vector.list[index] = value }

  9. Deleting From a Vector Classwork

  10. Vector Delete

  11. Vector Read

  12. When to use a Vector

More Related