You are on page 1of 5

DANH SCH LIN KT

MC TIU
Hon tt bi thc hnh ny, sinh vin c th: Hiu c cc thnh phn ca danh sch lin kt. Thnh tho cc thao tc trn danh sch lin kt: thm phn t, xa phn t, duyt danh sch lin kt. p dng cu trc d liu danh sch lin kt vo vic gii quyt mt s bi ton n gin.

Thi gian thc hnh: t 120 pht n 400 pht

TM TT
Danh sch lin kt l cu trc d liu dng lu tr mt danh sch (tp hp hu hn) d liu. im c bit ca cu trc ny l kh nng cha ca n ng (c th m rng v thu hp d dng). C cc loi danh sch lin kt: Danh sch lin kt n Danh sch lin kt kp Danh sch lin kt vng

Mi danh sch lin kt l tp hp cc phn t (node) cha thng tin lu tr ca d liu. Gia cc phn t c mt hoc nhiu lin kt m bo danh sch lin kt c th gi cc phn t ny mt cch cht ch. V d 1:

Phn t c mt lin kt V d 2:

Phn t c hai lin kt

Phn t rng Danh sch lin kt n Danh sch lin kt kp Danh sch lin kt vng

Trong mi phn t ca danh sch lin kt, thng tin lin kt l v cng quan trng. Ch cn mt x l khng cn thn c th lm mt phn lin kt ny th danh sch lin kt s b gy t phn t (khng th truy xut tip cc phn t t phn t tr v trc hoc tr v sau). Cc thao tc c bn trn danh sch lin kt: Thm phn t: vo u danh sch lin kt, vo cui danh sch lin kt, vo trc/sau mt phn t trn danh sch lin kt. Xa phn t: u danh sch lin kt, cui danh sch lin kt, mt phn t trn danh sch lin kt. Duyt danh sch lin kt: c th i c ht cc phn t trn danh sch lin kt.

Ti liu hng dn thc hnh mn Cu trc d liu v gii thut HCMUS 2010

Trang

NI DUNG THC HNH C bn


Sinh vin c k pht biu bi tp v thc hin theo hng dn: T chc mt danh sch lin kt n trong mi phn t cha thng tin d liu nguyn. Ngi dng s nhp cc gi tr nguyn t bn phm. Vi mi gi tr nguyn c nhp vo, gi tr c thm vo pha u ca danh sch lin kt. Nu ngi dng nhp vo gi tr -1, qu trnh nhp d liu s kt thc. Sau , in ra cc phn t ang c trn danh sch lin kt. Khi chng trnh kt thc, tt c cc phn t trn danh sch lin kt b xa b khi b nh. Phn tch Danh sch lin kt n gm mi phn t cha d liu nguyn. Thng tin ca mi phn t c khai bo theo ngn ng C/C++ nh sau:

struct NODE{ int Key; NODE *pNext; };

Thao tc cn thc hin: thm phn t nguyn vo u danh sch lin kt (AddHead), in cc phn t ca danh sch lin kt (PrintList), loi b tt c cc phn t trn danh sch lin kt (RemoveAll).

Chng trnh mu
#include "stdafx.h" struct NODE{ int Key; NODE *pNext; }; NODE* CreateNode(int Data) { NODE* pNode; pNode = new NODE; //Xin cp pht b nh ng to mt phn t (node) mi if (pNode == NULL) return NULL; pNode->Key = Data; pNode->pNext = NULL; return pNode; } bool AddHead(NODE* &pHead, int Data) { NODE *pNode; pNode = CreateNode(Data); if (pNode == NULL) return false; if (pHead == NULL) pHead = pNode; else { pNode->pNext = pHead; pHead = pNode; } return true; } Ti liu hng dn thc hnh mn Cu trc d liu v gii thut HCMUS 2010

Trang

void PrintList(NODE *pHead) { NODE *pNode; pNode = pHead; while (pNode != NULL) { printf("%5d", pNode->Key); pNode = pNode->pNext; //Ghi chu: thao tc ny dng lm g? } } void RemoveAll(NODE* &pHead) //Ghi chu: ngha ca k hiu & { NODE *pNode; while (pHead != NULL) { pNode = pHead; pHead = pHead->pNext; delete pNode; } pHead = NULL; //Ghi chu: Ti sao phi thc hin php gn ny? } int _tmain(int argc, _TCHAR* argv[]) { NODE *pRoot; //Ghi chu: Ti sao li phi thc hin php gn pha di? pRoot = NULL; int Data; do { printf("Nhap vao du lieu, -1 de ket thuc: "); scanf("%d", &Data); if (Data == -1) break; AddHead(pRoot, Data); }while (Data != -1); printf("\nDu lieu da duoc nhap: \n"); //Ghi chu: Chc nng ca dng lnh pha di PrintList(pRoot); //Ghi chu : Chc nng ca dng lnh pha di RemoveAll(pRoot); return 0; }

Yu cu 1. Bin dch on chng trnh nu trn. 2. Cho bit kt qu in ra mn hnh khi ngi dng nhp vo cc d liu sau: -1 5 7 1 -1 2 3 4 -1 10 -23 -25 -4 1 -1

3. Nu nhn xt ngn gn mi lin h gia th t nhp d liu vo vi th t in d liu ra mn hnh.


Ti liu hng dn thc hnh mn Cu trc d liu v gii thut HCMUS 2010

Trang

4. V hnh danh sch lin kt theo d liu c nhp cu 2. 5. Nu trong hm main (_tmain) th t hai dng lnh sau y b hon i cho nhau th kt qu kt xut ra mn hnh s nh th no i vi d liu cu 2? Gii thch l do?
//Ghi chu PrintList(pRoot); //Ghi chu RemoveAll(pRoot);

6. Nu trong hm main (_tmain) vng lp dowhile c thay i nh di y th kt qu kt xut ra mn hnh s nh th no i vi d liu cu 2? Gii thch l do?
do { printf("Nhap vao du lieu, -1 de ket thuc: "); scanf("%d", &Data); AddHead(pRoot, Data); if (Data == -1) break; }while (Data != -1);

7. Vi cc hm CreateNode, AddHead c cung cp sn, hy cho bit ngha ca cc gi tr tr v ca hm. 8. Hy ghi ch cc thng tin bng cch tr li cc cu hi ng vi cc dng lnh c yu cu ghi ch (//Ghi ch) trong cc hm RemoveAll, PrintList, _tmain. 9. Kt qu s nh th no nu hm RemoveAll c thay i nh di y? Gii thch l do
void RemoveAll(NODE* &pHead) { while (pHead != NULL) { pHead = pHead->pNext; delete pHead; } pHead = NULL; //Ghi chu: Ti sao phi thc hin php gn ny? }

10. Gi tr cui cng ca bin pRoot trong on chng trnh mu l g? Gii thch l do.

p dng Nng cao


1. B sung chng trnh mu cho php tnh tng gi tr cc phn t trn danh sch lin kt n gm cc gi tr nguyn. Gi : tham kho hm PrintList vit hm SumList. 2. B sung chng trnh mu cho php tm gi tr nguyn ln nht trong s cc phn t nguyn trn danh sch lin kt n gm cc gi tr nguyn. Gi : tham kho hm PrintList vit hm MaxList. 3. B sung chng trnh mu cho php tnh s lng cc phn t ca danh sch lin kt n gm cc gi tr nguyn. Gi : tham kho hm PrintList vit hm CountList. 4. B sung chng trnh mu cho php thm vo cui danh sch lin kt n mt gi tr nguyn. 5. B sung chng trnh mu cho php xa phn t u danh sch lin kt n. 6. B sung chng trnh mu cho php xa phn t cui danh sch lin kt n.
Ti liu hng dn thc hnh mn Cu trc d liu v gii thut HCMUS 2010

Trang

Gi : tham kho hm AddHead vit hm AddTail.

7. B sung chng trnh mu cho bit s lng cc phn t trn danh sch lin kt n c gi tr trng vi gi tr x c cho trc. Gi : tham kho thao tc duyt danh sch lin kt trong hm PrintList. 8. B sung chng trnh mu cho php to mt danh sch lin kt n gm cc phn t mang gi tr nguyn trong khng c cp phn t no mang gi tr ging nhau. Gi : s dng hm AddHead hoc AddTail c b sung thao tc kim tra phn t ging nhau. 9. Cho sn mt danh sch lin kt n gm cc phn t mang gi tr nguyn v mt gi tr nguyn x. Hy tch danh sch lin kt cho thnh 2 danh sch lin kt: mt danh sch gm cc phn t c gi tr nh hn gi tr x v mt danh sch gm cc phn t c gi tr ln hn gi tr x. Gii quyt trong 2 trng hp: a. Danh sch lin kt ban u khng cn tn ti. b. Danh sch lin kt ban u bt buc phi tn ti.

BI TP THM
1. xut cu trc d liu thch hp biu din a thc (anxn + an-1xn-1+..+ a1x + a0) bng danh sch lin kt (n hoc kp). Ci t cc thao tc trn danh sch lin kt n biu din a thc: a. b. c. d. In a thc Rt gn a thc Cng hai a thc Nhn hai a thc Tn sch (chui) Tc gi (chui, ti a 5 tc gi) Nh xut bn (chui)

2. Thng tin ca mt quyn sch trong th vic gm cc thng tin:

Nm xut bn (s nguyn) a. Hy to danh sch lin kt (n hoc kp) cha thng tin cc quyn sch c trong th vin (c nhp t bn phm). b. Cho bit s lng cc quyn sch ca mt tc gi bt k (nhp t bn phm). c. Trong nm YYYY (nhp t bn phm), nh xut bn ABC (nhp t bn phm) pht hnh nhng quyn sch no.

Ti liu hng dn thc hnh mn Cu trc d liu v gii thut HCMUS 2010

Trang