Nhập nội dung tìm kiếm

30/03/2022

Bài tập QLHS bằng Danh sách liên kết

//CHƯƠNG TRÌNH QUẢN LÝ HỌC SINH 

#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

//1.Dinh nghia du lieu hoc sinh

typedef struct hocsinh{

int mahs;

char hoten[30];

float dtb;

} hocsinh;

//2.Khai bao cau truc 1 cai node

struct NODE{

    hocsinh data;

    struct NODE *next;

 };

//Thay the kieu du lieu struct NODE bang ten node

typedef struct NODE *node; 

//3. Khoi tao danh sach rong

node KhoiTao(node head){

    head = NULL;

    return head;

}


//4. Tao gia tri cho 1 cai node

node TaoNode(hocsinh hs){

    node tam; //Khoi tao node tam

    tam = (node)malloc(sizeof(struct NODE));

    tam->next = NULL;// Cho next tro toi NULL

    tam->data = hs; // Gan gia tri cho Node

    return tam;//Tra ve node moi da co gia tri

}


//5. Ham them vao cuoi danh sach

node ThemVaoCuoi(node head, hocsinh hs){

    node tam,p;// Khai bao 2 node tam và p

    tam = TaoNode(hs);//Tao node voi gia tri x gan vao bien tam

    if(head == NULL){

        head = tam;     //Neu node head trong thi gan tam cho head

    }

    else{

        p  = head;// p tro toi head

        while(p->next != NULL){

            p = p->next;//Duyet danh sach lien ket den cuoi.

        }

        p->next = tam;//Ket noi next den tam

    }

    return head;

}


//Ham nhap danh sach

node NhapDanhSach(node head,int n){

    hocsinh hs;

    for(int i = 1; i <= n; ++i){

        printf("\nNhap hoc sinh thu %i: ",i);

        printf("\nNhap Ma hoc sinh: ");

        scanf("%d",&hs.mahs);

printf("Nhap Ten hoc sinh: ");

fflush(stdin);

gets(hs.hoten);

printf("Nhap Diem trung binh: ");

        scanf("%f",&hs.dtb);

        head = ThemVaoCuoi(head, hs);

    }

    return head;

}


//Ham xem danh sach

void XemDanhSach(node head){

int i=1;

for(node p = head; p != NULL; p = p->next){

        printf("\n--------------------------");

        printf("\nHoc sinh thu %d:",i);

        printf("\nMa HS: %d",p->data.mahs);

        printf("\nTen HS:");

        puts(p->data.hoten);

        printf("Diem TB: %.1f",p->data.dtb);

        i++;

    }

}


//Ham Dem hoc sinh co diem trung binh nho hon 5

int DemHocSinh(node head){

int dem = 0;

for(node p = head; p != NULL; p = p->next)

        if (p->data.dtb < 5) dem++;

    return dem;

}

int main(){

    node ds;

    int n;

printf("\n==CHUONG TRINH QUAN LY HOC SINH==");

    ds = KhoiTao(ds);

do{

        printf("\nNhap so luong hoc sinh n = ");

        scanf("%d", &n);

    }while(n <= 0);

ds = NhapDanhSach(ds,n);

    printf("\nDANH SACH HOC SINH VUA NHAP");

XemDanhSach(ds);

    

    printf("\nSo luong hoc sinh co DTB duoi 5 la:%d",DemHocSinh(ds));

  getch();

  return 0;

}

Liên hệ

Tên

Email *

Thông báo *