# Array ADT

### Creating a Structure for Array&#x20;

```c
struct Array {
    int A[20];
    int length;
    int size;
}
```

The following Array structure has three key elements to it:&#x20;

* The actual array
* An integer to store the length of the array (No. of elements present)
* An integer to store the size of an the array (Maximum capacity)

### Appending to the End

For appending an element to the end of an array, we need to first increment the length of the array and then store the desired value in the last index of array. This can be implemented using the following C code :

```c
void append(struct Array *arr, int value) {

    // Check if there is enough space to append
    if (arr -> length < arr -> size) {
        // Set desired value the last index and increment length
        arr -> A[length++] = value;
    }
    
}
```

### Inserting at a Specific Index

To insert an element at a particular index in the array, we must check if there's enough space in the array and then shift all the elements to the right of desired index by 1. This creates a space for the element to be inserted at the desired index. Function for the same :&#x20;

```c
void insert(struct Array *arr, int index, int value) {
    
    // Check if index is valid
    if (index >= 0 && index < arr -> length) {
        // Shift elements to the right 
        for (int i = 0; i > index; i++) {
            arr -> A[i] = arr -> A[i-1];
        }
        // Set the value at desired index
        arr -> A[index] = value;
        // Increment the length
        arr -> length++;
    }
    
}
```

### Deleting from Specific Index

In this operating, the index of the element which needs to be deleted will be passed along with the array. To perform this operation, all the elements which are present after the index from which an element was deleted should be shifted left by 1 and length of array must be decremented.

```c
void delete(struct Array *arr, int index) {

    // Check if index is valid or not
    if (index >= 0 && index < arr -> length) {
        // Shift elements to the left
        for (int i = index; i < arr -> length - 1; i++) {
            arr -> A[i] = arr -> A[i+1];
        }
        arr -> length--;
    }

}
```

Contributed by Nitin Ranganath


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://nitinranganath.gitbook.io/data-structures/arrays/array-adt.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
