Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Date:20/09/2023

Ex. No: 01

UCS 2312 Data Structures Lab


Exercise 1: Array ADT and its applications

Date of Exercise: 05.09.2023

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

Best practices to be followed:


 Design before coding
 Usage of algorithm notation
 Use of multi-file C program
 Versioning of code

Data Structure –Array

Department of Computer Science and Engineering


Date:20/09/2023
Ex. No: 01

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

Output : delete element at this 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

Output : insert the data before every element

1. newSize=p->size * 2;
2. *newArray=(int *) realloc(p->a, newSize * sizeof(int));
3. for(int i= p->size-1; i>=0; i--)
{

Department of Computer Science and Engineering


Date:20/09/2023
Ex. No: 01

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

Output : return position if found otherwise return -1

1. for(int i=0; i<p->size; i++)


{
if(p->a[i]==ele){
return i
}
}
return -1;
}

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];

Department of Computer Science and Engineering


Date:20/09/2023
Ex. No: 01

}
}
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(){

struct arr *A;


A=(struct arr*)malloc(sizeof(struct arr));
int arr[]={2,4,6,8,10};
create(A,5,arr);
deleteAt(A,3);
insertAtEvery(A,1);
printf("\nSearching 1 :%d\n",search(A,1));
printf("\nSearching 2 :%d\n",search(A,2));
printArray(A);
findPeek(A);
printf("\n");

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){

Department of Computer Science and Engineering


Date:20/09/2023
Ex. No: 01

printf("\nPrinting Array:");
for(int i=0;i<p->size;i++){
printf("%d ",p->a[i]);
}
printf("\n");
}

int findPeek(struct arr *p) {


int ret_arr[p->size];
int ret_count=0;
for (int i = 0; i < p->size; i++) {
if (i == 0 || i == p->size - 1) {
if (i == 0) {
if (p->a[i] > p->a[i + 1]) {
ret_arr[ret_count++] = p->a[i];
}
}
if (i == p->size - 1) {
if (p->a[i] > p->a[i - 1]) {
ret_arr[ret_count++] = p->a[i];
}
}
} else {
if (p->a[i] > p->a[i - 1] && p->a[i] > p->a[i + 1]) {
ret_arr[ret_count++] = p->a[i];
}
}
}

printf("Printing Peek Elements:");


for(int i=0;i<ret_count;i++)
printf("%d ",ret_arr[i]);
}

int search(struct arr *p,int ele){


int flag=0;
int s_ele_pos;
for(int i=0;i<p->size;i++){
if(p->a[i]==ele){

Department of Computer Science and Engineering


Date:20/09/2023
Ex. No: 01

flag++;
s_ele_pos=i;
if(flag==2)
break;

}
}
if(flag==2)
return s_ele_pos;
else
return -1;
}

void deleteAt(struct arr *p,int pos){


int index=pos-1;
printf("Deleted %d\n",p->a[index]);
for(int i=index;i<p->size-1;i++){
p->a[i]=p->a[i+1];
}
p->size--;

void insertFront(struct arr *p,int ele){


printf("\nInserted %d at Front",ele);
for(int i=p->size-1;i>=0;i--){
p->a[i+1]=p->a[i];
}
p->a[0]=ele;
p->size++;

void insertAtEvery(struct arr *adt, int data) {

int newSize = adt->size * 2;


int *newArray = (int *)realloc(adt->a, newSize * sizeof(int));

for (int i = adt->size - 1; i >= 0; i--) {


newArray[i * 2 + 1] = newArray[i];
}

Department of Computer Science and Engineering


Date:20/09/2023
Ex. No: 01

for (int i = 0; i < newSize; i += 2) {


newArray[i] = data;
}

//adt->a = newArray;
adt->size = newSize;
printf("\nInserted %d before every element\n",data);
}

void insertLast(struct arr *p,int ele){


p->a[p->size]=ele;
p->size++;
printf("\nInserted %d at End",ele);

}
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);
}

void create(struct arr *p,int size,int a_entries[]){


p->size=size;
for(int i=0;i<p->size;i++){
p->a[i]=a_entries[i];

}
}

Output

Department of Computer Science and Engineering


Date:20/09/2023
Ex. No: 01

Learning Outcome

Department of Computer Science and Engineering

You might also like