[C language] [Chain queue] [Beginner] Using chain queue to achieve simple student information management

Hits: 0

After finishing the discussion of sequential [queues] , I started to study the chain storage of queues.

I have to say that this project gave me a deeper understanding of the [data structure of the queue]

This project is based on a personal summary of “Definition of Chain Queue, Empty, Enqueue, Dequeue” in the C language data structure course

Since the blogger is still a beginner, the code is quite unfamiliar. If there is something wrong, please correct it.

The following is the text

all code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct  student {     //Define a chain queue named student 
    char name[ 20 ]; //Data field 
    int score;
     struct  student * next ;   //Pointer field
};
struct  Link { 
    struct  student * front ,* rear ;  //declare the head pointer and tail pointer of the chain queue
};
void  menu ( void ) ; //Main menu 
void __init__(struct Link *p); //Initialize the chain queue 
void  Add_student (struct Link *p) ; //Add student information (new data into the queue) 
void  Read_student (struct Link * p) ; //Read student information 
void  Delete_student (struct Link *p) ; //Delete student information (dequeue data from the head of the queue) 
int  studentEmpty (struct Link *p) ; //Dismiss the team empty 
int  main ( void ) {
     int choose; //function selection for main menu 
    struct  Link * p= NULL ; 
    p = (struct Link *) malloc ( sizeof (struct Link)); //Apply memory for the pointer p pointing to the head and tail pointers 
    int skip = 0 ;
    __init__(p);
    do{
        menu();
        scanf("%d",&choose);
        switch(choose){
            case 1:
                Add_student(p);
                break;
            case 2:
                Read_student(p);
                break;
            case 3:
                Delete_student(p);
                break;
            case 4:
                skip = 1;
                break;
        }
    } while (skip == 0 );
     free (p); // free memory 
    printf ( "End, memory has been freed!" );
     return  0 ;
}
void  menu ( void ) {
     printf ( "Student management system:\n1.Add student information\n2.Read student information\n3.Delete queue head student information\n4.Exit\n" );
}
void __init__(struct Link *p){
    p -> front = NULL;
    p -> rear = NULL;
}
int  studentEmpty (struct Link *p) {    //The queue is empty 
    return (p -> front == NULL && p -> rear == NULL );
}
void  Add_student (struct Link *p) {     //Add student information 
    struct  student * pr ;  //Define a pointer of student structure type 
    pr = (struct student *) malloc ( sizeof (struct student)); //Apply for the pointer memory space 
    if (pr == NULL ){
         printf ( "Insufficient memory!\n" );
         return ;
    }
    int sc;
     char nam[ 20 ];
     printf ( "Please enter the name and grade of the student you want to add:\n" );
     scanf ( "%s %d" ,nam,&sc);
     strcpy (pr -> name, nam);
    pr -> score = sc;
    pr -> next = NULL ;
     if (studentEmpty(p)){   //If the original chain queue is empty, create a new chain queue, and the head and tail pointers both point to the new application node pr
        p -> front = pr;
        p -> rear = pr;
    }else{
        p -> rear -> next = pr; //The pointer field of the original tail node points to the new node 
        p -> rear = pr;          //The tail pointer points to the new node
    }
}
void  Read_student (struct Link *p) {
     if (studentEmpty(p)){
         printf ( "The queue is empty, please enter student information first!\n" );
         return ;
    }
    struct student *pr = p -> front;
    while(pr != NULL){
        printf("%s %d\n",pr -> name,pr -> score);
        pr = pr -> next;
    }
}
void  Delete_student (struct Link *p) {
     if (studentEmpty(p)){
         printf ( "The queue is empty, please enter student information first!\n" );
         return ;
    }
    struct student *pr;
    pr = p -> front;
    printf ( "The student you want to delete is: %s %d\n" ,pr -> name,pr -> score);
    p -> front = pr -> next; //The head node moves backward 
    free (pr); //The memory of the original head node is released 
}

running instance

You may also like...

Leave a Reply

Your email address will not be published.