You are on page 1of 9

Name: G Harikumar Date:13.09.

2023
Reg No: 3122225001033
UCS 2312 Data Structures Lab
Assignment 1: Array ADT and its application
Create an ADT for the array data structure with the following functions. arrADT will have the
integer array and size.
a. create(arrADT,size, array) – Create the array with the required number of elements
b. deleteAt(arrADT, pos ) – Delete the specified element
c. insertAtEvery(arrADT,data) – Insert data before every element
d. search(arrADT, key) – return the position of the second occurrence of the element. If found
return the position, otherwise return 1
e. printArray(arrADT) – prints the elements of the array
f. findPeek(arrADT, int *) – return a set of peek elements

Source code:
Header file:

#include<stdio.h>
#include<stdlib.h>

struct arrAdt {
int* array;
int size;
};

void create(struct arrAdt* newa, int size) {

newa->array = (int*)malloc(size * sizeof(int));


newa->size = size;

printf("enter the elements in the array\n");


for (int i = 0;i < size;i++) {
printf("%d element :", i);
scanf("%d", &(newa->array[i]));
}

}
void display(struct arrAdt* newa) {
for (int i = 0;i < newa->size;i++){
printf("%d ", newa->array[i]);}
printf("\n");
}

Department of Computer Science and Engineering


Name: G Harikumar Date:13.09.2023
Reg No: 3122225001033
void deleteAt(struct arrAdt* newa,int pos)
{
for(int i=0;i< newa->size-1;i++)
{
if(i<pos)
continue;
else
newa->array[i]=newa->array[i+1];
}
newa->size=(newa->size)-1;
}

int search(struct arrAdt* newa,int key)


{
int count=0;
int len=newa->size;
int index;
for(int i=0;i<len;i++)
{
if(newa->array[i]==key)
{
count++;
if(count==2)
{
index=i;
}
}}
if(count==0 )
{
return -1;
}
else if(count==1)
{
return 1;
}
else
{
return index;
}
}

void peak(struct arrAdt* newa){


printf("\n");
int arr1[newa->size],j=0;

Department of Computer Science and Engineering


Name: G Harikumar Date:13.09.2023
Reg No: 3122225001033
for (int i=0;i<newa->size;i++){
if(i==0){
if(newa->array[0]>newa->array[1]){
arr1[j]=newa->array[0];
j++;
}
}
else if(i==newa->size-1){
if(newa->array[newa->size-1]>newa->array[newa->size-2]){
arr1[j]=newa->array[newa->size-1];
j++;

}
}
else{
if(newa->array[i]>newa->array[i+1] && newa->array[i]>newa-
>array[i-1]){
arr1[j]=newa->array[i];
j++;
}
}
}
for (int k=0;k<j;k++){
printf("%d\t",arr1[k]);
}
}

void insertAtEvery(struct arrAdt* newa,int key)


{
newa->size=2*(newa->size);
int len=newa->size;
int arr1[len];
int len1=len/2;
for(int i=0;i<len1;i++)
{
arr1[i]=newa->array[i];
}
newa->array=realloc((newa->array),len*(sizeof(int)));
for(int i=0;i<len;i++)
{
if(i%2==0)
{
newa->array[i]=key;
}

Department of Computer Science and Engineering


Name: G Harikumar Date:13.09.2023
Reg No: 3122225001033
else
{
newa->array[i]=arr1[(i-1)/2];
}
}
}

Main file:
#include"arrAdt.h"
int main()
{
struct arrAdt newarray;
int choise;
int a=1;
while(a!=0){
printf("\n1-create a new array\n2-deleting\n3-searching\n4-finding
peak\n5-inserting at every position\n");
printf("enter the choice:");
scanf("%d",&choise);
switch(choise)
{
case 1:
printf("creating the array:\n");
printf("enter the number of elements:");
int size;
scanf("%d",&size);
create(&newarray,size);
display(&newarray);
break;

case 2:
printf("deleting:\n");
printf("enter the position of the element to delete:");
int pos;
scanf("%d",&pos);
deleteAt(&newarray,pos);
display(&newarray);
break;

case 3:
printf("searching:\n");
int key,res;

Department of Computer Science and Engineering


Name: G Harikumar Date:13.09.2023
Reg No: 3122225001033
printf("enter the element to search:");
scanf("%d",&key);
res=search(&newarray,key);
if(res==-1)
printf("not found");
else if(res==1)
printf("found");
else
printf("found at %d",res);
break;

case 4:
printf("finding peak:\n");
peak(&newarray);
break;

case 5:
printf("inserting at every position\n");
int ins;
printf("enter the number to insert:");
scanf("%d",&ins);
insertAtEvery(&newarray,ins);
display(&newarray);
a=0;
break;

}
}}

Department of Computer Science and Engineering


Name: G Harikumar Date:13.09.2023
Reg No: 3122225001033

Sample Output:

Department of Computer Science and Engineering


Name: G Harikumar Date:13.09.2023
Reg No: 3122225001033
Pseudo code:

Department of Computer Science and Engineering


Name: G Harikumar Date:13.09.2023
Reg No: 3122225001033

Department of Computer Science and Engineering


Name: G Harikumar Date:13.09.2023
Reg No: 3122225001033

Department of Computer Science and Engineering

You might also like