DSA 7 : array ADT

PHOTO EMBED

Mon Mar 13 2023 04:38:12 GMT+0000 (Coordinated Universal Time)

Saved by @saakshi #c++

#include <stdlib.h>
#include <stdio.h>
using namespace std;

struct Array
{
    int *A;
    int size;
    int length;
};

void Display(struct Array arr)
{
  int i;
  printf("\n Elements are \n");
  for(i=0; i<arr.length; i++)
    printf("%d ", arr.A[i]);
}

void Append (struct Array *arr, int x)
{
  if(arr->length < arr->size)
    arr->A[arr->length++] = x;
}

void Insert(struct Array *arr, int index, int x)
{
  int i;
  if(index >= 0 && index <= arr->length)
    {
      for(i=arr->length; i>index; i--)
        arr->A[i] = arr->A[i-1];
      arr->A[index]=x;
      arr->length++;
    }
}

int main() 
{
    struct Array arr= {{2,3,4,5,6}, 10, 5};
	Append(&arr, 10);
	insert(&arr, 5, 10);
	Display(arr);
  
    //printf("Enter the size of an array : ") ;
    //scanf("%d", &arr.size);
    //arr.A = (int *)malloc(arr.size*sizeof(int));
    //arr.length=0;

    return 0;
}
content_copyCOPY