際際滷

際際滷Share a Scribd company logo
C畉utr炭cd畛li畛u1v叩thu畉tgi畉iC畉UTRCD畛LI畛UVGI畉ITHU畉T1 Click To Edit Master Title Style
1
N畛I DUNG
DANH SCH LIN K畉T k辿p
C畉utr炭cd畛li畛u1v叩thu畉tgi畉iC畉UTRCD畛LI畛UVGI畉ITHU畉T1 Click To Edit Master Title Style
2
畛nh Ngh挑a
 M畛i ph畉n t畛 li棚n k畉t v畛i ph畉n t畛 畛ng tr動畛c
v sau n坦 trong danh s叩ch
 H狸nh v畉 minh h畛a danh s叩ch li棚n k畉t k辿p:
A B C D
C畉utr炭cd畛li畛u1v叩thu畉tgi畉iC畉UTRCD畛LI畛UVGI畉ITHU畉T1 Click To Edit Master Title Style
3
C畉u Tr炭c D畛 Li畛u
 C畉u tr炭c d畛 li畛u 1 n炭t
typedef struct tagDnode
{ Data Info;
struct tagDnode *pPre;
struct tagDnode *pNext;
}DNode;
 C畉u tr炭c List k辿p
Typedef struct tagDList
{ DNode *pHead;
DNode *pTail;
}DList;
C畉utr炭cd畛li畛u1v叩thu畉tgi畉iC畉UTRCD畛LI畛UVGI畉ITHU畉T1 Click To Edit Master Title Style
4
C叩c Thao T叩c Tr棚n List K辿p
 Kh畛i t畉o danh s叩ch li棚n k畉t k辿p r畛ng
 T畉o 1 n炭t c坦 thnh ph畉n d畛 li畛u = x
 Ch竪n 1 ph畉n t畛 vo danh s叩ch
 Ch竪n vo 畉u
 Ch竪n sau ph畉n t畛 Q
 Ch竪n vo tr動畛c ph畉n t畛 Q
 Ch竪n vo cu畛i danh s叩ch
 Hu畛 1 ph畉n t畛 trong danh s叩ch
 H畛y ph畉n t畛 畉u danh s叩ch
 H畛y ph畉n t畛 cu畛i danh s叩ch
 H畛y 1 ph畉n t畛 c坦 kho叩 b畉ng x
 T狸m 1 ph畉n t畛 trong danh s叩ch
 S畉p x畉p danh s叩ch
C畉utr炭cd畛li畛u1v叩thu畉tgi畉iC畉UTRCD畛LI畛UVGI畉ITHU畉T1 Click To Edit Master Title Style
5
T畉o 1 Danh S叩ch R畛ng
void CreateDList(DList &l)
{
l.DHead=NULL;
l.DTail=NULL;
}
C畉utr炭cd畛li畛u1v叩thu畉tgi畉iC畉UTRCD畛LI畛UVGI畉ITHU畉T1 Click To Edit Master Title Style
6
T畉o 1 N炭t C坦 Thnh Ph畉n D畛 Li畛u = X
DNode *CreateDNode(int x)
{ DNode *tam;
tam=new DNode;
if(tam==NULL)
{ printf("khong con du bo nho");
exit(1);
}
else
{ tam->Info=x;
tam->pNext=NULL;
tam->pPre=NULL;
return tam;
}
}
C畉utr炭cd畛li畛u1v叩thu畉tgi畉iC畉UTRCD畛LI畛UVGI畉ITHU畉T1 Click To Edit Master Title Style
7
Th棚m 1 N炭t Vo 畉u Danh S叩ch
A B C D
X pTail
pHead
 Minh h畛a h狸nh v畉
C畉utr炭cd畛li畛u1v叩thu畉tgi畉iC畉UTRCD畛LI畛UVGI畉ITHU畉T1 Click To Edit Master Title Style
8
Ci 畉t Th棚m 1 N炭t Vo 畉u Danh S叩ch
void AddFirst(DList &l, DNode *tam)
{
if(l.pHead==NULL)//xau rong
{
l.pHead=tam;
l.pTail=l.pHead;
}
else
{
tam->pNext=l.pHead;
l.pHead->pPre=tam;
l.pHead=tam;
}
}
C畉utr炭cd畛li畛u1v叩thu畉tgi畉iC畉UTRCD畛LI畛UVGI畉ITHU畉T1 Click To Edit Master Title Style
9
Th棚m Vo Cu畛i Danh S叩ch
 Minh h畛a th棚m 1 ph畉n t畛 vo sau danh s叩ch
A B C D
pHead
pTail
X
C畉utr炭cd畛li畛u1v叩thu畉tgi畉iC畉UTRCD畛LI畛UVGI畉ITHU畉T1 Click To Edit Master Title Style
10
Ci 畉t Th棚m 1 N炭t Vo Cu畛i Danh S叩ch
void AddEnd(DList &l,DNode *tam)
{
if(l.pHead==NULL)
{
l.pHead=tam;
l.pTail=l.pHead;
}
else
{
tam->pPre=l.pTail;
l.pTail->pNext=tam;
tam=l.pTail;
}
}
C畉utr炭cd畛li畛u1v叩thu畉tgi畉iC畉UTRCD畛LI畛UVGI畉ITHU畉T1 Click To Edit Master Title Style
11
Th棚m Vo Sau N炭t Q
 Minh h畛a th棚m n炭t X vo sau n炭t q
A B C D
pHead pTail
X
q
C畉utr炭cd畛li畛u1v叩thu畉tgi畉iC畉UTRCD畛LI畛UVGI畉ITHU畉T1 Click To Edit Master Title Style
12
Ci 畉t Th棚m 1 N炭t Vo Sau N炭t Q
void AddLastQ(DList &l,DNode *tam, DNode *q)
{
DNode *p;
p=q->pNext;
if(q!=NULL)//them vao duoc
{
tam->pNext=p;
tam->pPre=q;
q->pNext=tam;
if(p!=NULL)
p->pPre=tam;
if(q==l.pTail) //them vao sau danh sach lien ket.
l.pTail=tam;
}
else
AddFirst(l,tam);
C畉utr炭cd畛li畛u1v叩thu畉tgi畉iC畉UTRCD畛LI畛UVGI畉ITHU畉T1 Click To Edit Master Title Style
13
Th棚m 1 N炭t Vo Tr動畛c N炭t Q
 Minh ho畉 th棚m 1 n炭t vo tr動畛c n炭t q
A B C D
pHead pTail
X
q
C畉utr炭cd畛li畛u1v叩thu畉tgi畉iC畉UTRCD畛LI畛UVGI畉ITHU畉T1 Click To Edit Master Title Style
14
Ci 畉t Th棚m 1 N炭t Vo Tr動畛c N炭t Q
void AddBeforeQ(DList &l,DNode *tam,DNode *q)
{ DNode *p;
p=q->pPre;
if(q!=NULL)
{
tam->pNext=q;
q->pPre=tam;
tam->pPre=p;
if(p!=NULL)
p->pNext=tam;
if(q==l.pHead)
l.pHead = tam;
}
else
AddEnd(l,tam);
}
C畉utr炭cd畛li畛u1v叩thu畉tgi畉iC畉UTRCD畛LI畛UVGI畉ITHU畉T1 Click To Edit Master Title Style
15
Xo叩 Ph畉n T畛 畉u Danh S叩ch
void DeleteFirst(DList &l)
{
DNode *p;
if(l.pHead!=NULL)
{
p=l.pHead;
l.pHead=l.pHead->pNext;
l.pHead->pPre=NULL;
delete p;
if(l.pHead==NULL)
l.pTail=NULL;
}
}
C畉utr炭cd畛li畛u1v叩thu畉tgi畉iC畉UTRCD畛LI畛UVGI畉ITHU畉T1 Click To Edit Master Title Style
16
Xo叩 1 Ph畉n T畛 Cu畛i Danh S叩ch
void DeleteEnd(DList &l )
{
DNode *p;
if(l.pHead!=NULL) //tuc xau co hon mot phan tu
{
p=l.pTail;
l.pTail=l.pTail->Pre;
l.pTail->pNext=NULL;
delete p;
if(l.pTail==NULL)
l.pHead=NULL;
}
}
C畉utr炭cd畛li畛u1v叩thu畉tgi畉iC畉UTRCD畛LI畛UVGI畉ITHU畉T1 Click To Edit Master Title Style
17
H畛y 1 N炭t Sau N炭t Q
void DeleteLastQ(DList &l,DNode *q)
{
DNode *p;//luu node dung sau node q
if(q!=NULL)
{
p=q->pNext;
if(p!=NULL)
{
q->pNext=p->pNext;
if(p==l.pTail)//xoa dung nu't cuoi
l.pTail=q;
else //Nut xoa khong phai nut cuoi
p->pNext->pPre=q;
delete p;
}
}
else
DeleteFirst(l);
}
C畉utr炭cd畛li畛u1v叩thu畉tgi畉iC畉UTRCD畛LI畛UVGI畉ITHU畉T1 Click To Edit Master Title Style
18
Hu畛 1 N炭t 畛ng Tr動畛c N炭t Q
void DeleteBeforeQ(DList &l,DNode *q)
{
DNode *p;
if(q!=NULL) //tuc ton tai node q
{
p=q->pPre;
if(p!=NULL)
{
q->pPre=p->pPre;
if(p==l.pHead)//p la Node dau cua danh sach
l.pHead=q;
else //p khong phai la node dau
p->pPre->pNext=q;
delete p;
}
}
else
DeleteEnd(l);
}
C畉utr炭cd畛li畛u1v叩thu畉tgi畉iC畉UTRCD畛LI畛UVGI畉ITHU畉T1 Click To Edit Master Title Style
19
Xo叩 1 Ph畉n T畛 C坦 Kho叩 = X
int DeleteX(DList &l,int x)
{
DNode *p;
DNode *q;
q=NULL;
p=l.pHead;
while(p!=NULL)
{
if(p->Info==x)
break;
q=p;//q la Node co truong Info = x
p=p->pNext;
}
if(q==NULL) return 0;//khong tim thay Node nao co truong Info =x
if(q!=NULL)
DeleteLastQ(l,q);
else
DeleteFirst(l);
return 1;
C畉utr炭cd畛li畛u1v叩thu畉tgi畉iC畉UTRCD畛LI畛UVGI畉ITHU畉T1 Click To Edit Master Title Style
20
S畉p X畉p
void DoiChoTrucTiep(DList &l)
{ DNode *p,*q;
p=l.pHead;
while(p!=l.pTail)
{
q=p->pNext;
while(q!=NULL)
{
if(p->Info>q->Info)
HV(p,q);
q=q->pNext;
}
p=p->pNext;
}}

More Related Content

ctdl&gt 05-list_kep