Write the functions that will add print and delete a bank re
Write the functions that will add, print, and delete a bank record. The program will be written in C
These are the protoypes for the functions:
- int addRecord (struct record **, int, char [ ],char [ ],int);
- struct record ** is the pointer to the pointer to the start of the record, passed in main.
- struct record *start = NULL will be defined in main
- the second int will pass an account number
- char[] will pass the name of the user
- the second char[] will pass the address of the user
- the last int will pass the year of birth of the user
- THIS FUNCTION WILL ADD THE RECORDS IN ORDER OF THE YEAR OF BIRTH, YOUNGEST TO OLDEST
- e.g. an account with a YoB 1990 will come before a record with 1980.
- If there is a duplicate account (Same account number), it will still be made, but ordered in the birthyear
- if the birth year is the same, the new account will be added after the old account
- int printRecord (struct record *, int);
- int will pass the account number of the record that needs to be printed
- int deleteRecord(struct record **, int);
- int will pass the account number of the record that needs to be printed
- this function will delete the entire record for that account number
This header file will be provided:
struct record
{
int accountno;
char name[25];
char address[80];
int yearofbirth;
struct record* next;
};
None of the information on top can be changed. Thank you in advance for the help!
Solution
//tested on ubuntu,Linux
/**************program***********/
#include<stdio.h>
#include<malloc.h>
#include<string.h>
/*Structure declaration*/
struct record
{
int accountno;
char name[25];
char address[80];
int yearofbirth;
struct record* next;
};
/*Addrecord function implementation
* @param start
* @param account number
* @param Name
* @param Address
* @param Year Of Birth
*
*
*
* */
int addRecord (struct record ** start,int accountno,char name[],char address[],int yob){
struct record *temp=*start,*newRecord;
/*New record creation using malloc*/
newRecord=(struct record*)malloc(sizeof(struct record));
newRecord->accountno=accountno;
strncpy(newRecord->name,name,strlen(name)+1);
strncpy(newRecord->address,address,strlen(address)+1);
newRecord->yearofbirth=yob;
/*initial condition is start is NULL
* OR year of Birth of start is less then yob*/
if(*start==NULL || (*start)->yearofbirth<yob) {
newRecord->next=*start;
*start=newRecord;
}else{
/*If year of created is greater then the current record*/
while(temp->next!=NULL && temp->next->yearofbirth>=yob){
temp=temp->next;
}
/*Inserting into right place*/
newRecord->next=temp->next;
temp->next=newRecord;
}
}
/*Printrecord function implementation
* @param start
* @param Account number
* Printing only which matches accountno in record list
* */
int printRecord (struct record *start, int accountno){
struct record *temp=start;
while(temp!=NULL){
if(temp->accountno==accountno){
printf(\"Account Number :%d \ Name:%s\ Address:%s\ YearOfBirth:%d \ \",temp->accountno,temp->name,temp->address,temp->yearofbirth);
}
temp=temp->next;
}
}
/*Deleterecord Function implementation
* @param start
* @param Account number
* account number may be matches with start account number
* OR it can matches in between of list*/
int deleteRecord(struct record ** start, int accountno){
struct record *previous,*temp=*start;
while(temp!=NULL){
/*If account number matches with start*/
if(temp->accountno==accountno){
if(temp==*start){
*start=temp->next;
free(temp);
return 1;
}else{
previous->next=temp->next;
free(temp);
return 1;
}
}else{
/*Else storing current record into previous record pointer and moving temp to next record*/
previous=temp;
temp=temp->next;
}
}
}
int main(){
/*Start declaration and initializing with NULL*/
struct record *start=NULL;
/*Calling addrecord function for adding record*/
addRecord(&start,112,\"Anshu\",\"France\",1990);
addRecord(&start,113,\"Lalchand\",\"UK\",1990);
addRecord(&start,114,\"Gopal\",\"INDIA\",1967);
addRecord(&start,117,\"Sam\",\"USA\",1997);
printf(\"*******Before Record deletion*******\ \");
/*Calling Printrecord function for printing*/
printRecord(start,112);
/*Calling deleterecord function*/
deleteRecord(&start,112);
printf(\"*******After Record deletion*******\ \");
printRecord(start,112);
return 0;
}
/*************output*************/
Anshu@Anshu:~/Desktop/chegg$ gcc account.c
Anshu@Anshu:~/Desktop/chegg$ ./a.out
*******Before Record deletion*******
Account Number :112
Name:Anshu
Address:France
YearOfBirth:1990
*******After Record deletion*******
Thanks a lot
If you have any query Feel free to ask



