Hiển thị các bài đăng có nhãn Bải tập thực hành môn Cấu trúc dữ liệu. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn Bải tập thực hành môn Cấu trúc dữ liệu. Hiển thị tất cả bài đăng

Thứ Ba, 29 tháng 12, 2015

Chương trình tổng hợp các bài toán cơ bản về cây tìm kiếm nhị phân

#include<stdio.h>
#include<conio.h>

typedef struct tree
{
    int info;
    tree *l;
    tree *r;
};
   
tree *getnode(int x)
{
    tree *p=new tree;
    if(!p) return NULL;
    p->info= x;
    p->l=NULL;
    p->r=NULL;
    return p;
}

void addx(tree *&t, int x)
{
    if(!t)
    {
        t=getnode(x);
        return;
    }
    if(t->info ==x) return;
    if(t->info>x)
        addx(t->l,x);
    else   
        addx(t->r,x);
}

void list(tree *&t)
{
    int n,x;
    do
    {
        printf("\nNhap n: ");
        scanf("%d",&n);
        if(n<0)    printf("\nNhap sai Nhap lai");
    }while(n<0);
   
    for(int i=0; i<n; i++)
    {
        printf("\nNhap x: ");
        scanf("%d",&x);
        addx(t,x);
    }
}

void NLR(tree *t)
{
   
    if(!t) return;
    printf("%5d",t->info);   
    NLR(t->l);
    NLR(t->r);
}

void LNR(tree *t)
{
   
    if(!t) return;
    LNR(t->l);
    printf("%5d",t->info);   
    LNR(t->r);
}

void LRN(tree *t)
{
   
    if(!t) return;
    LRN(t->l);
    LRN(t->r);
    printf("%5d",t->info);   
}

tree *timx(tree *t, int x)
{
    if(!t) return NULL;
    if(t->info==x) return t;
    if(t->info >x)
            return(timx(t->l,x));
    return(timx(t->r,x));
}

tree *timx2(tree *t, int x)
{
    tree *p=t;
    if(!t) return NULL;
    while(p && p->info !=x)
    {
        if(p->info >x)
            p=p->l;
        else p=p->r;
    }
    return p;
}

void calltimx(tree *t)
{
    int x;
    printf("\nNhap x can tim: ");
    scanf("%d",&x);
    if(!t)
        printf("\nCay rong");
    else
    {
        tree *p=timx(t,x);
        if(!p)
            printf("\nKhong tim thay");
        else printf("\nTim thay");
    }
}

int ktsnt(int n)
{
    if(n<2) return 0;
    if(n==2) return 1;
    for(int i =2; i*i<=n; i++)
        if(n%i == 0) return 0;
    return 1;
}

void ktam(tree *t)
{
    if(!t) return;
    if(t->info<0)
        printf("%5d",t->info);
    ktam(t->l);
    ktam(t->r);
}

void ktduong(tree *t)
{
    if(!t) return;
    if(t->info>0)
        printf("%5d",t->info);
    ktduong(t->l);
    ktduong(t->r);
}

void ktchan(tree *t)
{
    if(!t) return;
    if(t->info%2==0)
        printf("%5d",t->info);
    ktchan(t->l);
    ktchan(t->r);
}

void ktle(tree *t)
{
    if(!t) return;
    if(t->info%2!=0)
        printf("%5d",t->info);
    ktle(t->l);
    ktle(t->r);
}

void xuatsnt(tree *t)
{
    if(!t) return;
    if(ktsnt(t->info))
        printf("%5d",t->info);
    xuatsnt(t->l);
    xuatsnt(t->r);
}

int tong(tree *t)
{
    if(!t) return 0;
    return t->info + tong(t->l) +tong(t->r);
}
   
int tongam(tree *t)
{
    if(!t) return 0;
    if(t->info <0)
        return t->info + tongam(t->l) + tongam(t->r);
    return tongam(t->l) + tongam(t->r);
}

int tongduong(tree *t)
{
    if(!t) return 0;
    if(t->info >0)
        return t->info + tongduong(t->l) + tongduong(t->r);
    return tongduong(t->l) + tongduong(t->r);
}

int tongchan(tree *t)
{
    if(!t) return 0;
    if(t->info %2 == 0)
        return t->info + tongchan(t->l) + tongchan(t->r);
    return tongchan(t->l) + tongchan(t->r);
}

int tongle(tree *t)
{
    if(!t) return 0;
    if(t->info %2 != 0)
        return t->info + tongle(t->l) + tongle(t->r);
    return tongle(t->l) + tongle(t->r);
}

int tongsnt(tree *t)
{
    if(!t) return 0;
    if(ktsnt(t->info))
        return t->info + tongsnt(t->l) + tongsnt(t->r);
    return tongsnt(t->l) + tongsnt(t->r);
}

int demnode(tree *t)
{
    if(!t) return 0;
    return 1+ demnode(t->l)+demnode(t->r);
}

int demnodela(tree *t)
{
    if(!t) return 0;
    if(t->l == NULL && t->r == NULL)
        return 1+demnodela(t->l) + demnodela(t->r);
    return demnodela(t->l) + demnodela(t->r);
}

int demnodeam(tree *t)
{
    if(!t) return 0;
    if(t->info <0)
        return 1+ demnodeam(t->l)+ demnodeam(t->r);
    return demnodeam(t->l)+ demnodeam(t->r);
}

int demnodeduong(tree *t)
{
    if(!t) return 0;
    if(t->info >0)
        return 1+ demnodeduong(t->l)+ demnodeduong(t->r);
    return demnodeduong(t->l)+ demnodeduong(t->r);
}

int demnodechan(tree *t)
{
    if(!t) return 0;
    if(t->info %2 == 0)
        return 1 + demnodechan(t->l) + demnodechan(t->r);
    return demnodechan(t->l) + demnodechan(t->r);
}

int demnodele(tree *t)
{
    if(!t) return 0;
    if(t->info %2 != 0)
        return 1 + demnodele(t->l) + demnodele(t->r);
    return demnodele(t->l) + demnodele(t->r);
}

int demnodesnt(tree *t)
{
    if(!t) return 0;
    if(ktsnt(t->info))
        return 1 + demnodesnt(t->l) + demnodesnt(t->r);
    return demnodesnt(t->l) + demnodesnt(t->r);
}

int max(tree *t)
{
    tree *p=t;
    while(p->r)
        p=p->r;
    return p->info;
}

int min(tree *t)
{
    tree *p=t;
    while(p->l)
        p=p->l;
    return p->info;
}

tree *nodemax(tree *t)
{
    tree *p=t;
    while(p->r)
        p=p->r;
    return p;
}

tree *nodemin(tree *t)
{
    tree *p=t;
    while(p->l)
        p=p->l;
    return p;
}

int demnodelonx(tree *t, int x)
{
    if(!t) return 0;
    if(t->info >x)
        return 1+demnodelonx(t->l,x) + demnodelonx(t->r,x);
    return demnodelonx(t->l,x) + demnodelonx(t->r,x);
}

int demnodenhox(tree *t, int x)
{
    if(!t) return 0;
    if(t->info <x)
        return 1+demnodenhox(t->l,x) + demnodenhox(t->r,x);
    return demnodenhox(t->l,x) + demnodenhox(t->r,x);
}

int Max(int a, int b)
{
    return a>b?a:b;
}

int height(tree *t)
{
    if(t)
        return 1+ Max(height(t->l), height(t->r));
    return 0;
}

int xuatxy(tree *t, int x, int y)
{
    if(!t) return 0;
    if(t->info>=x && t->info <=y)
        return 1+xuatxy(t->l,x,y)+xuatxy(t->r,x,y);
    return xuatxy(t->l,x,y)+xuatxy(t->r,x,y);
}

void xuatxy1(tree *t)
{
    int x,y;
    printf("\nNhap x: ");
    scanf("%d",&x);
    printf("\nNhap y: ");
    scanf("%d",&y);
    printf("\nSo nut tu x den y trong cay la: %d",xuatxy(t,x,y));
}


void deltree(tree *&t)
{
    if(!t) return;
    deltree(t->l);
    deltree(t->r);
    delete t;
}
   


void main(void)
{
    tree *t =NULL;
    list(t);
    printf("\nDuyet NLR: ");
    NLR(t);
    printf("\nDuyet LNR: ");
    LNR(t);
    printf("\nDuyet LRN: ");
    LRN(t);
    calltimx(t);
    printf("\nCac so duong: ");
    ktduong(t);
    printf("\nCac so am: ");
    ktam(t);
    printf("\nCac so chan: ");
    ktchan(t);
    printf("\nCac so le: ");
    ktle(t);
    printf("\nCac so nguyen to: ");
    xuatsnt(t);
    printf("\nTong cac so  duong trong cay: %d",tongduong(t));
    printf("\nTong cac so le trong cay: %d",tongle(t));
    printf("\nTong cac so chan trong cay: %d",tongchan(t));
    printf("\nTong cac so am trong cay: %d",tongam(t));
    printf("\nTong cac so nt trong cay: %d",tongsnt(t));
    printf("\nTong so node tren cay: %d",demnode(t));
    printf("\nTong so node la tren cay: %d",demnodela(t));
    printf("\nTong cac so node duong trong cay: %d",demnodeduong(t));
    printf("\nTong cac so node le trong cay: %d",demnodele(t));
    printf("\nTong cac so node chan trong cay: %d",demnodechan(t));
    printf("\nTong cac so node am trong cay:%d",demnodeam(t));
    printf("\nTong cac so node nt trong cay: %d",demnodesnt(t));
    printf("\nMax: %d",max(t));
    printf("\nMin: %d",min(t));
    int x;
    printf("\nNhap x can tim: ");
    scanf("%d",&x);
    printf("\nTong cac node lon hon x: %d",demnodelonx(t,x));
    printf("\nTong cac node nho hon x: %d",demnodenhox(t,x));
    printf("\nChieu cao cua cay: %d",height(t));
    xuatxy(t);
    deltree(t);
    if(t)
        printf("\nCay da xoa !");
    xuatxy1(t);
    getch();
}

Thứ Sáu, 25 tháng 12, 2015

Chương trình mô phỏng bài toán Tháp Hà Nội sử dụng đệ quy.



Lưu ý: Code này chỉ chạy được trên Borland C !

#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>

#define move(x,y,z) makeblocks(x,y,z); delay(t); delblocks(x, y, z);

int t;
int a[] = { 0, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25 };
int color[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
int bg = 0;


void makeblocks(int x, int y, int z)
{
    int h = a[z] / 2;
    window(x - h, y, x + h, y);
    textbackground(color[z]);
    textcolor(color[z]);
    clrscr();
}

void delblocks(int x, int y, int z)
{
    int h = a[z] / 2;
    window(x - h, y, x + h, y);
    textbackground(bg);
    clrscr();
}

void tower(int x, int y, int m)
{
    for (int i = m; i >= 1; i--)
        makeblocks(x, y - m + i, i);
}

void moveblocks(int x1, int y1, int x2, int y2, int z)
{
    static int count = 0;
    int x, y;
    count++;
    window(1, 1, 24, 1);
    textbackground(BLUE);
    clrscr();
    textcolor(WHITE);
    cprintf("\nLan dich chuyen thu: ");
    textcolor(YELLOW + BLINK);
           cprintf("%4d", count);
    for (y = y1; y >= 4; y--)
    {
        move(x1, y, z);
        }
    if (x1<x2)
    for (x = x1; x <= x2; x++)
    {
        move(x, 4, z);
    }
    else
    for (x = x1; x >= x2; x--)
    {
        move(x, 4, z );
    }
    for (y = 4; y <= y2; y++)
    {
        move(x2, y, z);
    }
    makeblocks(x2, y2,z);
}

void movetower(int x1, int y1, int x2, int y2, int x3, int y3, int m)
{
    if (m < 1)    return;
    else
    if (m == 1)
        moveblocks(x1, y1, x2, y2, 1);
    else
    {
        movetower(x1, y1 - 1, x3, y3, x2, y2, m - 1);
        moveblocks(x1, y1, x2, y2, m);
        movetower(x3, y3, x2, y2 - 1, x1, y1, m - 1);
    }
}

void main(void)
{
    int m;
    int x1, y1, x2, y2, x3, y3;
    x1 = 13;
    x2 = 40;
    x3 = 67;
    y1 = y2 = y3 = 24;
    textmode(C80);
    clrscr();
    printf("\nSo tang: ");
    scanf("%d", &m);
    printf("\nTime delay: ");
    scanf("%d", &t);
    textbackground(bg);
    clrscr();
    tower(x1, y1, m);
        getch();
    movetower(x1, y1, x2, y2, x3, y3, m);
    getch();
    window(1, 1, 80, 25);
    textbackground(bg);
    textcolor(WHITE);
    clrscr();

}

Thứ Sáu, 18 tháng 12, 2015

Viết chương trình sử dụng danh sách liên kết đơn xử lí các công việc sau.

Công việc:
1. Nhập/xuất random danh sách các số nguyên gồm n số, mỗi số mới thêm vào cuối dánh sách.
2. Thêm vào đầu 1 số random.
3. Xóa số đầu danh sách.
4. Xóa 1 số sau số X đầu tiên tùy chọn (nếu có)
5. Xóa hết các số X tùy chọn.
6. Xóa 1 số sau số MIN đầu tiên (nếu được)
7. Xóa các số sau số MAX (nếu được)
8. Xóa hết các số MAX.
9. Xóa số gần kế cuối (nếu đượ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<stdlib.h>

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

node *getnode(int x)
{
node *p = new node;
if (!p) return NULL;
p->info = x;
p->next = NULL;
return p;
}

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

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

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

node *search(node *h, int x)
{
node *p = h;
while (p && p->info != x)
p = p->next;
return p;
}

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

void delX(node *&h, node *&t, int x)
{
if (h->info == x)
{
delhead(h, t);
return;
}
node *p = h;
while (p->next != NULL && p->next->info != x)
p = p->next;
if (p->next->info == x)
delafterX(p, t);
}

int min(node *h)
{
int min = h->info;
for (node *p = h; p; p=p->next)
if (min > p->info)
min = p->info;
return min;
}

int max(node *h)
{
int max = h->info;
for (node *p = h; p; p = p->next)
if (max < p->info)
max = p->info;
return max;
}

//

void list(node *&h, node *&t)
{
int x, n;
do
{
printf("\nNhap so phan tu: ");
scanf("%d", &n);
if (n < 0)
printf("\nNhap sai, nhap lai.");
} while (n < 0);

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

x = rand() % 10;
addtail(h, t, x);
}

}

void xuat(node *h)
{
printf("\nDanh sach hien tai: ");
if (!h)
{
printf("Rong!");
return;
}

for (node *p = h; p; p = p->next)
printf("%5d", p->info);
}

void themdau(node *&h, node *&t)
{
int x;
printf("\nNhap so muon them vao dau: ");
scanf("%d", &x);
addhead(h, t, x);
}

void xoadau(node *&h, node *&t)
{
printf("\nDa xoa 1 phan tu dau danh sach !");
delhead(h, t);
}

void xoasauX(node *h, node *&t)
{
int x;
node *q;
printf("\nNhap so can tim: ");
scanf("%d", &x);
q = search(h, x);
if (q && q != t)
{
printf("\nDa xoa phan tu sau X !");
delafterX(q, t);
}
else printf("\nKhong tim thay hoac X la phan tu cuoi cung !");
}

void xoahetX(node *&h, node *&t)
{
int x;
printf("\nNhap so can tim: ");
scanf("%d", &x);
node *p = h;
while (p)
{
if (p->info == x)
{
p = p->next;
delX(h, t, x);
continue;
}
p = p->next;
}
}

void xoasaumin(node *h, node *&t)
{
int Min = min(h);
node *p = search(h, Min);
if (p && p != t)
{
printf("\nDa xoa 1 phan tu sau min dau tien!");
delafterX(p, t);
}
else printf("\nMin la tail !");

}

void xoahetsaumax(node *h, node *&t)
{
int Max = max(h);
node *p = h;
while (p && p!=t)
{
if (p->info == Max)
delafterX(p, t);
p = p->next;
}
}

void xoahetmax(node *&h, node *&t)
{
int Max = max(h);
node *p = h;
while (p && p != t)
while (p)
{
if (p->info == Max)
{
p = p->next;
delX(h, t, Max);
continue;
}
p = p->next;
}
}

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

}

////////////////////////////////////
void main(void)
{
node *h = NULL;
node *t = NULL;
list(h, t);
xuat(h);
themdau(h, t);
xuat(h);

xoadau(h, t);
xuat(h);

xoasauX(h, t);
xuat(h);

xoahetX(h, t);
xuat(h);

xoasaumin(h, t);
xuat(h);

xoahetsaumax(h, t);
xuat(h);

xoahetmax(h, t);
xuat(h);

xoasogancuoi(h, t);
xuat(h);
getch();
}

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();
}

Thứ Ba, 15 tháng 12, 2015

Chương trình tổng hợp các bài cơ bản của danh sách liên kết đơn SỐ NGUYÊN

 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<stdlib.h>

typedef struct node  //tạo kiểu của danh sách liên kết đơn
{
    int info;
    node *next;
};

node *getnode(int x)  //tạo nút mới
{
    node *p = new node;
    if (!p)    return NULL;
    p->info = x;
    p->next = NULL;
    return p;
}

void addhead(node *&h, node *&t, int x)  //thêm vào đầu danh sách
{
    node *p = getnode(x);
    if (!h)
        h = t = p;
    else
    {
        p->next = h;
        h = p;
    }
}

void addtail(node *&h, node *&t, int x) //thêm vào cuối danh sách
{
    node *p = getnode(x);
    if (!h)
        h = t = p;
    else
    {
        t->next = p;
        t = p;
    }
}

void addafter(node *q, node *&t, int x) //thêm nút vào sau 1 nút
{
    node *p = getnode(x);
    p->next = q->next;
    q->next = p;
    if (q == t)
        t = p;
}

void delhead(node *&h, node *&t) //xóa nút đầu danh sách
{
    if (!h)    return;
    node *p = h;
    h = p->next;
    delete p;
    if (!h)    t = NULL;
}

void delafter(node *q, node *&t) //xóa nút sau 1 nút
{
    node *p = q->next;
    q->next = p->next;
    delete p;
    if (q->next == NULL)
        t = q;
}

node *search(node *h, int x)
{
    node *p = h;
    while (p && p->info != x)
        p = p->next;
    return p;
}

void list(node *&h, node *&t) //tạo danh sách random
{
    int n, x;
    do
    {
        printf("\nNhap so phan tu: ");
        scanf("%d", &n);
        if (n < 0)
            printf("\nNhap sai. Nhap lai.");
    } while (n < 0);
    for (int i = 0; i < n; i++)
    {
        x = rand() % 10;  //Random số từ 0 - 10
        addtail(h, t, x);
    }
}

void xuat(node *h) //xuất danh sách
{
    for (node *p = h; p; p = p->next)
        printf("%5d", p->info);
}

void themtruoc(node *&h, node *&t)
{
    int x = rand() % 10;
    addhead(h, t, x);
    printf("\nBan vua them so %d vao dau danh sach !", x);
}

void themcuoi(node *&h, node *&t)
{
    int x = rand() % 10;
    addtail(h, t, x);
    printf("\nBan vua them so %d vao cuoi danh sach !", x);
}

void themsaux(node *h, node *t)
{
    int x, s;
    node *p;
    printf("\nNhap so can tim de them vao sau: ");
    scanf("%d", &x);
    p = search(h, x);
    if (p)
    {
        s = rand() % 10;
        printf("\nBan vua them sau so %d so %d", x, s);
        addafter(p, t, s);
    }
    else
        printf("\nKhong the them duoc vi tim khong thay so can tim !");
   
}

void xoasaux(node *h, node *&t)
{
    int x;
    node *p;
    printf("\nNhap so can tim de xoa so sau no: ");
    scanf("%d", &x);
    p = search(h, x);
    if (p && p != t)
    {
        printf("\nBan xoa so %d", p->next->info);
        delafter(p, t);
    }
    else
        printf("\nKhong the xoa duoc vi tim khong thay so can tim hoac da o cuoi danh sach !");

}

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

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

void xoax(node *&h, node *&t)
{
    int x;
    node *p;
    printf("\nNhap so can tim de xoa: ");
    scanf("%d", &x);
    p = search(h, x);
    if (!p)
    {
        printf("\nDeo thay, deo xoa.");
        return;
    }
    if (p == h)
    {
        printf("\nBan vua xoa so %d !", h->info);
        delhead(h, t);
        return;
    }
    if (p == t)
    {
        printf("\nBan vua xoa so %d !", t->info);
        xoacuoi(h, t);
        return;
    }
    for (node *q = h; q; q->next)
        if (q->next == p)
        {
            q->next = p->next;
            p->next = NULL;
            printf("\nBan vua xoa so %d !", p->info);
            delete p;
            return;
        }
   
}

void main(void)
{
    node *h = NULL;
    node *t = NULL;
    int chon;
    puts("----TAO DANH SACH LIEN KET----");
    list(h, t);
    puts("Cac phan tu vua duoc tao la:\n ");
    xuat(h);
    puts("\n\nEnter de tiep tuc !");
    getch();
back:
    system("cls");
    printf("\nDANH SACH HIEN TAI: ");
    if (!h)
        printf("RONG !");
    else
    xuat(h);
    puts("\n\n----MENU CHUONG TRINH THUC HIEN CAC THAO TAC CUA DANH SACH LIEN KET DON----");
    puts("1. Them ngau nhien 1 so vao dau danh sach.");
    puts("2. Them ngau nhien 1 so vao cuoi danh sach.");
    puts("3. Them ngau nhien 1 so sau 1 so cho truoc.");
    puts("4. Xoa so dau tien cua danh sach.");
    puts("5. Xoa 1 so sau 1 so cho truoc.");
    puts("6. Xoa so cuoi cung cua danh sach.");
    puts("7. Xoa het danh sach.");
    puts("8. Xoa 1 so cho truoc.");
    printf("\nChon 1 so: ");
    scanf("%d", &chon);
    switch (chon)
    {
    case 1:
        system("cls");
        themtruoc(h, t);
        puts("\nDanh sach hien tai la: \n");
        if (!h)
            printf("RONG !");
        else
            xuat(h);
        getch();
        goto back;
    case 2:
        system("cls");
        themcuoi(h, t);
        puts("\nDanh sach hien tai la: \n");
        if (!h)
            printf("RONG !");
        else
            xuat(h);
        getch();
        goto back;
    case 3:
        system("cls");
        themsaux(h, t);
        puts("\nDanh sach hien tai la: \n");
        if (!h)
            printf("RONG !");
        else
            xuat(h);
        getch();
        goto back;
    case 4:
        system("cls");
        printf("\nBan vua xoa so %d !", h->info);
        delhead(h, t);
        puts("\nDanh sach hien tai la: \n");
        if (!h)
            printf("RONG !");
        else
            xuat(h);
        getch();
        goto back;
    case 5:
        system("cls");
        xoasaux(h, t);
        puts("\nDanh sach hien tai la: \n");
        if (!h)
            printf("RONG !");
        else
            xuat(h);
        getch();
        goto back;
    case 6:
        system("cls");
        if (!h)
            printf("\nXoa cung nhu khong !");
        else
        printf("\nBan vua xoa so %d", t->info);
        xoacuoi(h, t);
        puts("\nDanh sach hien tai la: \n");
        if (!h)
            printf("RONG !");
        else
            xuat(h);
        getch();
        goto back;
    case 7:
        system("cls");
        if (!h)
            printf("\nXoa cung nhu khong !");
        else
            printf("\nXoa het cmnr !");
        xoahet(h, t);
        puts("\nDanh sach hien tai la: \n");
        if (!h)
            printf("RONG !");
        else
            xuat(h);
        getch();
        goto back;
    case 8:
        system("cls");
        xoax(h, t);
        puts("\nDanh sach hien tai la: \n");
        if (!h)
            printf("RONG !");
        else
            xuat(h);
        getch();
        goto back;
    }


    getch();
}