Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct studentRec{
- int id;
- char name[25];
- char major[15];
- int year;
- }studentRec;
- typedef struct node{
- studentRec info;
- struct node* right;
- struct node* left;
- }node;
- FILE* input;
- node* initBST();
- int search(node* top, int id);
- node* addNode(node* top, int id, char* name, char* major, int year);
- node* updateStudent(node* top, int id, int newId);
- node* deleteStudent(node* top, int id);
- void print(node* top);
- void printFrom(node* top);
- node* insertNode(node* top, node* newNode);
- int main()
- {
- int choice=1, id=0, newId=0;
- node* top = initBST();
- while(choice!=0){
- printf("Please select and action from the following menu\n");
- printf("2 - Search\n3 - Add\n4 - Update\n5 - Delete\n6 - Print\n7 - Print From\n0 - Exit\n");
- scanf("%d",&choice);
- switch (choice){
- case 2:
- printf("Enter ID to search: ");
- scanf("%d", &id);
- search(top, id);
- break;
- case 3:
- case 4:
- printf("Enter student current ID: ");
- scanf("%d", &id);
- printf("Enter student's new ID: ");
- scanf("%d", &newId);
- break;
- case 5:
- printf("Enter ID to delete: ");
- scanf("%d", &id);
- deleteStudent(top, id);
- break;
- case 6:
- print(top);
- break;
- case 7:
- printFrom(top);
- break;
- case 0:
- break;
- default:
- printf("invalid Selection\n");
- }
- }
- return 0;
- }
- node* initBST(){
- node* top=NULL;
- input = fopen("input.txt", "r+");
- int id = -1, year=0;
- char name[25], lName[25], major[15];
- while(!feof(input)){
- fscanf(input, "%d ", &id);
- if(id==0)
- break;
- fscanf(input, "%s %s %d", name, lName, &year);
- strcat(name, lName);
- addNode(top, id, name, major, year);
- }
- fclose(input);
- }
- int search(node* top, int id){
- }
- node* addNode(node* top, int id, char* name, char* major, int year){
- node* tempNode = (malloc(sizeof(node)));
- tempNode->info.id = id;
- strcpy(tempNode->info.name, name);
- strcpy(tempNode->info.major, major);
- tempNode->info.year = year;
- node* tempPtr = top;
- insertNode(tempPtr, tempNode);
- }
- node* insertNode(node* top, node* newNode){
- if(top==NULL){
- top=newNode;
- }
- if(newNode->info.id > top->info.id){
- if(top->right==NULL){
- top->right = newNode;
- }
- else{
- top = top->right;
- insertNode(top, newNode);
- }
- }
- if(newNode->info.id < top->info.id){
- if(top->left==NULL){
- top->left = newNode;
- }
- else{
- top = top->left;
- insertNode(top, newNode);
- }
- }
- return top;
- }
- node* updateStudent(node* top, int id, int newId){
- }
- node* deleteStudent(node* top, int id){
- }
- void print(node* top){
- if (top != NULL) {
- printf("no");
- print(top->left);
- printf("%d %s %s %d\n", top->info.id, top->info.name, top->info.major, top->info.year);
- print(top->right);
- }
- }
- void printFrom(node* top){
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement