[r]
Trang 1Mo ta:
typedef struct {
int key;
float value;
} elem;
//======Tong hop theo khoa key tren dslk khong thu tu======= void tonghop(list &l)
{
nodeptr p, q, c;
if (l.count==0) return;
q = l.head;
while (q!=NULL)
{
p = q;
c = q->next;
while (c!=NULL) {
if (q->data.key==c->data.key) {
q->data.value+=c->data.value;
if (c->next==NULL) l.rear = p; p->next = c->next;
delete c;
l.count ;
}
Trang 2p = c;
c = p->next;
}
q = q->next;
}
}
//======Tong hop theo khoa key tren dslk co thu tu======= void tonghoptt(list &l)
{
nodeptr q, c;
if (l.count==0) return;
q = l.head;
while (q!=NULL)
{
c = q->next;
while (c!=NULL && q->data.key==c->data.key) {
q->data.value+=c->data.value;
if (c->next==NULL) l.rear = q;
q->next = c->next;
delete c;
l.count ;
c = q->next;
Trang 3q = c;
}
}
//======Tach dslk thanh 2 dslk theo 1 dieu kien ======== void tach(list &l, list &l1, list &l2)
{
nodeptr c, c1, c2, q1, q2;
l1.count = l2.count = 0;
c1 = q1 = new node;
c2 = q2 = new node;
c = l.head;
while (c!=NULL)
{
if (c->data.value>=5) {
c1->next = c;
c1 = c;
l1.count++;
} else {
c2->next = c;
c2 = c;
l2.count++;
Trang 4c = c->next;
}
c1->next = c2->next = NULL;
l1.head = q1->next;
l2.head = q2->next;
l1.rear = (l1.head==NULL? NULL:c1);
l2.rear = (l2.head==NULL? NULL:c2);
delete q1;
delete q2;
l.head = l.rear =NULL;
l.count = 0;
}
//Tach dslk thanh 2 dslk theo 1 dieu kien tong quat hon void tach(list &l, list &l1, int (*cond(elem))
{
nodeptr c, c1, c2, q1, q2;
int n=0;
l1.count = l2.count = 0;
c1 = q1 = new node;
c2 = q2 = new node;
c = l.head;
while (c!=NULL)
{
Trang 5if (cond(c->data)) {
c1->next = c;
c1 = c;
l1.count++;
} else {
c2->next = c;
c2 = c;
n++;
}
c = c->next;
}
c1->next = c2->next = NULL;
l1.head = q1->next;
l.head = q2->next;
l1.rear = (l1.head==NULL? NULL:c1); l.rear = (l.head==NULL? NULL:c2); delete q1;
delete q2;
}
// Khi su dung ham tach phai viet ham dieu kien int kem(elem x)
Trang 6return x.value<4; }
int tb(elem x)
{
return x.value<6; }
int kha(elem x)
{
return x.value<8; }
list l, lk, lkha, ltb; // Loi goi de tach
tach(l, lk, kem); tach(l, ltb, tb); tach(l, lkha, kha);