🗂️
Data Structures
  • Data Structures Manual
  • Arrays
    • Array ADT
    • Linear Search
    • Binary Search
    • Some More Basic Operations
    • Reversing an Array
    • Operations in a Sorted Array
    • Merging Two Arrays
    • Set Operations
    • Finding Missing Elements
    • Duplicates in an Array
    • Getting a Pair whose Sum = K
    • Finding Max & Min in Single Scan
  • Strings
    • Finding the Length of a String
    • Changing Cases in a String
    • Finding Number of Vowels, Consonants & Words
    • Reversing a String
    • Checking for Palindrome
    • Duplicates in a String
    • Checking if Strings are Anagrams
    • Permutations of a String
  • Singly Linked List
    • Displaying the Nodes
    • Counting the Nodes
    • Sum of all Nodes
    • Finding the Maximum Element
    • Searching in a Node
    • Inserting a Node
    • Inserting a Node in Sorted List
    • Deleting a Node
    • Checking if List is Sorted
    • Removing Duplicates from a List
    • Reversing a Linked List
    • Concatenating Two Lists
    • Detecting a Loop in Linked List
    • Merge Two Sorted Lists
    • Finding the Middle Node
  • Cirular Linked List
    • Displaying the Nodes
    • Inserting a Node
    • Deleting a Node
  • Doubly Linked List
    • Inserting a Node
    • Deleting a Node
    • Reversing a Doubly Linked List
    • Circular Doubly Linked List
  • Stack
    • Stack Using Array
    • Stack Using Linked List
    • Balancing Parenthesis
    • Infix to Postfix
    • Evaluation of Postfix Expression
  • Queue
    • Queue using Array
    • Queue using Linked List
    • Double Ended Queue
  • Binary Tree
    • Creating a Binary Tree using Queue
    • Recursive Tree Traversals
    • Iterative Tree Traversals
    • Level Order Traversal
    • Counting Nodes in a Binary Tree
    • Finding the Height of Tree
    • Finding Sum of All Nodes
  • Binary Search Tree
    • Searching in a BST
    • Inserting in a BST
    • Deleting in a BST
  • AVL Tree
    • Inserting in an AVL Tree
    • AVL Tree Rotations
    • Deleting in an AVL Tree
  • Heap
    • Inserting in a Heap
    • Deleting in a Heap
    • Heapify
Powered by GitBook
On this page
  • Creating a Structure for Array
  • Appending to the End
  • Inserting at a Specific Index
  • Deleting from Specific Index

Was this helpful?

  1. Arrays

Array ADT

Implementation of append, insert, delete and display function in an array.

Creating a Structure for Array

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

The following Array structure has three key elements to it:

  • 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 :

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 :

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.

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

PreviousData Structures ManualNextLinear Search

Last updated 4 years ago

Was this helpful?