Dynamic Array
Problem 1
Create an array data type using own data structure to overcome problem of array
#include<stdio.h>
#include<stdlib.h>
//* use my structure
struct DynArray
{
int capacity;
int lastIndex;
int *ptr;
};
void doubleArray(struct DynArray *arr)
{
int *temp;
temp = (int*) malloc(sizeof(int)*arr->capacity*2);
for(int i=0;i<=arr->lastIndex;i++)
temp[i] = arr->ptr[i];
free(arr->ptr);
arr->ptr =temp;
arr->capacity*=2;
}
void halfArray(struct DynArray *arr)
{
int *temp;
temp = (int*) malloc(sizeof(int)*arr->capacity/2);
for(int i=0;i<=arr->lastIndex;i++)
temp[i] = arr->ptr[i];
free(arr->ptr);
arr->ptr =temp;
arr->capacity/=2;
}
struct DynArray* createArray(int cap)
{
struct DynArray *arr = NULL;
arr = (struct DynArray*) malloc(sizeof(struct DynArray));
arr->capacity=cap;
arr->lastIndex = -1;
arr->ptr = (int*) malloc(sizeof(int)*arr->capacity);
return arr ;
};
void append(struct DynArray *arr, int data)
{
/*check if array is full*/
if(arr->lastIndex+1 == arr->capacity)
doubleArray(arr);
arr->lastIndex+=1;
arr->ptr[arr->lastIndex] = data;
}
void insertAt(struct DynArray *arr,int index, int data)
{
/*check if index is valid*/
if(index <0 || index >arr->lastIndex+1)
printf("\nInsertion Error : Invalid Index");
else
{
if(arr->lastIndex+1 == arr->capacity)
doubleArray(arr);
for(int i = arr->lastIndex ;i >=index ;i--)
arr->ptr[i+1] = arr->ptr[i];
arr->ptr[index] = data;
arr->lastIndex++;
}
}
int countElements(struct DynArray *arr)
{
return (1+arr->lastIndex);
}
int getItem(struct DynArray *arr,int index)
{
if(index <0 || index > arr->lastIndex)
printf("\nError: Invalid Index");
else
return (arr->ptr[index]);
}
void deleteItem(struct DynArray *arr, int index)
{
if(index <0 || index > arr->lastIndex)
printf("\nDeletion Error : Invalid index");
else
{
for(int i = index ; i<= arr->lastIndex - 1 ;i++)
arr->ptr[i] = arr->ptr[i+1];
arr->lastIndex --;
if(arr->capacity/2 == arr->lastIndex+1)
halfArray(arr);
}
}
void editElement(struct DynArray *arr, int index, int data)
{
if(index <0 || index > arr->lastIndex)
printf("\nEdit Error : Invalid index");
arr->ptr[index] = data;
}
int searchIndex(struct DynArray *arr, int data)
{
for(int i=0;i<= arr->lastIndex;i++)
if(arr->ptr[i] == data)
return i;
return -1;
}
void showElements(struct DynArray *arr)
{
for(int i=0;i<=arr->lastIndex;i++)
printf(" %d",arr->ptr[i]);
}
int menu()
{
int choice;
printf("\n--------------------------");
printf("\n1.Append");
printf("\n2.Insert");
printf("\n3.Delete");
printf("\n4.Edit");
printf("\n5.Exit");
printf("\nEnter your choice\n");
scanf("%d",&choice);
return choice;
}
int main()
{
int x, index;
struct DynArray *arr;
arr = createArray(4);
while(1)
{
system("clear");
printf("\nCapacity : %d",arr->capacity);
printf("\n Count : %d ",countElements(arr));
printf("Data :");
showElements(arr);
switch(menu())
{
case 1:
printf("\nEnter the value to append");
scanf("%d",&x);
append(arr,x);
break;
case 2:
printf("\nEnter the value and index ");
scanf("%d%d",&x,&index);
insertAt(arr,index,x);
break;
case 3:
printf("\n Enter index to be deleted");
scanf("%d",&index);
deleteItem(arr,index);
break;
case 4:
printf("\nEnter the index to be edited");
scanf("%d",&index);
printf("\n Enter value to be entered");
editElement(arr,index,x);
break;
case 5:
exit(0);
break;
default:
printf("Invalid choice try again");
}
getchar();
}
return 0;
}
Comments
Post a Comment