You are on page 1of 4

Ch ng 4: DANH SCH LIN K T

4.1 Danh sch lin kt n


Khai bo
struct { int Node }; typedef struct { }; Node LinkList pNode head,tail; *pNode; Node key; *next;

In danh sch ra mn hnh


void { Print(LinkList if { l) (isEmpty(l)) is

Kim tra danh sch rng


int { } isEmpty(LinkList l) return (l.head==NULL)

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; }

Khi to danh sch


void { } Initial(LinkList &l) l.head = l.tail = NULL;

Thm 1 nt vo u danh sch


void { InsertHead(LinkList &l, int k) pNode p = new Node; if (p==NULL) return; p -> key = k; p -> next = NULL; p -> next = l.head; l.head = p; if (l.tail == NULL) l.tail = p; }

Xa 1 nt cui danh sch


void { DeleteTail(LinkList &l) if(isEmpty(l)) return; pNode p=l.head,q=l.tail; if(l.head==l.tail) { delete p; l.head=l.tail=NULL; return; } while(p->next!=l.tail) p=p->next; p->next=NULL; l.tail=p; delete q; }

Thm 1 nt vo cui danh sch


void { InsertTail(LinkList &l,int k) pNode p=new Node; if (!p) return; p->key=k; p->next=NULL; if (l.tail==NULL) l.tail=p; else { l.tail->next=p; l.tail=p; } if (l.head==NULL) l.head=p; }

Hy ton b danh sch


void { Delete(LinkList &l) if(l.head) delete l.head; l.head=l.tail=NULL; }

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); }

(thay i mi lin kt)


void ListSelectionSort(LinkList &l) { pNode i,j,min,minpre=NULL; LinkList lresult; Initial(lresult); while(l.head!=NULL) { min=l.head; minpre=NULL; for(j=min, i=min->next; i!=NULL; j=i, i=i->next) if(i->key < min->key) { min=i; minpre=j; } if(minpre==NULL) { l.head=l.head->next; if(min==l.tail) l.tail=NULL; } else { if(min==l.tail) l.tail=minpre; minpre->next=min->next; } min->next=NULL; InsertTail(lresult,min->key); } l=lresult; }

Thm mt phn t kha k vo sau m t phn t cho trc


void InsertAfter(LinkList q,int k) { if(isEmpty(l)) { InsertHead(l,k); return; } if(q==NULL) return; if(q==l.tail) { InsertTail(l,k); return; } pNode p=new Node; if (!p) return; p->next=q->next; p->key=k; q->next=p; } &l,pNode

Sp xp chn trc tip (InsertionSort)


void InsertionSort(LinkList &l) { pNode pos; pNode x = new Node; for(pNode p=l.head->next;p;p=p->next) { x->key = p->key; pos=p; while(pos!=l.head) { pNode q=l.head;

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);

You might also like