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