You are on page 1of 32

Chap10[D]a Printing specfic datas in structures

Create a structure to specify data on students given below: Roll number, Name,
Department, Course, Year of joining Assume that there are not more than 450
students in the collage. (a) Write a function to print names of all students who
joined in a particular year. (b) Write a function to print the data of a student
whose roll number is given.

void main()
{
char j[1];
int yearc,rollc,i;
struct book
{
int roll;
char name[20];
char department[20];
char course[20];
int year;
};
struct book b[5]={
{1,"MrA","Science","Physics",2009},
{2,"MrB","Science","Maths",2008},
{3,"MrC","Arts","History",2009},
{4,"MrsD","Arts","History",2010},
{5,"MrsE","Science","Maths",2009}
};
while(1)
{
clrscr();
printf("\nEnter a year(2000 or 2001)\n");
scanf("%d",&yearc);
for(i=0;i<5;i++)
{
if(yearc==b[i].year)
printf("\nName: %s Year: %d",b[i].name,b[i].year);
}
printf("\nEnter a roll number(1-5)\n");

scanf("%d",&rollc);
for(i=0;i<5;i++)
{
if(rollc==b[i].roll)
{
printf("\nRoll number: %d, Name: %s, Department: %s\nCourse: %s,Year:
%d",b[i].roll,b[i].name,b[i].department,b[i].course,b[i].year);
break;
}
}
printf("\nPress q to quit or any key to continue\n");
scanf("%s",j);
if(j[0]==113)
break;
}
}
linkfloat()
{
float a=0,*b;
b=&a;
a=*b;
}

Chap10[D]c Automobile has weird serial numbers


An automobile company has serial number for engine parts starting from AA0 to
FF9. The other characteristics of parts to be specified in a structure are: Year of
manufacture, material and quantity manufactured.
(a) Specify a structure to store information corresponding to a part.
(b) Write a program to retrieve information on parts with serial numbers between
BB1 and CC6.
***When keying in the parts key alphabets in uppercase.
void main()
{
int i,j,letter,a,b,c,d;
char m[3],n[3],q[1];
struct parts

{
int year;
char material[20];
int qty;
};
struct parts p[27]={
{1991,"rubber",5},{1992,"rubber",6},{1991,"metal",7},{1997,"wood",8},
{1993,"plastic",9},{1992,"wood",6},{1997,"metal",6},{1992,"rubber",7},
{1991,"wood",2},
{1992,"metal",6},{1995,"wood",4},{1992,"rubber",5},{1998,"wood",2},
{1997,"plastic",8},{1998,"plastic",9},{1998,"metal",8},{1991,"metal",8},
{1992,"wood",5},
{1993,"wood",8},{1993,"rubber",7},{1997,"metal",2},{1998,"wood",1},
{1999,"plastic",8},{1995,"wood",6},{1990,"rubber",3},{1997,"plastic",9},
{1998,"wood",7}
};
/* for(i=0,letter=65;i<2;i++,letter++)
{
for(j=0;j<9;j++)
{
printf("\Key in values for year,material and quantity respectively for part %c%c
%d\n",letter,letter,j+1);
scanf("%d%s%d",&p[i][j].year,&p[i][j].material,&p[i][j].qty);
}
} */
while(1)
{
clrscr();
printf("\nKey in the part number to retrieve the information from\n");
scanf("%s",m);
printf("\nKey the part number till the information is retrieved(End)\n");
scanf("%s",n);
a=((m[0]-65)*9+(m[2]-49));
b=((n[0]-65)*9+(n[2]-49));
printf("\na is %d,\nb is %d,\nm[0] is %d, n[0] is %d",a,b,m[0],n[0]);
c=a;
d=a;
while(c>=9)
{

c-=9;
}
printf("\nc after while is %d",c);
while( d>9)
d-=9;

for(i=a;i<=b;i++,c++,d++)
{
if(c==9)
{
m[0]+=1;
c=0;
}
if(d==9)
{
d=0;
}
printf("\nPart: %c%c%d, Year: %d, Material: %s, Quantity:
%d",m[0],m[0],d+1,p[i].year,p[i].material,p[i].qty);
}

printf("\nKey q to quit or any other key to continue\n");


scanf("%s",q);
if(q[0]==113)
break;
}
}

Chap10[D]e Displaying data in structure with condition


There is a structure called employee that holds information like employee code,
name, date of joining. Write a program to create an array of the structure and
enter some data into it. Then ask the user to enter current date. Display the names
of those employees whose tenure is 3 or more than 3 years according to the given
current date.

This program doesn't check whether your date is correct so even if you put 99990001 it is
still valid.
void main()
{
int i,k;
char date[8];
char j[1];
struct em
{
int code;
char name[20];
char datej[8];
};
struct em e[5];
for(i=0;i<5;i++)
{
printf("\nKey in code, name and date(ddmmyyy) of employment for %d) employee\n",i+1);
scanf("%d%s%s",&e[i].code,e[i].name,e[i].datej);
if(strlen(e[i].datej)!=8)
{
printf("\nYou keyed the date wrongly, key again");
i--;
continue;
}
}
while(1)
{
clrscr();
printf("\nEnter current date\n");
scanf("%s",date);

for(i=0;i<5;i++)
{
k=0;
k=funccheck(date,e[i].datej);
if(k==1)
printf("\nEmployee code= %d Employee name= %s has tenure more than or equal 3

years",e[i].code,e[i].name);
}
printf("\nKey q to quit or any other key to continue\n");
scanf("%s",j);
if(j[0]==113)
break;
}
}
funccheck(char *date,char *datej)
{
int a,a1,i,t;
for(i=7,t=1,a=0;i>3;i--,t*=10)
{
a=a+((*(date+i)-48)*t);
}
for(i=7,t=1,a1=0;i>3;i--,t*=10)
{
a1=a1+((*(datej+i)-48)*t);
}
if(a-a1>3)
return(1);
else if(a-a1<0)
return(0);
for(i=3,a=0,t=1;i>1;i--,t*=10)
{
a=a+((*(date+i)-48)*t);
}
for(i=3,a1=0,t=1;i>1;i--,t*=10)
{
a1=a1+((*(datej+i)-48)*t);
}
if(a-a1>0)
return(1);
else if(a-a1<0)
return(0);

for(i=1,a=0,t=1;i>=0;i--,t*=10)
{
a=a+((*(date+i)-48)*t);
}
for(i=1,a1=0,t=1;i>=0;i--,t*=10)
{
a1=a1+((*(datej+i)-48)*t);
}
if(a-a1>=0)
return(1);
else if(a-a1<0)
return(0);
}

Chap10[D]g Use structure to check dates


Write a program that compares two given dates. To store date use structure say
date that contains three members namely date, month and year. If the dates are
equal then display message as "Equal" otherwise "Unequal".

void main()
{
int i,f=0;
struct date
{
int date;
int month;
int year;
};
struct date d[2];
for(i=0;i<2;i++)
{
printf("\nEnter day for the %d) date\n",i+1);
scanf("%d",&d[i].date);
printf("\nEnter the month for the %d) date\n",i+1);
scanf("%d",&d[i].month);
printf("\nEnter the year for the %d) date\n",i+1);
scanf("%d",&d[i].year);

}
if(d[0].date==d[1].date)
{
if(d[0].month==d[1].month)
{
if(d[0].year==d[1].year)
{
f=1;
}
}
}
if(f==1)
printf("\nThe dates are equal");
else
printf("\nThe dates are not equal");
}

(a) Create a structure to specify data on students given


below:
Roll number, Name, Department, Course, Year of joining
Assume that there are not more than 450 students in the
collage.
(a) Write a function to print names of all students who
joined in a particular year.
(b) Write a function to print the data of a student whose
roll number is given.
Solution:
/* NOTE: since number of students to be assumed is too much ( about 450 )
I have alloted full size (about 450) to it but array has been kept empty,
if you have time then you can fill up all 450 names and can search through
them.program has been tested by me and it works accurately. you can reduce
the size of array of structure conveniently by changing the value of N */
#include<stdio.h>
#include<conio.h>

#define N 450
struct students {
int rlnm;
char name[25];
char dept[25]; /* structure defined outside of main(); */
char course[25];
int year;
};
void main() {

/* main() */

struct students s[N];


int i,ch;
clrscr();

/* taking input of 450 students in an array of structure */


for(i=0;i<N;i++) {
printf(" Enter data of student %d\t\t\t\ttotal students: %d\n",i+1,N);
printf("****************************\n\n");
printf("enter rollnumber: ");
scanf("%d",&s[i].rlnm);
printf("\n\nenter name: ");
scanf(" %s",&s[i].name);
printf("\n\nenter department: ");
scanf("%s",&s[i].dept);
printf("\n\nenter course: ");
scanf("%s",&s[i].course);
printf("\n\nenter year of joining: ");
scanf("%d",&s[i].year);
clrscr();
}
/* displaying a menu */

printf("\n\tenter your choice: \n");


printf("\t**********************\n\n");
printf("1: enter year to search all students who took admission in that:\n\n");
printf("2: enter roll number to see details of that student\n\n\n");
printf("your choice: ");
scanf("%d",&ch);

/* taking input of your choice */

clrscr();
switch(ch) {
case 1:
clrscr();
dispyr(&s); /* function call to display names of students who joined in\
a particular year */
break;
case 2:
clrscr();
disprl(&s); /* function call to display information of a student \
whose roll number is given */
break;
default:
printf("\n\nerror! wrong choice");
}
getch();
}
/******************* main() ends **************/
dispyr(struct students *a) { /* function for displaying names of students\
who took admission in a particular year */
int j,yr;
printf("\nenter year: ");

scanf("%d",&yr);
printf("\n\nthese students joined in %d\n\n",yr);
for(j=0;j<N;j++) {
if(a[j].year==yr) {
printf("\n%s\n",a[j].name);
}
}
return 0;
}

disprl(struct students *a) {


/* function to print information of a\
student whose roll number has been \
given. */
int k,rl;
printf("\nenter roll number: ");
scanf("%d",&rl);
for(k=0;k<N;k++) {
if(a[k].rlnm==rl) {
printf("\n\n\t Details of roll number: %d\n",a[k].rlnm);
printf("\t***************************\n\n");
printf("
Name: %s\n",a[k].name);
printf(" Department: %s\n",a[k].dept);
printf("
Course: %s\n",a[k].course);
printf("Year of joining: %d",a[k].year);
break;
}
else {
printf("\nRoll number you entered does not exist\n\n");
break;
}
}

return 0;
}

--------------------------------------------------------------------------------------------------------------

(b) Create a structure to specify data of customers in a


bank. The data to be stored is: Account number, Name,
Balance in account. Assume maximum of 200 customers
in the bank.
(a) Write a function to print the Account number and
name of each customer with balance below Rs. 100.
(b) If a customer request for withdrawal or deposit, it is
given in the form:
Acct. no, amount, code (1 for deposit, 0 for withdrawal)
Write a program to give a message, The balance is
insufficient for the specified withdrawal.
Solution:
/* NOTE: since number of customers to be assumed is too much ( about 200 )
I have alloted full size (about 200) to it but array has been kept empty,
if you have time then you can fill up all 200 names and can search through
them.program has been tested by me and it works accurately. you can reduce
the size of array of structure conveniently by changing the value of N */
#include<stdio.h>
#include<conio.h>
#define N 200
struct bank {
int acn;
char name[20];
int bal;
/* defined out of main() */
};

void main() {
struct bank b[N];
int i,ch,lw=100,ch2,ac,am;
clrscr();
for(i=0;i<N;i++) {

/* inputting customer data */

printf("\tEnter information of customers \n");


printf("\t******************************\n\n");
printf("enter account no.: ");
scanf("%d",&b[i].acn);
printf("\n\nenter customer name: ");
scanf("%s",&b[i].name);
printf("\n\nenter balance: ");
scanf("%d",&b[i].bal);
clrscr();
}
clrscr();
printf("\tEnter your choice\n");
printf("\t*****************\n\n");

/* further processing of transaction */

printf("1: to know whose balance is below 100.\n\n");


printf("2: to process request or withdrawl.\n\n\n");

scanf("%d",&ch);
switch(ch) {
case 1:
clrscr();
disp(&b);
break;

/* displaying whose balance is below 100 */

case 2:
clrscr();
printf("enter your account number: ");
scanf("%d",&ac);
for(i=0;i<N;i++) {
if((b[i].acn)==ac) {
clrscr();
printf("\tHello %s\n",b[i].name);
printf("\n\n");
printf("\n\nenter your choice\n");
printf("\n1: deposite:\n");
printf("\n0: withdrawl:\n\n");
scanf("%d",&ch2);
switch(ch2) {

case 0:
clrscr();
if(b[i].bal<lw) {
printf("\n\nsorry! account balance is too low for withdrawl.\n");
break;
}
else {
printf("\n\nenter amount for withdrawl: ");
scanf("%d",&am);
}
if(b[i].bal<am) {

printf("\n\nyou don't have enough balance for withdrawl.\n");

else {
b[i].bal=b[i].bal+am;
printf("\n\nwithdrawl was successful.\n");
}
break;
case 1:
clrscr();
printf("\n\nenter amount to deposite: ");
scanf("%d",&am);
b[i].bal=b[i].bal+am;
printf("\n\ncash deposited successfully.\n\n");
break;
}
}
}
}
getch();
}
disp(struct bank *a) {
int k;
printf("\tCustomers whose balance is below 100:\n");
printf("\t*************************************\n\n");
for(k=0;k<N;k++) {

if((a[k].bal)<100) {
printf("%2d\t%s\n",a[k].acn,a[k].name);
}
}
return 0;
}

-----------------------------------------------------------------------------------------------------------------

(c) An automobile company has serial number for engine


parts starting from AA0 to FF9. The other characteristics
of parts to be specified in a structure are: Year of
manufacture, material and quantity manufactured.
(a) Specify a structure to store information corresponding
to a part.
(b) Write a program to retrieve information on parts with
serial numbers between BB1 and CC6.
Solution:
#include<stdio.h>
#include<conio.h>
struct automob {
char name[5];
int year;
char mtr[10];
int qty;
};
void main() {
struct automob a[54]={{"AA1",2009,"AAA",10},
{"AA2",2009,"AAA",10},
{"AA3",2009,"AAA",10},
{"AA4",2009,"AAA",10},
{"AA5",2009,"AAA",10},

{"AA6",2009,"AAA",10},
{"AA7",2009,"AAA",10},
{"AA8",2009,"AAA",10},
{"AA9",2009,"AAA",10},
{"BB1",2010,"BBB",11},
{"BB2",2010,"BBB",11},
{"BB3",2010,"BBB",11},
{"BB4",2010,"BBB",11},
{"BB5",2010,"BBB",11},
{"BB6",2010,"BBB",11},
{"BB7",2010,"BBB",11},
{"BB8",2010,"BBB",11},
{"BB9",2010,"BBB",11},
{"CC1",2011,"CCC",12},
{"CC2",2011,"CCC",12},
{"CC3",2011,"CCC",12},
{"CC4",2011,"CCC",12},
{"CC5",2011,"CCC",12},
{"CC6",2011,"CCC",12},
{"CC7",2011,"CCC",12},
{"CC8",2011,"CCC",12},
{"CC9",2011,"CCC",12},
{"DD1",2012,"DDD",13},
{"DD2",2012,"DDD",13},
{"DD3",2012,"DDD",13},
{"DD4",2012,"DDD",13},
{"DD5",2012,"DDD",13},
{"DD6",2012,"DDD",13},
{"DD7",2012,"DDD",13},
{"DD8",2012,"DDD",13},
{"DD9",2012,"DDD",13},
{"EE1",2013,"EEE",14},
{"EE2",2013,"EEE",14},
{"EE3",2013,"EEE",14},
{"EE4",2013,"EEE",14},
{"EE5",2013,"EEE",14},
{"EE6",2013,"EEE",14},
{"EE7",2013,"EEE",14},
{"EE8",2013,"EEE",14},
{"EE9",2013,"EEE",14},
{"FF1",2014,"FFF",15},
{"FF2",2014,"FFF",15},
{"FF3",2014,"FFF",15},
{"FF4",2014,"FFF",15},
{"FF5",2014,"FFF",15},
{"FF6",2014,"FFF",15},

{"FF7",2014,"FFF",15},
{"FF8",2014,"FFF",15},
{"FF9",2014,"FFF",15}};

int i,j,k;
clrscr();

printf("Information of parts between BB1 to CC6:\n\n");


printf("\n\nName\t\tyear(manu.)\t\tmaterial\tquantity\n\n\n");
for(i=0;i<54;i++) {

if(i>=9 && i<=23) {


printf("%3s\t\t%4d\t\t\t%5s\t\t%4d\n",a[i].name,a[i].year,a[i].mtr,a[i].qty);

}
}

getch();
}
-----------------------------------------------------------------------------------------------------------------

(d) A record contains name of cricketer, his age, number


of test matches that he has played and the average runs
that he has scored in each test match. Create an array of
structure to hold records of 20 such cricketer and then
write a program to read these records and arrange them
in ascending order by average runs. Use the qusort( )
standard library function.
Solution:
#include<stdio.h>

#include<conio.h>
#include<string.h>

void main() {
struct cricketer {
char name[50];
int age;
int match;
float avrn;
};
struct cricketer c[20],temp;
int i,j;
clrscr();
for(i=0;i<20;i++) {
printf("Enter data of cricketer %d\n\n\n",i+1);
fflush(stdin);
printf("Name: ");
gets(c[i].name);
printf("\n\nAge: ");
scanf("%d",&c[i].age);
printf("\n\nMatches: ");
scanf("%d",&c[i].match);
printf("\n\nAverage runs: ");
scanf("%f",&c[i].avrn);
clrscr();
}
/*******************/
/* sorting records */
/*******************/

for(i=0;i<20;i++) {
for(j=i+1;j<20;j++) {

if(c[i].avrn > c[j].avrn) {


temp=c[i];
c[i]=c[j];
c[j]=temp;
}
}
}
printf("Sorted records:\n\n");
for(i=0;i<20;i++) {
printf("%d\t%s\t%d\t%d\t%f\n\n\n",i+1,c[i].name,c[i].age,c[i].match,c[i].avrn);
}
getch();
}
linkfloat() {
float a=0,*b;
b=&a;
a=*b;
return 0;
}
-----------------------------------------------------------------------------------------------------------------

(e) There is a structure called employee that holds


information like employee code, name, date of joining.
Write a program to create an array of the structure and
enter some data into it. Then ask the user to enter
current date. Display the names of those employees
whose tenure is 3 or more than 3 years according to the
given current date.
Solution:

#include<stdio.h>
#include<conio.h>
#define N 3
void main() {

struct employee {
int code;
char name[20];
int dd,mm,yy;
} e[N];
int d,m,y,i;
clrscr();
for(i=0;i<N;i++) {
printf("\tEnter employee data:\n");
printf("\t*********************\n");
printf("\nEnter employee code: ");
scanf("%d",&e[i].code);
printf("\n\nEnter employee name: ");
scanf("%s",&e[i].name);
printf("\n\nEnter date of joining (dd mm yy): ");
scanf("%d%d%d",&e[i].dd,&e[i].mm,&e[i].yy);
clrscr();
}
clrscr();
printf("\nenter current date(dd mm yy): \n\n");
scanf("%d%d%d",&d,&m,&y);
clrscr();
for(i=0;i<N;i++) {
if((y-(e[i].yy))>=3) {
printf("%s\n\N",e[i].name);

}
getch();
}
-----------------------------------------------------------------------------------------------------------------

(f) Write a menu driven program that depicts the working


of a library. The menu options should be:
1. Add book information
2. Display book information
3. List all books of given author
4. List the title of specified book
5. List the count of books in the library
6. List the books in the order of accession number
7. Exit
Create a structure called library to hold accession
number, title of the book, author name, price of the
book, and flag indicating whether book is issued or not.
Solution:
#include<stdio.h>
#include<conio.h>
#include<dos.h>

void main() {
struct library {
int acn;
char title[25];

char auth[25];
float price;
int flag;
};
struct library a[5]={
{2,"AAA","AAA",154.8,1},
{1,"BBB","BBB",124.6,0},
{5,"EEE","EEE",234.3,0},
{3,"CCC","CCC",232.3,1},
{4,"DDD","DDD",121.3,0}
};
struct library temp; /* temporary structure to overwrite information /
and for assistance in sorting the whole structure */

int i,ch,k,flag,j=0;
char author[10];
clrscr();
while(1) {

/* initialized an indefinite loop */

clrscr();
printf("\t1: Add book information\n");
printf("\t2: Display book information\n");
printf("\t3: List all books of given author\n");
printf("\t4: List the title of specified book\n");
printf("\t5: List the count of books in the library\n");
printf("\t6: List the books in order of accesion number\n");
printf("\t7: Exit\n\n\n");
printf("Choice: ");
scanf("%d",&ch);
switch(ch) {
case 1:

/* adding book information over current information */

clrscr();
printf("\t\t1: Add book information:\n\n\n");

printf("enter book information:\n\n");


printf("Accesion number of book to be modified: ");
scanf("%d",&j); fflush(stdin);
for(i=0;i<5;i++) {
if(j==a[i].acn) {
printf("\n\nenter new information:\n\n");
printf("accesion no.: ");
scanf("%d",&temp.acn); fflush(stdin);
printf("\n\ntitle: ");
scanf("%s",&temp.title); fflush(stdin);
printf("\n\nauthor: ");
scanf("%s",&temp.auth); fflush(stdin);
printf("\n\nprice: ");
scanf("%f",&temp.auth); fflush(stdin);
printf("\n\nflag(1/0): ");
scanf("%d",&temp.flag); fflush(stdin);
}
}
a[j]=temp; /* overwriting current information with new one */
clrscr();
printf("\n\nInformation added succesfully!\n\n");
delay(2000);
break;

case 2:

/* displaying current book information */

clrscr();
printf("\t\t2: Display book information:\n\n\n");
printf("Enter accesion number: ");
scanf("%d",&k);
for(i=0;i<5;i++) {

if(k==a[i].acn) {
clrscr();
printf("Book information: \n\n");
printf("\n\nAccesion Number: %d",a[i].acn);
printf("\nTitle: %s",a[i].title);
printf("\nAuthor: %s",a[i].auth);
printf("\nPrice: %f",a[i].price);
if(a[i].flag==0) {
printf("\nFlag: Not issued\n\n");
}
else{
printf("\nFlag: Issued\n\n");
}
}
}
delay(1000);
break;

case 3: /* listing all books of given author */

clrscr();
printf("\t\t3: List all books of given author:\n\n\n");
printf("Enter author name: ");
scanf("%s",&author); fflush(stdin);
printf("\n\nbooks of %s\n\n",author);
for(i=0;i<5;i++) {
k=strcmp(author,a[i].auth);
if(k==0) {
j=j+1;
printf("%d.\t%s\n",j,a[i].title);
}
}
delay(2000);
break;

case 4:

/* displaying title of given book */

clrscr();
printf("\t\t4: Title of the specified book:\n\n\n");
printf("Enter the accesion number: ");
scanf("%d",&k);
printf("\n\n");
for(i=0;i<5;i++) {
if(k==a[i].acn) {
printf("Title: %s\n",a[i].title);
}
}
delay(2000);
break;

case 5:

/* counting total books in library */

clrscr();
printf("\t\t5: List the count of books in the library: \n\n\n");
for(i=0;i<5;i++) {
j=j+1;
}
printf("\tTotal books: %2d",j);
delay(2000);
break;

case 6: /* sorting the books by their accesion numbers */

clrscr();
printf("6: List the books in order of accesion number: \n\n\n");
for(i=0;i<5;i++) {

for(j=i+1;j<4;j++) {
if(a[i].acn>a[j].acn) {
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("Access no.\tTitle\tAuthor\tFlag\n\n\n");
for(i=0;i<5;i++) {
printf("%4d\t\t%5s\t%5s\t",a[i].acn,a[i].title,a[i].auth);
if(a[i].flag==0)
printf("not issued\n\n");
else
printf("issued\n\n");
}
delay(2000);
break;

case 7:

/* condition for going out */

exit();
}
}
}
linkfloat() { /* special function to solve compilar error */
float a=0,*b; /* not used withing the program but still defined */
b=&a;
a=*b;
return 0;
}
------------------------------------------------------------------------------------------------------------------

(g) Write a program that compares two given dates. To


store date use structure say date that contains three
members namely date, month and year. If the dates are
equal then display message as "Equal" otherwise
"Unequal".
Solution:
#include<stdio.h>
#include<conio.h>
void main() {
struct date {
int date;
int month;
int year;
}d1;
int d,m,y,i;
clrscr();
printf("enter the date to save in structure: \n\n");
printf("Enter the date: ");
scanf("%d",&d1.date);
printf("\n\nEnter the month: ");
scanf("%d",&d1.month);
printf("\n\nEnter the year: ");
scanf("%d",&d1.year);
clrscr();
printf("\nenter the date to compare with structure:\n\n");
printf("Enter the date: ");
scanf("%d",&d);
printf("\n\nEnter the month: ");
scanf("%d",&m);

printf("\n\nEnter the year: ");


scanf("%d",&y);
if(d==d1.date && m==d1.month && y==d1.year) {
printf("\n\ndates are equal\n\n");
}
else {
printf("\n\ndates are unequal\n\n");
}
getch();
}

-----------------------------------------------------------------------------------------------------------------

(h) Linked list is a very common data structure often


used to store similar data in memory. While the elements
of an array occupy contiguous memory locations, those of
a linked list are not constrained to be stored in adjacent
location. The individual elements are stored
somewhere in memory, rather like a family dispersed,
but still bound together. The order of the elements is
maintained by explicit links between them. Thus, a
linked list is a collection of elements called nodes, each
of which stores two item of informationan element of
the list, and a link, i.e., a pointer or an address that
indicates explicitly the location of the node containing
the successor of this list element.
Write a program to build a linked list by adding new
nodes at the beginning, at the end or in the middle of

the linked list. Also write a function display( ) which


display all the nodes present in the linked list.
Solution:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<ctype.h>
struct linklist {
int data;
linklist *link;
};
void main() {
struct linklist *first, *temp;
char choice='y';
int count=0;
clrscr();

/* taking the input of first element */

printf("Enter element %d: ",++count);


first=(linklist *)malloc(sizeof(linklist));
scanf("%d",&first->data);
first->link=NULL;
printf("\n\nEnter another element(Y/N): ");
choice=getche();
tolower(choice); /* converting to lowercase to match the condition of switch */
switch(choice) {

case 'y':
clrscr();

while(choice=='y' || choice=='Y') {
printf("\nEnter element %d: ",++count);
temp=(linklist *)malloc(sizeof(linklist));
scanf("%d",&temp->data);
temp->link=first;
first=temp;

printf("\n\n\nEnter another elements(Y/N): ");


choice=getche();
clrscr();
}
break;

case 'n':
break;
}

printf("\nTotal elements = %d\n\n",count);

/* printing all elements */

for(temp=first; temp!=NULL; temp=temp->link) {


printf("%d ",temp->data);

getch();
}

-----------------------------------------------------------------------------------------------------------------

You might also like