Skip to main content

Hashing line segments C Program Code

Below the information to understand the program completely.

Input:
R = a rectangular region with bottom-left corner at (0,0) and top-right corner at (X,Y), X and Y being user-specified.
n = Number of line segments whose endpoints have integer coordinates and lie in R.
Task:  Use hashing to store the line segments one by one in a suitable data structure T. While generating, the two endpoints of each segment should be randomly selected and a newly generated segment should be inserted in T only if it is not already there in T.

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

typedef struct nodetype
{
int x1;
int x2;
int y1;
int y2;
struct nodetype *next;
}node;

node* lastnode(node *);
int findduplicate(node **,int,int,int,int,int);
void display(node **,int);

main()
{
int X,Y,x1,y1,x2,y2,n,i=0,j,c,exist,s=1;
node  **A=NULL,*ptr=NULL,*last=NULL;

printf(“*****************************************************************************\n”);
printf(“\t\tPROGRAM:\tHASHING LINE SEGMENTS\n”);
printf(“\t\tPROGRAMMER:\tKALPATARU MALLICK\n”);
printf(“*****************************************************************************\n”);

printf(“\n Enter the Second co-orinates of the Rectangle as integer value (X,Y) respectively :”);
scanf(“%d%d”,&X,&Y);
printf(“\n Enter the no. of segments must be less than or equals to (Y+1)^2*(Y+1)^2-2(X+1)*(Y+1) :\n”);
scanf(“%d”,&n);

A=(node **)malloc(sizeof(node *)*n);
for(j=0;j<n;j++)
A[j]=NULL;

while(i<n)
{
x1=rand()%(X+1);
y1=rand()%(Y+1);
x2=rand()%(X+1);
y2=rand()%(Y+1);
if((x1!=x2)||(y1!=y2))
{
exist = findduplicate(A,n,x1,x2,y1,y2);
if(exist==0)
{
i++;
ptr=(node *)malloc(sizeof(node));
ptr->next=NULL;

ptr->x1=x1;
ptr->x2=x2;
ptr->y1=y1;
ptr->y2=y2;

c=rand()%n;

if(A[c]==NULL)
{
A[c]=ptr;
}
else
{
last=lastnode(A[c]);
last->next=ptr;
}
}
}
}
printf(“\n\nThe cordinates of all segments are:\n “);
display(A,n);
printf(“\n”);
}

int findduplicate(node **A,int n,int x1,int x2,int y1,int y2)
{
int j;
node *head;
for(j=0;j<n;j++)
{
if (A[j] == NULL)
continue;
head=A[j];
while(head!=NULL)
{
if(((head->x1==x1)&&(head->y1==y1)&&(head->x2==x2)&&(head->y2==y2))||((head->x2==x2)&&(head->y2==y2)&&(head->x1==x1)&&(head->y1==y1)))
{
return 1;
}
head=head->next;
}
}
return 0;
}

node* lastnode(node *head)
{
while(head->next!=NULL)
{
head=head->next;
}
return head;
}

void display(node **A,int n)
{
node *head=NULL;
int k;
if(n==0)
printf(“\n no segments created\n”);
for(k=0;k<n;k++)
{
printf(“\n%d”,k+1);
if(A[k]!=NULL)
{
head=A[k];
while(head!=NULL)
{
printf(“\t(x1=%d”,head->x1);
printf(“\tx2=%d”,head->x2);
printf(“\ty1=%d”,head->y1);
printf(“\ty2=%d)”,head->y2);
head=head->next;
}
}
}
}

Note :The Above code is compiled on a linux environment 


Share your review and thoughts with us 

Comments

Popular posts from this blog

CIVIL SERVICES' (I.A.S.) EXAMINATION

The Union Public Service Commission (U.P.S.C.)  conducts Civil Services' Examination once a year in two stages. The Preliminary Examination (Objective Type) for selection of candidates for the Main Examination is held in the month of May. The Civil Services Main Examination  is held in the months of October/November. Blank application forms and other particulars are published in the Employment News, generally in the month of December. The last date for the submission of applications to the Secretary, Union Public Service Commission, Dholpur House, Shahjahan Road, NewDelhi-11001 1 is usually the last week of January of the year of examination. The Combined Civil Services Examination is conducted for Recruitment to the following Services/Posts: 1. Indian Administrative Service. 2. Indian Foreign Service. 3. Indian Police Service. 4. Indian P & T Accounts & Finance Service, Group 'A'. 5. Indian Audit and Accounts Service, Group 'A'. 6. Indian Customs and Centr

Predict the output or error(s) for the following:

1 . void main(){ int const * p=5; printf("%d",++(*p)); } Answer: Compiler error: Cannot modify a constant value. Explanation: p is a pointer to a "constant integer". But we tried tochange the value of the "constant integer". 2. main() {  char s[ ]="man"; int i;  for(i=0;s[ i ];i++) printf("\n%c%c%c%c",s[i],*(s+i),*(i+s),i[s]); } Answer: mmmm aaaa nnnn Explanation: s[i], *(i+s), *(s+i), i[s] are all different ways of expressing the same idea. Generally array name is the base address for that array. Here s is the base address. i is the index number/displacement from the base address. So, indirecting it with * is same as s[i]. i[s] may be surprising. But in the case of C it is same as s[i]. 3 . main(){  float me = 1.1;  double you = 1.1;  if(me==you) printf("I love U"); else printf("I hate U"); } Answer: I hate U Explanation : For floating point numbers (float, double, long double)

AGRICULTURAL SCIENTIST RECRUITMENT BOARD

Agricultural Research Service National   Eligibility  Test/ Senior Research Fellowship Examination  The Agricultural Scientists Recruitment Board (ASRB)  holds a Competitive Examination for recruiting Scientists of the ARS in the pay scale of Rs. 8,000-13,500 in the ICAR Institutes, combined with National Eligibility Test (NET) for recruitment of Lecturers and Assistant Professors by the State Agricultural Universities (SAUS) and for award of ICAR Senior Research Fellowships. The selected candidates for Agricultural Research Service must serve in the institutes to which they are posted until they find appointment for higher positions through selection at other institutes. (i) Candidates successful in ARS are appointed as Scientists in the Indian Council of Agricultural Research in the pay scale of Rs. 8,000-13,500. (ii) Candidates clearing the National Eligibility Test are recommended to various State Agricultural Universities who will consider them for appointment as Lecturers or A