Student Information System Project using Linked List and BST in C
Let us create a simple project for Student Information System. It uses a Linked list and Binary Search Tree.
// First,
a student structure is created with rollno, name ,marks and a pointer next.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Let us create
Student structure
typedef struct Student {
int s_rollno;
char s_name[50];
float s_marks;
struct Student
*next; // Self Referential structure
} Student;
// BST Node structure is created
typedef struct BST_Node {
Student *student;
struct BST_Node
*left, *right;
} BST_Node;
Student *head = NULL;
// Linked List head is assigned as NULL
BST_Node *root = NULL;
// BST root
// create function to memory allocation
Student* createStudent(int roll, char name[], float marks) {
Student
*newStudent = (Student*)malloc(sizeof(Student));
newStudent->s_rollno = roll;
strcpy(newStudent->s_name, name);
newStudent->s_marks = marks;
newStudent->next = NULL;
return newStudent;
}
// Adding a student details to Linked List (at end)
void insertLinkedList(Student *newStudent) {
if (head == NULL)
{
head =
newStudent;
} else {
Student *temp
= head;
while
(temp->next != NULL)
temp =
temp->next;
temp->next
= newStudent;
}
}
// Creation of a new BST node
BST_Node* create_BSTNode(Student *student) {
BST_Node *newNode
= (BST_Node*)malloc(sizeof(BST_Node));
newNode->student = student;
newNode->left =
newNode->right = NULL;
return newNode;
}
// Insert the values into BST
BST_Node* insertBST(BST_Node *node, Student *student) {
if (node == NULL)
return create_BSTNode(student);
if
(student->s_rollno < node->student->s_rollno)
node->left
= insertBST(node->left, student);
else if
(student->s_rollno > node->student->s_rollno)
node->right
= insertBST(node->right, student);
return node;
}
// Search the roll no in BST
Student* searchBST(BST_Node *node, int roll) {
if (node == NULL)
return NULL;
if (roll ==
node->student->s_rollno) return node->student;
if (roll <
node->student->s_rollno) return searchBST(node->left, roll);
return
searchBST(node->right, roll);
}
// Display the details of all students (Linked List
traversal)
void display_Students() {
Student *temp =
head;
printf("\n---
Student Records ---\n");
while (temp !=
NULL) {
printf("Roll Number: %d | Name: %s | Marks: %.2f\n",
temp->s_rollno, temp->s_name, temp->s_marks);
temp =
temp->next;
}
}
// Main function
int main() {
int choice, roll;
char name[50];
float marks;
Student *student;
printf("\t\tStudent Information System\n");
while (1) {
printf("\n1. Add \n2. Display \n3. Search by Rollno \n4.
Exit\nEnter choice: ");
scanf("%d", &choice);
switch
(choice) {
case 1:
printf("Enter Roll number: ");
scanf("%d", &roll);
printf("Enter Name: ");
scanf("%s", name);
printf("Enter Marks: ");
scanf("%f", &marks);
student = createStudent(roll, name, marks);
insertLinkedList(student);
root =
insertBST(root, student);
break;
case 2:
display_Students();
break;
case 3:
printf("Enter Roll to Search: ");
scanf("%d", &roll);
student = searchBST(root, roll);
if
(student != NULL)
printf("Found -> Roll: %d | Name: %s | Marks: %.2f\n",
student->s_rollno, student->s_name, student->s_marks);
else
printf("Student not found!\n");
break;
case 4:
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}
Output:
Student
Information System
1. Add
2. Display
3. Search by Rollno
4. Exit
Enter choice: 1
Enter Roll number: 1
Enter Name: Ajay
Enter Marks: 89
1. Add
2. Display
3. Search by Rollno
4. Exit
Enter choice: 1
Enter Roll number: 2
Enter Name: Baby
Enter Marks: 78
1. Add
2. Display
3. Search by Rollno
4. Exit
Enter choice: 1
Enter Roll number: 3
Enter Name: Jey
Enter Marks: 95
1. Add
2. Display
3. Search by Rollno
4. Exit
Enter choice: 2
--- Student Records ---
Roll Number: 1 | Name: Ajay | Marks: 89.00
Roll Number: 2 | Name: Baby | Marks: 78.00
Roll Number: 3 | Name: Jey | Marks: 95.00
1. Add
2. Display
3. Search by Rollno
4. Exit
Enter choice: 3
Enter Roll to Search: 2
Found -> Roll: 2 | Name: Baby | Marks: 78.00
1. Add
2. Display
3. Search by Rollno
4. Exit
Enter choice: 4
=== Code Execution Successful ===
Hope, this code is useful to you. Keep Coding!!!
Comments
Post a Comment