1. Trang chủ
  2. » Tất cả

giải bài tập DLCTGT

60 8 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 60
Dung lượng 36,52 KB

Nội dung

bai 1 include include include struct sinhvien { int masv; char tensv20; char lop20; float dtk; char hk15; }; typedef struct sinhvien SV; struct node{ SV dat. bai 1 include include include struct sinhvien { int masv; char tensv20; char lop20; float dtk; char hk15; }; typedef struct sinhvien SV; struct node{ SV dat. bai 1 include include include struct sinhvien { int masv; char tensv20; char lop20; float dtk; char hk15; }; typedef struct sinhvien SV; struct node{ SV dat. bai 1 include include include struct sinhvien { int masv; char tensv20; char lop20; float dtk; char hk15; }; typedef struct sinhvien SV; struct node{ SV dat. bai 1 include include include struct sinhvien { int masv; char tensv20; char lop20; float dtk; char hk15; }; typedef struct sinhvien SV; struct node{ SV dat. bai 1 include include include struct sinhvien { int masv; char tensv20; char lop20; float dtk; char hk15; }; typedef struct sinhvien SV; struct node{ SV dat. bai 1 include include include struct sinhvien { int masv; char tensv20; char lop20; float dtk; char hk15; }; typedef struct sinhvien SV; struct node{ SV dat.

// bai 1: #include #include #include struct sinhvien { int masv; char tensv[20]; char lop[20]; float dtk; char hk[15]; }; typedef struct sinhvien SV; struct node{ SV data; node * next; }; typedef struct node NODE; struct list{ NODE *pHead; NODE *pTail; }; typedef struct list LIST; void KhoiTao(LIST &ds){ ds.pHead = NULL; ds.pTail = NULL; } int KiemTraRong(LIST ds){ if (ds.pHead == NULL){ return 1; } return 0; } NODE* TaoNode(SV x) { NODE *p; p = new NODE; if (p==NULL) { printf ("KHONG DU BO NHO"); return NULL; } p->data=x; p->next=NULL; return p; } void ChenCuoi (LIST &ds, NODE *p){ if (ds.pHead==NULL) { ds.pHead=p; ds.pTail=p; } else { ds.pTail->next=p; ds.pTail=p; } } void Nhap(LIST &ds, int n){ printf("NHAP THONG TIN SINH VIEN\n"); for(int i = 0; i < n; i++){ SV x; printf ("Nhap ma sinh vien : ");fflush(stdin); scanf("%d",&x.masv); printf ("Nhap ten sinh vien : ");fflush(stdin); gets(x.tensv); printf ("Nhap lop : ");fflush(stdin); gets(x.lop); printf ("Nhap diem tong ket : ");fflush(stdin); scanf("%f",&x.dtk); printf ("Nhap hanh kiem : ");fflush(stdin); gets(x.hk); printf ("\n \n"); NODE *p = new NODE; p = TaoNode(x); ChenCuoi(ds,p); } } void Xuat(LIST ds){ for(NODE *p = ds.pHead; p!= NULL; p=p->next){ printf("%d\t %s\t %s\t %f\t %s\n", p->data.masv, p->data.tensv, p->data.lop, p->data.dtk, p>data.hk); } } void SVD13CNPM(LIST ds){ for(NODE *p = ds.pHead; p!= NULL; p=p->next){ if(strcmp(p->data.lop, "D13CNPM")==0){ printf("%d\t %s\t %s\t %f\t %s\n", p->data.masv, p->data.tensv, p->data.lop, p->data.dtk, p>data.hk); } } } void SapXep(LIST &ds){ NODE *p, *q; for(p = ds.pHead; p != ds.pTail; p=p->next){ for(q = p->next; q != NULL; q = q->next){ if(p->data.masv > q->data.masv){ SV x = p->data; p->data = q->data; q->data = x; } } } Xuat(ds); } void XoaCuoi(LIST &ds) { for(NODE *k = ds.pHead; k != NULL; k = k ->next) { if(k->next == ds.pTail) { delete ds.pTail; k->next = NULL; ds.pTail = k; } } Xuat(ds); } int main(){ LIST ds; int n; printf("NHAP N: "); scanf("%d",&n); KhoiTao(ds); Nhap(ds,n); printf("\nDANH SACH SINH VIEN\n"); Xuat(ds); printf("\nDANH SACH SINH VIEN D13CNPM\n"); SVD13CNPM(ds); printf("\nDANH SACH SINH VIEN SAP XEP THEO MA\n"); SapXep(ds); printf("\nDANH SACH SINH VIEN DA XOA PHAN TU CUOI\n"); XoaCuoi(ds); } //bai #include #include #include struct hocvien{ int mhv; char tenHV[25]; char lop[25]; char chuyenNganh[30]; float diemTK; }; typedef struct hocvien HV; struct Node { //khai bao phan du lieu co kieu int HV data; //khai bao tro next co kieu Node de chua dia chi phan tu sau Node *next; //khai bao tro prev co kieu Node de chua dia chi phan tu truoc Node *prev; }; typedef struct Node NODE; struct doulist{ //thanh phan dau danh sach NODE *pHead; //thanh phan cuoi danh sach NODE *pTail; }; typedef struct doulist DLIST; void KhoiTao(DLIST &ds){ //dat dia chi dau danh sach bang NULL ds.pHead = NULL; //dat dia chi cuoi danh sach bang NULL ds.pTail = NULL; } NODE* TaoNode(HV x) { //tao mot node p moi NODE *p; p = new NODE; //neu p==NULL thi khong du bo nho va ket thuc viec tao node if (p==NULL) { printf ("KHONG DU BO NHO"); return NULL; } //gan phan data = x p->data=x; //gan tro next = NULL p->next=NULL; //gan tro prev = NULL p->prev = NULL; //tra ve node p da tao return p; } void ThemCuoi(DLIST &ds, NODE*p){ //kiem tra danh sach rong neu rong thi them vao dau va cuoi if (ds.pHead == NULL){ ds.pHead = ds.pTail = p; }else{ //dat tro next cua pTail hien tai vao p la node can them cuoi ds.pTail->next = p; //dat tro prev cua node p ve phan tu cuoi danh sach p->prev = ds.pTail; //thay doi lai phan tu cuoi danh sach ds.pTail = p; } } void Nhap(DLIST &ds, int n){ printf("NHAP VAO THONG TIN HOC VIEN\n"); //duyet N lan for(int i = 0; i < n; i++){ //nhap du lieu la cho hoc vien thu i HV x; printf("Nhap MaHV: "); scanf("%d",&x.mhv); printf("Nhap TenHV: "); fflush(stdin); gets(x.tenHV); printf("Nhap lop: "); fflush(stdin); gets(x.lop); printf("Chuyen nganh: "); fflush(stdin); gets(x.chuyenNganh); printf("Diem TK: "); scanf("%f",&x.diemTK); printf("\n\n"); //tao node p NODE *p = new NODE; //dua du lieu vua nhap vao node p p = TaoNode(x); //dua node p vao ham them cuoi va truyen vao node p vua tao ThemCuoi(ds,p); } } void Xuat(DLIST ds){ //khoi tao mot node NODE *p = new NODE; //duyet tu dau danh sach den cuoi danh sach voi dieu kien p!=NULL for(p = ds.pHead; p!= NULL; p=p->next){ //hien thi du lieu cua tung hoc vien printf("%d\t %s\t %s\t %s\t %f\t \n", p->data.mhv, p->data.tenHV, p->data.lop, p>data.chuyenNganh, p->data.diemTK); } } void LietKe(DLIST ds){ //khoi tao mot node NODE *p = new NODE; //duyet tu dau danh sach den cuoi danh sach voi dieu kien p!=NULL for(p = ds.pHead; p!= NULL; p=p->next){ //so sanh hoc vien thuoc nganh Thuong Mai Dien Tu if(strcmp(p->data.chuyenNganh,"Thuong Mai Dien Tu") == 0){ //neu diem tong ket cua hoc vien >= if(p->data.diemTK >= 8){ //hien thi du lieu cua tung hoc vien printf("%d\t %s\t %s\t %s\t %f\t \n", p->data.mhv, p->data.tenHV, p->data.lop, p>data.chuyenNganh, p->data.diemTK); } } } } NODE* TimKiem(DLIST ds, int mhv){ //tao node p NODE *p; //gan p bang phan tu dau danh sach p = ds.pHead; //su dung vong lap while ((p!=NULL) && (p->data.mhv!=mhv)){ p=p->next; } //tra ve ket qua, neu NULL thi khong tim thay return p; } void XoaDau(DLIST &ds){ //gan p bang phan tu dau danh sach NODE *p = ds.pHead; //thuc hien gan lai phan tu dau danh sach ds.pHead = ds.pHead->next; ds.pHead->prev = NULL; //neu khong ton tai phan tu dau danh sach if (ds.pHead==NULL){ ds.pTail = NULL; } //thay doi tro next ve NULL p->next = NULL; //xoa node p duoc gan bang phan tu dau danh sach delete p; } void XoaCuoi (DLIST &ds){ //tao node p va gan bang phan tu cuoi danh sach NODE *p = ds.pTail; //thuc hien gan lai phan tu cuoi danh sach ds.pTail = ds.pTail->prev; ds.pTail->next = NULL; //kiem tra phan tu cuoi neu rong if (ds.pTail==NULL){ ds.pHead = NULL; } //thay doi tro prev cua node p ve NULL p->prev = NULL; //xoa p duoc gan bang phan tu cuoi danh sach delete p; } void XoaTheoKhoaK(DLIST &ds, int mhv){ //tim kiem theo du lieu x va gan vao node p NODE *p = TimKiem(ds, mhv); //neu tim thay ket qua if(p != NULL){ //neu p->prev == NULL thuc hien xoa dau if (p->prev==NULL){ XoaDau(ds); return; } //neu p->next == NULL thuc hien xoa cuoi if (p->next==NULL){ XoaCuoi(ds); return; } //thay doi lai lien ket cua phan tu co khoa K can xoa p->prev->next = p->next; p->next->prev = p->prev;

Ngày đăng: 05/01/2023, 16:34

w