Student Management System. |
|
|
/* Student Management System */ #include <stdio.h> #include <alloc.h> #include <stdlib.h> #include <conio.h> #include <ctype.h> #include <string.h> /* function prototypes */ void create( char *, int, int, char * ); void printnode( struct node * ); void display( struct node * ); struct node * search( struct node *, int ); void delete( struct node * ); /* definition of a data node for holding student information */ struct node { char name[20]; int age; int studentID; char course[15]; struct node *next; }; /* head points to first node in list, end points to last node in list */ /* initialise both to NULL, meaning no nodes in list yet */ struct node *head = (struct node *) NULL; struct node *end = (struct node *) NULL; /* this initialises a node, allocates memory for the node, and returns */ /* a pointer to the new node. Must pass it the node details, name and id */ void create( char *name, int age, int studentID, char *course){ struct node *ptr; ptr = (struct node *) calloc( 1, sizeof(struct node ) ); if( ptr == NULL ) /* error allocating node? */ printf("List is null"); /* then return NULL, else */ else { /* allocated node successfully */ strcpy( ptr->name, name ); /* fill in name details */ ptr->age = age; /* copy id details */ ptr->studentID = studentID; strcpy(ptr->course,course); if( head == NULL ) /* if there are no nodes in list, then */ head = ptr; /* set head to this new node */ end->next = ptr; /* link in the new node to the end of the list */ ptr->next = NULL; /* set next field to signify the end of list */ end = ptr; /* return pointer to new node */ } } /* this prints the details of a node, eg, the name and id */ /* must pass it the address of the node you want to print out */ void printnode( struct node *ptr ){ printf("\n"); printf("Student ID : - %d \n", ptr->studentID); printf("Name of Student : - %s \n", ptr->name ); printf("Age : - %d \n", ptr->age ); printf("Course Carried : - %s \n", ptr->course); } /* this prints all nodes from the current address passed to it. If you */ /* pass it 'head', then it prints out the entire list, by cycling through */ /* each node and calling 'printnode' to print each node found */ void display( struct node *ptr ){ while( ptr != NULL ){ /* continue whilst there are nodes left */ printnode( ptr ); /* print out the current node */ ptr = ptr->next; /* goto the next node in the list */ } } /* search the list for a name, and return a pointer to the found node */ /* accepts a studentID to search for, and a pointer from which to start. If */ struct node * search( struct node *ptr, int studentID ){ while( ptr->studentID!=studentID ) { /* whilst name not found */ ptr = ptr->next; /* goto the next node */ if( ptr == NULL ) /* stop if we are at the */ break; /* of the list */ } return ptr; /* return a pointer to */ } /* found node or NULL */ /* deletes the specified node pointed to by 'ptr' from the list */ void delete( struct node *ptr ){ struct node *temp, *prev; temp = ptr; /* node to be deleted */ prev = head; /* start of the list, will cycle to node before temp */ if( temp == prev ) { /* are we deleting first node */ head = head->next; /* moves head to next node */ if( end == temp ) /* is it end, only one node? */ end = end->next; /* adjust end as well */ free( temp ); /* free space occupied by node */ printf("Node has been deleted ! "); } else{ /* if not the first node, then */ while( prev->next != temp ){ /* move prev to the node before*/ prev = prev->next; /* the one to be deleted */ } prev->next = temp->next; /* link previous node to next */ if( end == temp ) /* if this was the end node, */ end = prev; /* then reset the end pointer */ free( temp ); /* free space occupied by node */ } } /* main() the start of management system */ main(){ char name[20], course[20]; int age, ch = 1,studentID; struct node *ptr; clrscr(); while( ch != 5 ) { printf("\n \t ***** Student Management System ***** \n"); printf("1. Display all student List \n"); printf("2. Add a student \n"); printf("3. Delete a student \n"); printf("4. Search student\n"); printf("5. Exit\n"); printf("Please select your choice : - "); scanf("%d", &ch ); /* statements for handling user instruction */ switch( ch ){ /* Display all nodes in a list */ case 1: display( head ); break; /* Create new node in a List */ case 2: printf("\n ***** Data entry procedure ***** \n"); printf("\n Enter student ID : - "); scanf("%d",&studentID); printf("\n Enter student name : - "); scanf("%s", name ); printf("\n Enter student age : - "); scanf("%d", &age ); printf("\n Enter course name : - "); scanf("%s", course); create( name, age, studentID, course ); break; /* delete a node containing entered studentID */ case 3: printf("Enter student ID : - "); scanf("%d",&studentID ); ptr = search( head, studentID); delete( ptr); break; /*Search and display the node information whose studentID entered by user*/ case 4: printf("Enter in Student ID :- "); scanf("%d",&studentID ); ptr = search( head, studentID); if( ptr ==NULL ){ printf("Student ID - %d not found\n", studentID ); } else printnode( ptr ); break; } } } |
|
|
|
|
|
|
|