Professional Documents
Culture Documents
cout<<"The list empty"<<endl; return; } pNode p=l.head; while(p) { cout<<p->key<<"->"; p=p->next; } cout<<"NULL"<<endl; }
Xa 1 nt u danh sch
void { DeleteHead(LinkList &l) if(isEmpty(l)) return; pNode p=l.head; if(l.head==l.tail) { delete p; l.head=l.tail=NULL; return; } l.head=l.head->next; p->next=NULL; delete p; }
Tm kim tun t
int LinearSearch(LinkList l,int k) { pNode p=l.head; if (isEmpty(l)) return 0; while (p) { if (p->key==k) return k; p=p->next; } return 0; }
Sp xp chn trc tip (Selection Sort) (hon v ni dung phn d liu - key)
void { SelectionSort(LinkList &l) pNode min; for (pNode p=l.head; p->next!=NULL; p=p->next) { min = p; for(pNode q = p->next; q; q=q>next) if (q->key < min->key) min = q; if (min != p) swap(min->key,p->key); } }
Xa 1 nt c kha k
void DeleteK(LinkList &l, int k) { pNode p=l.head,q=NULL; while((p!=NULL)&&(p->key!=k)) { q=p; p=p->next; } if(p==NULL) return; if(q!=NULL) { if(p==l.tail) { l.tail=q; l.tail->next=NULL; } q->next=p->next; delete p; } else DeleteHead(l); }
while (q->next!=pos) q=q->next; if (q->key > x->key) { pos->key = q->key; pNode r=l.head; while(r->next!=pos) r=r->next; pos=r; } else break; } pos->key = x->key; } }
Sp xp ni bt (Bubble Sort)
void BubbleSort(LinkList &l) { pNode p,q; for (p = l.head ; p->next!=NULL ; p=p->next) for (q=p->next; q->next!=NULL ; q=q->next) if(q->key > q->next->key) swap(q->key, q->next->key);