Tuesday, November 21, 2017
Monday, November 13, 2017
Deleting every Nth node from a linked lint - Implementaion Using C - Interview Question
Scenario
Consider a game in which players are arranged circularly. The
game has to be played in a way demonstrared as follows.
1.) The games starts from first player as current player.
2.)A random number(n) from 1 to 10 is generated.
3.) *The count goes from the
current player and deletes
the
Nth player…
*After deleting the player,
now the game starts from the player
next to the deleted player…
*The game is to be stopped if there is only
one player and the
player is declared as a winner…
(Design
an algorithm for the game)
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int data;
struct Node *prev;
struct Node *next;
}Node;
Node *start=NULL;
void insert(Node *temp)
{
Node *var,*last;
Node *ptr=start;
if(start==NULL)
{
temp->next=temp;
temp->prev=temp;
start=temp;
}
else
{
while(ptr->next!=start)
{
ptr=ptr->next;
}
last=start->prev;
ptr->next=temp;
temp->prev=last;
last->next=temp;
temp->next=start;
start->prev=temp;
}
}
void display(Node *start)
{
Node *ptr=start;
if(start==NULL)
printf("\n No elements present");
else
{
printf("\n Elements in the Linked List are:");
while(ptr->next!=start)
{
printf("\n%d",ptr->data);
ptr=ptr->next;
}
printf("\n%d",ptr->data);
}
return;
}
int delete1(Node *start,int n)
{
Node *t=start,*p,*q;
int count;
while(t->next!=t->next->next)
{
count=1;
while(count<n)
{
t=t->next;
count++;
}
p=t->prev; q=t->next;
p->next=q; q->prev=p;
free(t);
t=q;
}
return t->data;
}
Node *createNode(int data)
{
Node *temp=(struct Node*)malloc(sizeof(struct Node));
temp->data=data;
return temp;
}
int main()
{
int data;
Node *temp;
temp=createNode(10);
insert(temp);
temp=createNode(20);
insert(temp);
temp=createNode(30);
insert(temp);
temp=createNode(40);
insert(temp);
temp=createNode(50);
insert(temp);
display(start);
data=delete1(start,4); /*Winner */
printf("\n Winner is :: %d",data);
return 0;
}
O/P: when N=4
Saturday, November 4, 2017
FLAMES - Finding realtionship between You both Using C
//flames using C
#include<stdio.h>
int main()
{
char name1[25],name2[25];
register int i,j;
static int count_fname,count_lname;
int sum=0;
char temp;
printf("\n Enter your name::");
scanf("%s",name1);
printf("\nEnter Miss/Mr 'X' Name::");
scanf("%s",name2);
i=0,j=0;
while(name1[i]!='\0') /*Sorting the first name*/
{
j=i+1;
while(name1[j]!='\0')
{
if(name1[i]>name1[j])
{
temp=name1[i];
name1[i]=name1[j];
name1[j]=temp;
}
j++;
}
i++;
count_fname=i;
}
printf("\n%s",name1);
i=0,j=0;
while(name2[i]!='\0') /*Sorting the second name*/
{
j=i+1;
while(name2[j]!='\0')
{
if(name2[i]>name2[j])
{
temp=name2[i];
name2[i]=name2[j];
name2[j]=temp;
}
j++;
}
i++;
count_lname=i;
}
printf("\n%s",name2);
i=0,j=0;
while(name1[i]!='\0' && name2[j]!='\0') /*Compare to get perfect count*/
{
if(name1[i]<name2[j])
{
i++;
}
else if(name1[i]>name2[j])
{
j++;
}
else if(name1[i]==name2[j])
{
count_fname=count_fname-1;
count_lname=count_lname-1;
i++;
j++;
}
}
printf("\n Value of count_fname:%d",count_fname);
printf("\n Value of count_fname:%d",count_lname);
sum=(count_fname+count_lname); //sum up remaining letters in two names*/
//sum=sum%6;
switch(sum)
{
case 0:
printf("\n Sorry Same Name found!!! else alike palindrome or anagram");
break;
case 1:
printf("\n -->Sister-->");
break;
case 2:
printf("\n -->Enemy-->");
break;
case 3:
printf("\n -->Friends-->");
break;
case 4:
printf("\n -->Enemy-->");
break;
case 5:
printf("\n -->Friends-->");
break;
case 6:
printf("\n -->You Gonna MARRY each other-->");
break;
case 7:
printf("\n -->Enemy-->");
break;
case 8:
printf("\n -->Affection-->");
break;
case 9:
printf("\n -->Enemy-->");
break;
case 10:
printf("\n Love");
case 11:
printf("\n You Gonna MARRY each other");
break;
case 12:
printf("\n Affection");
break;
case 13:
printf("\n Affection");
break;
case 14:
printf("\n Friends");
break;
case 15:
printf("\n You gonna marry each other");
break;
case 16:
printf("\n Friends");
break;
}
return 0;
}
#include<stdio.h>
int main()
{
char name1[25],name2[25];
register int i,j;
static int count_fname,count_lname;
int sum=0;
char temp;
printf("\n Enter your name::");
scanf("%s",name1);
printf("\nEnter Miss/Mr 'X' Name::");
scanf("%s",name2);
i=0,j=0;
while(name1[i]!='\0') /*Sorting the first name*/
{
j=i+1;
while(name1[j]!='\0')
{
if(name1[i]>name1[j])
{
temp=name1[i];
name1[i]=name1[j];
name1[j]=temp;
}
j++;
}
i++;
count_fname=i;
}
printf("\n%s",name1);
i=0,j=0;
while(name2[i]!='\0') /*Sorting the second name*/
{
j=i+1;
while(name2[j]!='\0')
{
if(name2[i]>name2[j])
{
temp=name2[i];
name2[i]=name2[j];
name2[j]=temp;
}
j++;
}
i++;
count_lname=i;
}
printf("\n%s",name2);
i=0,j=0;
while(name1[i]!='\0' && name2[j]!='\0') /*Compare to get perfect count*/
{
if(name1[i]<name2[j])
{
i++;
}
else if(name1[i]>name2[j])
{
j++;
}
else if(name1[i]==name2[j])
{
count_fname=count_fname-1;
count_lname=count_lname-1;
i++;
j++;
}
}
printf("\n Value of count_fname:%d",count_fname);
printf("\n Value of count_fname:%d",count_lname);
sum=(count_fname+count_lname); //sum up remaining letters in two names*/
//sum=sum%6;
switch(sum)
{
case 0:
printf("\n Sorry Same Name found!!! else alike palindrome or anagram");
break;
case 1:
printf("\n -->Sister-->");
break;
case 2:
printf("\n -->Enemy-->");
break;
case 3:
printf("\n -->Friends-->");
break;
case 4:
printf("\n -->Enemy-->");
break;
case 5:
printf("\n -->Friends-->");
break;
case 6:
printf("\n -->You Gonna MARRY each other-->");
break;
case 7:
printf("\n -->Enemy-->");
break;
case 8:
printf("\n -->Affection-->");
break;
case 9:
printf("\n -->Enemy-->");
break;
case 10:
printf("\n Love");
case 11:
printf("\n You Gonna MARRY each other");
break;
case 12:
printf("\n Affection");
break;
case 13:
printf("\n Affection");
break;
case 14:
printf("\n Friends");
break;
case 15:
printf("\n You gonna marry each other");
break;
case 16:
printf("\n Friends");
break;
}
return 0;
}
Thursday, November 2, 2017
Writing an Array of structures into a file & Reading a file into an Array of structures in c
Student Mark List Preparation
#include<stdio.h>
FILE *fp;
struct stud
{
int sid;
int sm[5];
int tot;
float avg;
int cls;
char pf;
};
void cal(struct stud s[],int);
void print(struct stud s[],int);
int main()
{
struct stud s[10];
int i,j,n;
printf("Enter the no.of students:");
scanf("%d",&n);
//s=(struct stud*)malloc(sizeof(struct stud)*n);
for(i=0;i<n;i++)
{
printf("Enter the student id:");
scanf("%d",&s[i].sid);
printf("enter 5 marks:");
for(j=0;j<5;j++)
scanf("%d",&s[i].sm[j]);
}
cal(s,n);
return 0;
}
void cal(struct stud s[],int n)
{
int i;
for(i=0;i<n;i++)
{
if(s[i].sm[0]>50 && s[i].sm[1]>50 && s[i].sm[2]>50 && s[i].sm[3]>50 && s[i].sm[4]>50)
s[i].pf='p';
else
s[i].pf='f';
if(s[i].pf=='p')
{
s[i].tot=(s[i].sm[0]+s[i].sm[1]+s[i].sm[2]+s[i].sm[3]+s[i].sm[4]);
s[i].avg=(s[i].tot)/5;
if(s[i].avg>70)
s[i].cls=1;
else if(s[i].avg>60 && s[i].avg<70)
s[i].cls=2;
else //(s[i].avg>50 && s[i].avg<60)
s[i].cls=3;
}
else
{
printf("FAIL");
}
}
print(s,n);
return;
}
void print(struct stud s[],int n)
{
int i,j;
fp =fopen("marklist.txt","w+");
for(i=0;i<n;i++)
{
fprintf(fp,"%d ",s[i].sid);
for(j=0;j<5;j++)
{
fprintf(fp,"%d ",s[i].sm[j]);
}
fprintf(fp,"%d ",s[i].tot);
fprintf(fp,"%f ",s[i].avg);
fprintf(fp,"%d ",s[i].cls);
fprintf(fp,"\n");
}
fclose(fp);
fp=fopen("marklist.txt","r");
printf("SID C COA DS RDBMS JAVA TOTAL AVG CLASS \n");
for(i=1;i<=n;i++)
{
j=0;
fscanf(fp,"%d %d %d %d %d %d %d %f %d",&s[i].sid,&s[i].sm[j],&s[i].sm[j+1],&s[i].sm[j+2],&s[i].sm[j+3],&s[i].sm[j+4],&s[i].tot,&s[i].avg,&s[i].cls);
printf("%d %d %d %d %d %d %d %f %d\n",s[i].sid,s[i].sm[j],s[i].sm[j+1],s[i].sm[j+2],s[i].sm[j+3],s[i].sm[4],s[i].tot,s[i].avg,s[i].cls);
}
fclose(fp);
return;
}
#include<stdio.h>
FILE *fp;
struct stud
{
int sid;
int sm[5];
int tot;
float avg;
int cls;
char pf;
};
void cal(struct stud s[],int);
void print(struct stud s[],int);
int main()
{
struct stud s[10];
int i,j,n;
printf("Enter the no.of students:");
scanf("%d",&n);
//s=(struct stud*)malloc(sizeof(struct stud)*n);
for(i=0;i<n;i++)
{
printf("Enter the student id:");
scanf("%d",&s[i].sid);
printf("enter 5 marks:");
for(j=0;j<5;j++)
scanf("%d",&s[i].sm[j]);
}
cal(s,n);
return 0;
}
void cal(struct stud s[],int n)
{
int i;
for(i=0;i<n;i++)
{
if(s[i].sm[0]>50 && s[i].sm[1]>50 && s[i].sm[2]>50 && s[i].sm[3]>50 && s[i].sm[4]>50)
s[i].pf='p';
else
s[i].pf='f';
if(s[i].pf=='p')
{
s[i].tot=(s[i].sm[0]+s[i].sm[1]+s[i].sm[2]+s[i].sm[3]+s[i].sm[4]);
s[i].avg=(s[i].tot)/5;
if(s[i].avg>70)
s[i].cls=1;
else if(s[i].avg>60 && s[i].avg<70)
s[i].cls=2;
else //(s[i].avg>50 && s[i].avg<60)
s[i].cls=3;
}
else
{
printf("FAIL");
}
}
print(s,n);
return;
}
void print(struct stud s[],int n)
{
int i,j;
fp =fopen("marklist.txt","w+");
for(i=0;i<n;i++)
{
fprintf(fp,"%d ",s[i].sid);
for(j=0;j<5;j++)
{
fprintf(fp,"%d ",s[i].sm[j]);
}
fprintf(fp,"%d ",s[i].tot);
fprintf(fp,"%f ",s[i].avg);
fprintf(fp,"%d ",s[i].cls);
fprintf(fp,"\n");
}
fclose(fp);
fp=fopen("marklist.txt","r");
printf("SID C COA DS RDBMS JAVA TOTAL AVG CLASS \n");
for(i=1;i<=n;i++)
{
j=0;
fscanf(fp,"%d %d %d %d %d %d %d %f %d",&s[i].sid,&s[i].sm[j],&s[i].sm[j+1],&s[i].sm[j+2],&s[i].sm[j+3],&s[i].sm[j+4],&s[i].tot,&s[i].avg,&s[i].cls);
printf("%d %d %d %d %d %d %d %f %d\n",s[i].sid,s[i].sm[j],s[i].sm[j+1],s[i].sm[j+2],s[i].sm[j+3],s[i].sm[4],s[i].tot,s[i].avg,s[i].cls);
}
fclose(fp);
return;
}
Subscribe to:
Posts (Atom)