Problem: In this program you are to implement a database of student records, usi
ID: 3829534 • Letter: P
Question
Problem: In this program you are to implement a database of student records, using the binary search tree abstract data type, implemented as a linked list. This must be written in C, not C++. The student record will have the following format: typedef struct studentRec{ int id; char[] name; //the name has a maximum length of 25 letters char[] major; //the major array has a max length of 15 int year; } You are required to maintain the database by providing the following functionality: 1. Search to see if a record exists in the data base 2. Add a record to the database; 3. Delete a record from the database 4. Update a record in the database 5. Print out all of the records in the database 6. Print out all of the records in the database from a given point I want answer of this question or similar to it please
Explanation / Answer
Assuming the database is maintained in the file with each line giving studen information as
id name major year
Everytime the program is run it reads the data from the file and at the end of the program, it writes the modified list to
the file.
id is chosen as a key to search records, update records and print purpose (from a given point)
typedef struct studentRec{
int id;
char name[25];
char major[15];
int year;
};
struct node {
studentRec rec;
struct node *next;
};
void main(){
head *node, *ptr, *ptr1;
FILE *fp;
int id;
char name[25];
char major[15];
int year;
int ch;
fp = fopen("input.txt", "r");
head = NULL;
while (fscanf(fp, "%d %s %s %d", &id, name, major, &year) != EOF){
if (head == NULL){
head = (struct node *)malloc(sizeof(struct node));
head->rec.id = id;
head->rec.name = name;
head->rec.major = major;
head->rec.year = year;
head->next = NULL;
}
else {
ptr = head;
while (ptr->next != NULL)
ptr = ptr->next;
ptr->next = (struct node *)malloc(sizeof(struct node));
ptr = ptr->next;
ptr->rec.id = id;
ptr->rec.name = name;
ptr->rec.major = major;
ptr->rec.year = year;
ptr->next = NULL;
}
}
fclose(fp);
ch = 0;
while (ch != 7){
printf("1.Search a record "");
printf("2.Add a record ");
printf("3.Delete a record ");
printf("4.Update a record ");
printf("5.Print all records ")
printf("6.Print all the records from a given point ");
printf("7.Quit ");
printf("Enter choice: ");
scanf("%d", &ch);
switch(ch) {
case 1:
printf("Enter Student ID: ");
scanf("%d", &id);
ptr = head;
found = 0;
while (found == 0 && ptr != NULL){
if (ptr->rec.id == id){
found = 1;
printf("%d %s %s %d ", ptr->rec.id, ptr->rec.name, ptr->rec.major, ptr->rec.year);
}
ptr = ptr->next;
}
if (found == 0)
printf("Record not found ");
case 2:
printf("Enter ID: ");
scanf("%d", &id);
printf("Enter Name: ");
scanf("%s", name);
printf("Enter Major: ");
scanf("%s", major);
printf("Enter year: ");
scanf("%d", &year);
ptr = head;
while (ptr=>next != NULL)
ptr = ptr->next;
ptr->next = (struct node *)malloc(sizeof(struct node));
ptr = ptr->next;
ptr->rec.id = id;
ptr->rec.name = name;
ptr->rec.major = major;
ptr->rec.year = year;
ptr->next = NULL;
case 3:
printf("Enter Student ID: ");
scanf("%d", &id);
if (head->rec.id == id){
head = head->next;
}
ptr = head;
ptr1 = ptr->next
found = 0;
while (found == 0 && ptr1 != NULL){
if (ptr1->rec.id == id){
found = 1;
ptr->next = ptr1->next;
printf("Record Deleted ")
}
ptr = ptr->next;
ptr1 = ptr1->next;
}
if (found == 0)
printf("Record not found ");
case 4:
printf("Enter Student ID: ");
scanf("%d", &id);
ptr = head;
found = 0;
while (found == 0 && ptr != NULL){
if (ptr->rec.id == id){
found = 1;
printf("%d %s %s %d ", ptr->rec.id, ptr->rec.name, ptr->rec.major, ptr->rec.year);
printf("Enter ID: ");
scanf("%d", &ptr->rec.id);
printf("Enter Name: ");
scanf("%s", ptr->rec.name);
printf("Enter Major: ");
scanf("%s", ptr->rec.major);
printf("Enter year: ");
scanf("%d", &ptr->rec.year);
printf("Record Updated ")
}
ptr = ptr->next;
}
if (found == 0)
printf("Record not found ");
case 5:
ptr = head;
while (ptr != NULL){
printf("%d %s %s %d ", ptr->rec.id, ptr->rec.name, ptr->rec.major, ptr->rec.year);
ptr = ptr->next;
}
}
case 6:
printf("Enter Student ID: "); /* Records will be printed from thid id onwards */
scanf("%d", &id);
ptr = head;
found = 0;
while (ptr != NULL){
if (ptr->rec.id == id){
found = 1;
}
if (found == 1){
printf("%d %s %s %d ", ptr->rec.id, ptr->rec.name, ptr->rec.major, ptr->rec.year);
}
ptr = ptr->next;
}
}
}
fopen("input.txt", "w");
ptr = head;
while (ptr != NULL){
fprintf(fp, "%d %s %s %d ", ptr->rec.id, ptr->rec.name, ptr->rec.major, ptr->rec.year);
ptr = ptr->next;
}
fclose(fp);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.