Thứ Tư, 16 tháng 12, 2015

Chương trình tổng hợp các bài về danh sách liên kết đơn kiểu cấu trúc

 CODE NÀY CHẠY CHUẨN TRÊN Visual Studio, NẾU CHẠY TRÊN Borland C THÌ SẼ PHẢI FIX MỘT VÀI CHỖ !
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>

int dem = 0;

//Tạo kiểu cấu trúc
typedef struct SV
{
    int stt;
    long ms;
    char ten[41];
    float dtb;
    char sex;
};

typedef struct node
{
    SV info;
    node *next;
};

//Tạo các hàm xử lí danh sách lkđ cơ bản
node *getnode(SV x)
{
    node *p = new node;
    if (!p)    return NULL;
    p->info = x;
    p->next = NULL;
    return p;
}

void addhead(node *&h, node *&t, SV x)
{
    node *p = getnode(x);
    if (!h)    h = t = p;
    else
    {
        p->next = h;
        h = p;
    }
}

void addtail(node *&h, node *&t, SV x)
{
    node *p = getnode(x);
    if (!h)    h = t = p;
    else
    {
        t->next = p;
        t = p;
    }
}

void addafter(node *q, node *&t, SV x)
{
    node *p = getnode(x);
    p->next = q->next;
    q->next = p;
    if (q == t)
        t = p;
}

void delhead(node *&h, node *&t)
{
    node *p = h;
    if (!h)    return;
    h = h->next;
    delete p;
    if (!h)    t = NULL;
}

void delafter(node *q, node *&t)
{
    node *p = q->next;
    q->next = p->next;
    delete p;
    if (q->next == NULL)
        t = q;
}

void delall(node *&h, node *&t)
{
    node *p = h;
    while (h)
    {
        p = h;
        h = h->next;
        delete p;
    }
    t = NULL;
}

void deltail(node *&h, node *&t)
{
    if (!h)    return;
    node *p = h;
    node *r = t;
    if (h == t)
    {
        h = t = NULL;
        delete p;
        delete r;
    }
    else
    {
        while (p->next != r) p = p->next;
        p->next = NULL;
        t = p;
        delete r;
    }
}

node *searchMS(node *h, long x)
{
    node *p = h;
    while (p && p->info.ms != x)
        p = p->next;
    return p;
}

node *searchTEN(node *h, char a[])
{
    node *p = h;
    while (p && strcmp(p->info.ten, a) != 0)
        p = p->next;
    return p;
}

//Các hàm xử lí linh tinh
void nhap1sv(SV &a)
{
    dem++;
    a.stt = dem;
    printf("\nNhap MSSV: ");
    scanf("%lo", &a.ms);
    fflush(stdin);
    printf("Nhap ho ten: ");
    gets(a.ten);
    fflush(stdin);
    printf("Nhap DTB: ");
    scanf("%f", &a.dtb);
    fflush(stdin);
    do
    {
        printf("Nhap gioi tinh (M/F): ");
        scanf("%c", &a.sex);
        fflush(stdin);
        if (a.sex != 'M' && a.sex != 'F')
            printf("\nNhap sai nhap lai !");
    } while (a.sex != 'M' && a.sex != 'F');
}

void xuat1sv(SV a)
{
    printf("\n\t\t|MSSV: %lo", a.ms);
    printf("\nSV %d",a.stt);
    printf("\t\t|Ten: %s", a.ten);
    printf("\n\t\t|DTB: %.3f", a.dtb);
    if (a.sex == 'F')
        printf("\n\t\t|Gioi tinh: NU");
    else printf("\n\t\t|Gioi tinh: NAM");
    printf("\n");
}

void list(node *&h, node *&t)
{
    int n;
    SV x;
    do
    {
        printf("\nNhap so luong SV: ");
        scanf("%d", &n);
        if (n < 0)    printf("\nNhap sai. Nhap lai !");
    } while (n < 0);
    for (int i = 0; i < n; i++)
    {
        printf("\n------Nhap SV thu %d------", i + 1);
        nhap1sv(x);
        addtail(h, t, x);
        printf("\n");
    }
}

void xuat(node *h)
{
    for (node *p = h; p; p = p->next)
        xuat1sv(p->info);
}

void themdau(node *&h, node *&t)
{
    SV x;
    nhap1sv(x);
    addhead(h, t, x);
}

void themcuoi(node *&h, node *&t)
{
    SV x;
    nhap1sv(x);
    addtail(h, t, x);
}

void themsauTEN(node *&h, node *&t)
{
    SV x;
    node *q;
    char a[41];
    fflush(stdin);
    printf("\nNhap ten can tim: ");
    gets(a);
    q = searchTEN(h, a);
    if (q)
    {
        printf("\nTim thay ! Day la SV so %d\n\n", q->info.stt);
        puts("--NHAP THONG TIN SV MOI--");
        nhap1sv(x);
        addafter(q, t, x);
        dem = 1;
        for (node *p = h; p; p = p->next)
        {
            p->info.stt = dem;
            dem++;
        }
        printf("\nBan vua them 1 SV vao sau SV %d thanh cong ! Danh sach hien tai la:", q->info.stt);
        if (h)
            xuat(h);
        else printf("\n RONG !");
        getch();
    }
    else
    {
        puts("Khong tim thay !");
        getch();
    }
}

void xoaq(node *&h, node *&t)
{
    node *q;
    char a[41];
    fflush(stdin);
    printf("\nNhap ten can tim: ");
    gets(a);
    q = searchTEN(h, a);
    node *p = h;
    if (q == h)
    {
        printf("Ban vua xoa SV dau tien co ten la %s ! Danh sach hien tai la:", h->info.ten);
        delhead(h, t);
        return;
    }
    if (q == t)
    {
        printf("Ban vua xoa SV cuoi cung co ten la %s ! Danh sach hien tai la:", t->info.ten);
        deltail(h, t);
        return;
    }
    while (p && p->next != q)
        p = p->next;
    p->next = q->next;
    printf("Ban vua xoa SV co ten la %s ! Danh sach hien tai la:", q->info.ten);
    delete q;
}

void xoasauTEN(node *&h, node *&t)
{
    node *q;
    char a[41];
    fflush(stdin);
    printf("\nNhap ten can tim: ");
    gets(a);
    q = searchTEN(h, a);
    if (q && q != t)
    {
        printf("\nTim thay ! Day la SV so %d\n\n", q->info.stt);
        printf("\nBan vua xoa SV ten %s ! Danh sach hien tai la: ", q->next->info.ten);
        delafter(q, t);
        dem = 1;
        for (node *p = h; p; p = p->next)
        {
            p->info.stt = dem;
            dem++;
        }
        if (h)
            xuat(h);
        else printf("\n RONG !");
        getch();
    }
    else
    {
        puts("Khong tim thay !");
        getch();
    }
}

void main(void)
{
    node *h = NULL;
    node *t = NULL;
    node *q;
    int chon;
    puts("** TAO DANH SACH SINH VIEN SU DUNG DANH SACH LIEN KET DON **");
    list(h, t);
    system("cls");
    puts("---DANH SACH CAC SINH VIEN VUA MOI TAO---");
    xuat(h);
    puts("Nhan phim bat ki de tiep tuc !");
    getch();
back:
    system("cls");
    puts("---MENU XU LY DANH SACH LIEN KET DON---");
    puts("1. Xem danh sach hien tai.");
    puts("2. Them 1 SV vao dau danh sach.");
    puts("3. Them 1 SV vao cuoi danh sach.");
    puts("4. Them 1 SV ngay sau 1 SV cho truoc.");
    puts("5. Xoa SV dau danh sach.");
    puts("6. Xoa SV cuoi danh sach.");
    puts("7. Xoa 1 SV sau 1 SV cho truoc.");
    puts("8. Xoa 1 SV bat ki.");
    puts("9. Tim kiem 1 SV theo ten.");
    puts("10. Tim kiem 1 SV theo MSSV.");
    puts("");
    printf("\nChon 1 muc: ");
    scanf("%d", &chon);
    switch (chon)
    {
    case 1:
        system("cls");
        puts("--DANH SACH CAC SV HIEN TAI--");
        if (h)
            xuat(h);
        else printf("\n RONG !");
        getch();
        goto back;
    case 2:
        system("cls");
        puts("--NHAP THONG TIN SV MOI--");
        themdau(h, t);
        dem = 1;
        for (node *p = h; p; p = p->next)
        {
            p->info.stt = dem;
            dem++;
        }
        printf("\nBan vua them 1 SV vao dau danh sach thanh cong ! Danh sach hien tai la:");
        if (h)
        xuat(h);
        else printf("\n RONG !");
        getch();
        goto back;
    case 3:
        system("cls");
        puts("--NHAP THONG TIN SV MOI--");
        themcuoi(h, t);
        dem = 1;
        for (node *p = h; p; p = p->next)
        {
            p->info.stt = dem;
            dem++;
        }
        printf("\nBan vua them 1 SV vao cuoi danh sach thanh cong ! Danh sach hien tai la:");
        if (h)
            xuat(h);
        else printf("\n RONG !");
        getch();
        goto back;
    case 4:
        system("cls");
        puts("--TIM TEN SINH VIEN DE THEM VAO SAU--");
        themsauTEN(h, t);
        goto back;
    case 5:
        system("cls");
        printf("Ban vua xoa SV dau tien co ten la %s ! Danh sach hien tai la:", h->info.ten);
        delhead(h, t);
        dem = 1;
        for (node *p = h; p; p = p->next)
        {
            p->info.stt = dem;
            dem++;
        }
        if (h)
            xuat(h);
        else printf("\n RONG !");
        getch();
        goto back;
    case 6:
        system("cls");
        printf("Ban vua xoa SV cuoi cung co ten la %s ! Danh sach hien tai la:", t->info.ten);
        deltail(h, t);
        dem = 1;
        for (node *p = h; p; p = p->next)
        {
            p->info.stt = dem;
            dem++;
        }
        if (h)
            xuat(h);
        else printf("\n RONG !");
        getch();
        goto back;
    case 7:
        system("cls");
        xoasauTEN(h, t);
        goto back;
    case 8:
        system("cls");
        xoaq(h, t);
        dem = 1;
        for (node *p = h; p; p = p->next)
        {
            p->info.stt = dem;
            dem++;
        }
        if (h)
            xuat(h);
        else printf("\n RONG !");
        getch();
        goto back;
    case 9:
       
        char a[41];
        fflush(stdin);
        printf("\nNhap ten can tim: ");
        gets(a);
        q = searchTEN(h, a);
        if (q)
            printf("\nTim Thay !");
        else printf("\nKhong tim thay !");
        getch();
        goto back;
    case 10:
       
        long x;
        fflush(stdin);
        printf("\nNhap MSSV can tim: ");
        scanf("%lo", &x);
        q = searchMS(h, x);
        if (q)
            printf("\nTim Thay !");
        else printf("\nKhong tim thay !");
        getch();
        goto back;
       
       
    }
    getch();
}

Không có nhận xét nào:

Đăng nhận xét