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

typedef int kieuphantu;
typedef struct nut{
kieuphantu phantu;
nut *ke;
};
typedef nut* vitri;
typedef vitri danhsach;

void khoitao(danhsach *dau){
*dau = (nut*)malloc(sizeof(nut));
(*dau)->ke =NULL;
}
int rong(danhsach L){
return (L->ke == NULL);
}
vitri ke(vitri p, danhsach L){
return p->ke;
}
kieuphantu giatritrave(vitri p, danhsach L){
return p->ke->phantu;
}
vitri dau(danhsach L){
return L;
}

vitri cuoi(danhsach L){
vitri p=L;
while (p->ke!= NULL){
p=p->ke;
return p;
}
}
nut* taonut(kieuphantu x){
nut *n;
n = (nut*)malloc(sizeof(nut));
n->phantu = x;
n->ke = NULL;
return n;
}
int dodai(danhsach L){
int so = 0;
vitri p=dau(L);
while(p!=cuoi(L)){
p= p->ke;
so++;
}
return so;
}
void them(kieuphantu x, vitri p, danhsach *L){
nut *nutmoi = taonut(x);
nutmoi->ke =p->ke;
p->ke = nutmoi;
}
void nhap(danhsach *L){
int n;
kieuphantu x;
do{
printf("\n So phan tu cua danh sach: ");
scanf("%d", &n);
}while(n<1);
for(int i=1; i<=n; i++){
printf("\n Phan tu thu %d =",i);
scanf("%d", &x);
them(x, cuoi(*L), L);
}
}

void in(danhsach L){
if(rong(L))
printf("\n Danh sach rong, khong co phan tu nao!!");
else{
vitri p = L;
printf("\n\n\t Phan tu danh sach: ");
while(p!= cuoi(L)){
printf("\t %d", giatritrave(p, L));
p=p->ke;
}
}
printf("\n");
}

void xoa(vitri p, danhsach L){
if(rong(L))
printf("\n Danh sach rong!!");

else{
vitri t;
t = p->ke;
p->ke=t->ke;
free(t);
}
}
void thaythe(danhsach *L){
kieuphantu x,y;
printf("\n\t Nhap gia tri x: ");
scanf("%d",&x);
printf("\n\t Nhap gia tri y: ");
scanf("%d",&y);
vitri p=dau(*L);
while(p!= cuoi(*L)){
if(giatritrave(p,*L)==x){
them(y, ke(p, *L), L);
xoa(p,*L);
break;
}else
p= ke(p, *L);
}
if(p==cuoi(*L))
printf("\n khong co gia tri %d trong danh sach", x);
}

main(){
danhsach L;
khoitao(&L);
nhap(&L);
in(L);
thaythe(&L);
in(L);
}