Array ADT
Array ADT
Array ADT
Ex. No: 01
Create an ADT for the array data structure with the following functions. arrADT will have the integer array
and size. [CO1, K3]
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
Given an array arr[] of integers. Find a peak element i.e. an element that is not smaller than its
neighbors.
Note: For corner elements, we need to consider only one neighbor.
Example:
Input: array[] = {10, 20, 15, 2, 23, 90, 67}
Output: 20, 90
Explanation: The element 20 has neighbors 10 and 15, both of them are less than 20, similarly 90
has neighbors 23 and 67.
Write a program in C to test the operations of arrADT with the following test cases:
Operation Expected Output
create(arrADT,20,[2,4,6,8,10]) 2,4,6,8,10
deleteAt(arrADT, 3) 2,4,6,10
insertAtEvery(arrADT,1) 1,2,1,4,1,6,1,10
search(arrADT,1) 2
search(arrADT,2) -1
printArray(arrADT) 1,2,1,4,1,6,1,10
create(arrADT,20,[10,20,15,2,23,90,67]) 20,90
create(arrADT,20,[1,2,3,4,4]) -1
Algorithm
Algorithm : create
Input :
i. pointer *p
ii. Size
iii. arrEntries[]
Output : void
1. p->size=size;
2. for(int i=0;i<p->size;i++)
{
p->a[i]=arrEntries[i]
}
Algorithm : deleteAt
Input :
i. pointer *p
ii. pos position
1. index=pos-1
2. for(int i=index;i<p->size-1;i++)
{
p->a[i]=p->a[i+1];
}
3. p->size--;
Algorithm : insertAtEvery
Input :
i. pointer *p
ii. data
1. newSize=p->size * 2;
2. *newArray=(int *) realloc(p->a, newSize * sizeof(int));
3. for(int i= p->size-1; i>=0; i--)
{
newArray[i*2+1]=newArray[i];
}
4. for(int i=0; i<newSize; i+=2)
{
newArray[i]=data;
}
5. p->a=newArray;
6. p->size=newSize;
Algorithm : search
Input :
i. pointer *p
ii. ele element to be searched
Algorithm : findPeek
Input :
1. pointer *p
Output : Print
1. int ret_arr[p->size];
2. int ret_count=0;
3. for (int i = 0; i < p->size; i++) {
4. if (i == 0 || i == p->size - 1) {
5. if (i == 0) {
6. if (p->a[i] > p->a[i + 1]) {
ret_arr[ret_count++] = p->a[i];
}
}
7. if (i == p->size - 1) {
8. if (p->a[i] > p->a[i - 1]) {
ret_arr[ret_count++] = p->a[i];
}
}
9. } else {
if (p->a[i] > p->a[i - 1] && p->a[i] > p->a[i + 1]) {
ret_arr[ret_count++] = p->a[i];
}
}
}
10. Print ret_arr;
Code
Appl.c
#include "arrAdt.h"
int main(){
insertMiddle(A,8,3);
insertLast(A,9);
insertFront(A,0);
printArray(A);
findPeek(A);
}
arrADT.h
struct arr{
int a[25];
int size;
};
void printArray(struct arr*p){
printf("\nPrinting Array:");
for(int i=0;i<p->size;i++){
printf("%d ",p->a[i]);
}
printf("\n");
}
flag++;
s_ele_pos=i;
if(flag==2)
break;
}
}
if(flag==2)
return s_ele_pos;
else
return -1;
}
//adt->a = newArray;
adt->size = newSize;
printf("\nInserted %d before every element\n",data);
}
}
void insertMiddle(struct arr *p,int ele,int pos){
for(int i=p->size-1;i>=pos-1;i--){
p->a[i+1]=p->a[i];
}
p->a[pos-1]=ele;
p->size++;
printf("\nInserted %d at position %d",ele,pos);
}
}
}
Output
Learning Outcome