You are on page 1of 22

REHA SHAH 21BCP148 G5 DAA LAB EXPERIMENT 4

Experiment 4
Design and Analysis of Algorithms Lab
20CP209P

AIM: To implement a city database using unordered lists. Each database record contains the
name of the city (a string of arbitrary length) and the coordinates of the city expressed as
integer x and y coordinates. Your program should allow following functionalities:
a) Insert a record,
b) Delete a record by name or coordinate,
c) Search a record by name or coordinate.
d)Print all records within a given distance of a specified point.
Implement the database using an array-based list implementation, and then a linked list
implementation. Perform following analysis:
a) Collect running time statistics for each operation in both implementations.
b) What are your conclusions about the relative advantages and disadvantages of the two
implementations?
c) Would storing records on the list in alphabetical order by city name speed any of the
operations?
d) Would keeping the list in alphabetical order slow any of the operations?

CODE: (in C)
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
struct city
{

1
REHA SHAH 21BCP148 G5 DAA LAB EXPERIMENT 4

char name[40];
int x;
int y;
};

struct node
{
struct city* data;
struct node* link;
};

int menu()
{
int c;
printf("\navailable functionalities:\n1.insert a record\n2.delete a record by name\n3.delete a
record by coordinate\n4.search a record by name\n5.search a record by coordinate\n6.print all
records in given locality\nenter choice: ");
scanf("%d",&c);
return(c);
}

struct city* insert()


{
struct city* p;
p=(struct city*)malloc(sizeof(struct city));
printf("enter name of city (max.39 characters, no spaces): ");
scanf("%s",p->name);
printf("enter x coordinate: ");
scanf("%d",&(p->x));
printf("enter y coordinate: ");
scanf("%d",&(p->y));

2
REHA SHAH 21BCP148 G5 DAA LAB EXPERIMENT 4

return(p);
};

void linked_list()
{
int f=1,c,e,x,y,i,a,b;
char n[40];
struct node* l;
struct node* t;
l=NULL;
while(f)
{
c=menu();
if(c==1)
{
t=(struct node*)malloc(sizeof(struct node));
t->data=insert();
t->link=l;
l=t;
printf("city has been inserted into the database successfully.\n");
}
else if(c==2)
{
printf("enter name of record to be deleted (max.40 characters,no spaces): ");
scanf("%s",n);
if(strcmp(l->data->name,n)==0)
{
l=l->link;
printf("city has been successfully deleted from database.\n");
}

3
REHA SHAH 21BCP148 G5 DAA LAB EXPERIMENT 4

else
{
e=0;
t=l;
while(t->link!=NULL)
{
if(strcmp(t->link->data->name,n)==0)
{
t->link=t->link->link;
printf("city has been successfully deleted from database.\n");
e=1;
break;
}
t=t->link;
}
if(e==0)
{
printf("no city with %s name in database!\n",n);
}
}
}
else if(c==3)
{
printf("enter x coordinate of record to be deleted: ");
scanf("%d",&x);
printf("enter y coordinate of record to be deleted: ");
scanf("%d",&y);
if((l->data->x==x)&&(l->data->y==y))
{
l=l->link;

4
REHA SHAH 21BCP148 G5 DAA LAB EXPERIMENT 4

printf("city has been successfully deleted from database.\n");


}
else
{
e=0;
t=l;
while(t->link!=NULL)
{
if((t->link->data->x==x)&&(t->link->data->y==y))
{
t->link=t->link->link;
printf("city has been successfully deleted from database.\n");
e=1;
break;
}
t=t->link;
}
if(e==0)
{
printf("no city with %d,%d coordinates in database!\n",x,y);
}
}
}
else if(c==4)
{
printf("enter name of record to be searched (max.40 characters): ");
scanf("%s",n);
e=0;
t=l;
i=0;

5
REHA SHAH 21BCP148 G5 DAA LAB EXPERIMENT 4

while(t!=NULL)
{
i++;
if(strcmp(t->data->name,n)==0)
{
printf("city found in database at %d position and its coordinates are %d,%d.\
n",i,t->data->x,t->data->y);
e=1;
break;
}
t=t->link;
}
if(e==0)
{
printf("no city with %s name in database!\n",n);
}
}
else if(c==5)
{
printf("enter x coordinate of record to be searched: ");
scanf("%d",&x);
printf("enter y coordinate of record to be searched: ");
scanf("%d",&y);
e=0;
t=l;
i=0;
while(t!=NULL)
{
i++;
if((t->data->x==x)&&(t->data->y==y))
{

6
REHA SHAH 21BCP148 G5 DAA LAB EXPERIMENT 4

printf("city found in database at %d position and its name is %s.\n",i,t->data-


>name);
e=1;
break;
}
t=t->link;
}
if(e==0)
{
printf("no city with %d,%d coordinates in database!\n",x,y);
}
}
else if(c==6)
{
printf("enter x coordinate of reference point: ");
scanf("%d",&x);
printf("enter y coordinate of reference point: ");
scanf("%d",&y);
printf("enter maximum distance allowed: ");
scanf("%d",&i);
t=l;
printf("cities in database in given locality:\n");
e=0;
while(t!=NULL)
{
a=t->data->x;
b=t->data->y;
if((sqrt((double)((x-a)*(x-a)+(y-b)*(y-b))))<=i)
{
printf("%s - %d,%d\n",t->data->name,a,b);
e+=1;

7
REHA SHAH 21BCP148 G5 DAA LAB EXPERIMENT 4

}
t=t->link;
}
if(e==0)
printf("no such city found!\n");
}
else
printf("invalid input.\n");
printf("do you want to continue?\nenter 0 if no else enter any integer: ");
scanf("%d",&f);
}
}

void array()
{
struct city* l[100];
char n[40];
int f=1,x,y,c,i,e,h,a,b;
for(i=0;i<100;i++)
l[i]=NULL;
while(f)
{
c=menu();
if(c==1)
{
i=0;
while(l[i]!=NULL)
i++;
l[i]=insert();
printf("city has been inserted into the database successfully.\n");

8
REHA SHAH 21BCP148 G5 DAA LAB EXPERIMENT 4

}
else if(c==2)
{
printf("enter name of record to be deleted (max.40 characters): ");
scanf("%s",n);
e=0;
i=0;
while((l[i]!=NULL)&&(i!=100))
{
if(strcmp(l[i]->name,n)==0)
{
e=1;
break;
}
i++;
}
if(e==1)
{
i--;
while((l[i]!=NULL)&&(i!=99))
{
l[i]=l[i+1];
i++;
}
l[99]=NULL;
}
else
printf("no city with %s name in database!\n",n);
}
else if(c==3)

9
REHA SHAH 21BCP148 G5 DAA LAB EXPERIMENT 4

{
printf("enter x coordinate of record to be deleted: ");
scanf("%d",&x);
printf("enter y coordinate of record to be deleted: ");
scanf("%d",&y);
e=0;
i=0;
while((l[i]!=NULL)&&(i!=100))
{
if((l[i]->x==x)&&(l[i]->y)==y)
{
e=1;
break;
}
i++;
}
if(e==1)
{
i--;
while((l[i]!=NULL)&&(i!=99))
{
l[i]=l[i+1];
i++;
}
l[99]=NULL;
}
else
printf("no city with %d,%d coordinates in database!\n",x,y);
}
else if(c==4)

10
REHA SHAH 21BCP148 G5 DAA LAB EXPERIMENT 4

{
printf("enter name of record to be searched (max.40 characters): ");
scanf("%s",n);
e=0;
i=0;
while((l[i]!=NULL)&&(i!=100))
{
if(strcmp(l[i]->name,n)==0)
{
printf("city found in database at %d position and its coordinates are %d,%d.\
n",i+1,l[i]->x,l[i]->y);
e=1;
break;
}
i++;
}
if(e==0)
printf("no city with %s name in database!\n",n);
}
else if(c==5)
{
printf("enter x coordinate of record to be searched: ");
scanf("%d",&x);
printf("enter y coordinate of record to be searched: ");
scanf("%d",&y);
e=0;
i=0;
while((l[i]!=NULL)&&(i!=100))
{
if((l[i]->x==x)&&(l[i]->y)==y)
{

11
REHA SHAH 21BCP148 G5 DAA LAB EXPERIMENT 4

printf("city found in database at %d position and its name is %s.\n",i+1,l[i]-


>name);
e=1;
break;
}
i++;
}
if(e==0)
printf("no city with %d,%d coordinates in database!\n",x,y);
}
else if(c==6)
{
printf("enter x coordinate of reference point: ");
scanf("%d",&x);
printf("enter y coordinate of reference point: ");
scanf("%d",&y);
printf("enter maximum distance allowed: ");
scanf("%d",&h);
i=0;
printf("cities in database in given locality:\n");
e=0;
while(l[i]!=NULL)
{
a=l[i]->x;
b=l[i]->y;
if((sqrt((double)((x-a)*(x-a)+(y-b)*(y-b))))<=h)
{
printf("%s - %d,%d\n",l[i]->name,a,b);
e+=1;
}
i++;

12
REHA SHAH 21BCP148 G5 DAA LAB EXPERIMENT 4

}
if(e==0)
printf("no such city found!\n");
}
else
printf("invalid input.\n");
printf("do you want to continue?\nenter 0 if no else enter any integer: ");
scanf("%d",&f);
}
}

void main()
{
int c;
printf("available choices:\n1.array-based implementation\n2.linked-list based
implementation\nenter choice: ");
scanf("%d",&c);
if(c==2)
linked_list();
else if(c==1)
array();
else
printf("invalid choice....program terminating...");
}

OUTPUT:
Array Implementation:

13
REHA SHAH 21BCP148 G5 DAA LAB EXPERIMENT 4

14
REHA SHAH 21BCP148 G5 DAA LAB EXPERIMENT 4

15
REHA SHAH 21BCP148 G5 DAA LAB EXPERIMENT 4

16
REHA SHAH 21BCP148 G5 DAA LAB EXPERIMENT 4

17
REHA SHAH 21BCP148 G5 DAA LAB EXPERIMENT 4

Linked List Implementation:

18
REHA SHAH 21BCP148 G5 DAA LAB EXPERIMENT 4

19
REHA SHAH 21BCP148 G5 DAA LAB EXPERIMENT 4

20
REHA SHAH 21BCP148 G5 DAA LAB EXPERIMENT 4

ANALYSIS:
a) Running Time Statistics:
Linked List Implementation:

21
REHA SHAH 21BCP148 G5 DAA LAB EXPERIMENT 4

1. Insertion: O(1)
2. Deletion: O(n)
3. Search: O(n)
Array Implementation:
1. Insertion: O(n)
2. Deletion: O(n)
3. Search: O(n)

b) Advantages and Disadvantages:


Linked List Implementation:
 Advantages: Insertion is faster and there is no limit to the number of cities we can
store.
 Disadvantages: It is more complex and has more space complexity.
Array Implementation:
 Advantages: Simpler and uses less space.
 Disadvantages: Insertion takes more time and number of cities allowed is constant
(static).

c) No, alphabetical arrangement would not speed up any operation.

d) Yes, it will slow down Insertion in both implementations.

22

You might also like