bai final
unknown
c_cpp
a year ago
4.0 kB
8
Indexable
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> struct NhanVien { int maNV; char ten[50]; int tuoi; struct NhanVien* next; }; struct LinkedList { struct NhanVien* head; }; struct LinkedList danhSachNV = {NULL}; void themNhanVien() { struct NhanVien* newNhanVien = (struct NhanVien*)malloc(sizeof(struct NhanVien)); if (newNhanVien != NULL) { printf("Nhap ma nhan vien: "); scanf("%d", &newNhanVien->maNV); printf("Nhap ten nhan vien: "); scanf("%s",newNhanVien->ten); printf("nhap tuoi: "); scanf("%d",&newNhanVien->tuoi); newNhanVien->next = danhSachNV.head; danhSachNV.head = newNhanVien; printf("Nhan vien da duoc them thanh cong.\n"); } else { printf("Khong du bo nho cho viec them nhan vien.\n"); } } void timKiemNhanVien() { if (danhSachNV.head != NULL) { int maTimKiem; printf("Nhap ma nhan vien can tim kiem: "); scanf("%d", &maTimKiem); struct NhanVien* current = danhSachNV.head; while (current != NULL) { if (current->maNV == maTimKiem) { printf("Nhan vien tim thay - Ma: %d, Ten: %s\n", current->maNV, current->ten); return; } current = current->next; } printf("Khong tim thay nhan vien.\n"); } else { printf("Danh sach nhan vien dang trong.\n"); } } void sapXepNhanVien() { struct NhanVien *i, *j, *temp; for (i = danhSachNV.head; i != NULL; i = i->next) { for (j = i->next; j != NULL; j = j->next) { if (i->maNV > j->maNV) { // Swap temp = (struct NhanVien*)malloc(sizeof(struct NhanVien)); temp->maNV = i->maNV; strcpy(temp->ten, i->ten); i->maNV = j->maNV; strcpy(i->ten, j->ten); j->maNV = temp->maNV; strcpy(j->ten, temp->ten); free(temp); } } } printf("Nhan vien da duoc sap xep thanh cong theo ma.\n"); } void hienThiDanhSachNhanVien() { if (danhSachNV.head != NULL) { printf("\nDanh sach nhan vien:\n"); struct NhanVien* current = danhSachNV.head; while (current != NULL) { printf("Ma: %d, Ten: %s\n", current->maNV, current->ten); printf("==========================\n"); current = current->next; } } else { printf("Khong co nhan vien nao trong danh sach.\n"); } } void Kiemtra() { struct NhanVien* current = danhSachNV.head; while (current==NULL) { printf("ham rong vui long nhap \n"); return; } } int main() { int luaChon; do { printf("\n1. Them nhan vien\n2. Tim kiem nhan vien\n3. Sap xep nhan vien\n4. Hien thi danh sach nhan vien\n0. Thoat\n"); printf("Nhap lua chon cua ban: "); scanf("%d", &luaChon); switch (luaChon) { case 1: themNhanVien(); break; case 2: Kiemtra(); timKiemNhanVien(); break; case 3: sapXepNhanVien(); break; case 4: hienThiDanhSachNhanVien(); break; case 0: printf("Dang thoat chuong trinh.\n"); break; default: printf("Lua chon khong hop le. Vui long thu lai.\n"); } } while (luaChon != 0); struct NhanVien* current = danhSachNV.head; while (current != NULL) { struct NhanVien* temp = current; current = current->next; free(temp); } return 0; }
Editor is loading...
Leave a Comment