conlaitungnay.nguyenanhque

Màu nền
Font chữ
Font size
Chiều cao dòng

 

void xoasoam(ds* tro_ds)

{

sn* tg= tro_ds->infor;

sn* tgnext= tg->next;

 

while(tg->Item< 0 && tg->next!= NULL) //xoa lien tiep cac phan tu am o dau danh sach

{

cout<<"Lap 1"<<endl;

tro_ds->infor= tg->next;

tg= tg->next;

tro_ds->count--;

}

 

///toi day, tg tro toi phan tu duong dau tien trong day

 

while(tg->next!= NULL)

{

tgnext= tg->next;

//cout<<"Lap 2"<<endl;

while(tgnext->Item< 0 && tgnext->next!= NULL) {tgnext= tgnext->next; tro_ds->count--; }  //tim so duong tiep theo

 

tg->next= tgnext;

tg= tg->next;   //tg tro vao phan tu duong tiep theo

}

}

 

void sapxep(ds* tro_ds)  ///sap xep tang dan bang phuong phap select sort;

{

sn* tg= tro_ds->infor;  //phan tu hien tai

sn* tg_chay;          //tim kiem nho nhat

sn* min;

while(tg->next!= NULL)

{

tg_chay= tg;

min= tg_chay;

while(tg_chay->next!= NULL)

{

if(tg_chay->Item< min->Item) min= tg_chay;

tg_chay= tg_chay->next;

}

//////doi cho

int trung_gian= tg->Item;

tg->Item= min->Item;

min->Item= trung_gian;

tg= tg->next;

}

}

 

int boxungsort(ds* tro_ds,int X)   ///sap xep tang dan

{

sn* tg= tro_ds->infor;

sn* pre_tg= NULL; //phan tu truoc tg

sn* p= new sn; //phan tu moi

 

p->Item= X;

 

if(tg->Item> X)

{

p->next= tg;

tro_ds->infor= p;

tro_ds->count++;

return 0;  //bo xung thanh cong

}

else

{

pre_tg= tg;

tg->next;

}

 

while(tg->next!= NULL && tg->Item<X)

{

pre_tg= tg;

tg= tg->next;

}

if(tg->next== NULL) return 0;

else

{

pre_tg->next= p;

p->next= tg;

return 1;

}

}

 

void infor(ds* tro_ds)

{

int am= 0;

int duong= 0;

sn* tg= tro_ds->infor;

 

while(tg->next!= NULL)

if(tg->Item<0) am++;

else duong++;

 

cout<<"

So phan tu cua danh sach: "<<tro_ds->count<<endl;

cout<<"So gia phan tu am: "<<am<<endl;

cout<<"So gia tri duong: "<<duong<<endl;

}

Bạn đang đọc truyện trên: Truyen2U.Pro