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();
}
Không có nhận xét nào:
Đăng nhận xét