TR
:
BÀI
:
: 17207
TRÌNH :
DÙNG CHO SV NGÀNH :
- 2008
i
1
1
1
2
3
6
2. 1. - Stack 6
2.1.1 6
2.1.2 6
2.1.3 7
2.1.4 7
2.1.5 10
2.2. - Queue 12
2.2.1 12
2.2.2 13
2.2.3 13
2.2.4 16
2.2.5
16
2.3.
Linked list 17
2.3.1
17
2.3.2
. 17
2.3.3 18
2.3.4 25
2.3.5
26
2.3.6. 26
26
28
3.1.
28
3.1.1. (Graph) 28
3.1.2. Cây (tree) 29
3.3.
(Binary Search Tree - BST) 31
3.3.1.
31
3.3.2.
32
3.3.3. 32
3.3.4. 33
3.3.5. 34
3.3.6.
35
3.3.7. 36
AVL 39
54
54
54
55
55
4.1.4.Các phép 57
57
57
58
ii
61
4.3.1. 61
- ) : 61
61
4.3.4. 71
: 72
72
75
iii
2
CNTT
: 3
60
30
30
0
0
0
-
-
-
-
-
-
TS
LT
TH/Xemina
BT
KT
Ch
2
2
0
liên quan.
12
6
6
2.1. Danh sách
2.1.2. Các phép toán trên danh sách
2.3. Hàng
18
9
8
1
iv
TS
LT
TH/Xemina
BT
KT
3.3.2. L
3.5.2. Cây AVL
(hash table)
14
7
6
1
4
4
4
4
4
Cấu trúc dữ liệu và thuật toán,
2004.
Cấu trúc dữ liệu và giải thuật, Nhà
3. Robert Sedgewick, Cẩm nang thuật toán
-
-
: / /20
1
CH
ình
toán.
au.
các
trong
2
-
-
mô hình thích hợp thức hoá một giải thuật, một
cấu trúc dữ liệu
kiểu dữ liệu
Newclr
void GREEDY ( GRAPH *G, SET *Newclr )
{
Newclr = ; /*1*/
{
thêm v vào Newclr; /*5*/
}
}
i báo thích
if
void GREEDY ( GRAPH *G, SET *Newclr )
{
Newclr= ; /*1*/
{
int found=0; /*3.1*/
found=1; /*3.4*/
if (found==0)/*3.5*/
{
thêm v vào Newclr; /*5*/
}
3
}
}
void GREEDY ( GRAPH *G, LIST *Newclr )
{
int found;
int v,w ;
Newclr= ;
trong G;
while (v<>null)
{
found=0;
while( w<>null) && (found=0)
{
found=1;
}
if (found==0 )
{
Thêm v vào Newclr;
}
}
}
ng hóa
ng hóa chng trình
ph
bao gói.
là:
void main()
{
Nhap(Lop);
4
Xu_ly (Lop);
Xuat (Lop);
}
cà
Một kiểu dữ liệu trừu tng là một mô hình toán học cùng với một tập hợp các phép
toán (operator) trừu tng c định nghĩa trên mô hình đó. nguyên cùng
ADT là sự tổng quát hoá của các kiểu dữ liệu nguyên thỷ.
án trên danh sách newclr là:
-
-
-
- h.
MAKENULL(newclr)
newclr=;
w=FIRST(newclr)
newclr
w=NEXT(w,newclr)
INSERT( v,newclr)
Thêm v vào newclr
-
5
-
1.
2.
1.
2.
6
2. 1.
- Stack
2.1.1
: (stack) (items)
(
)
. (Last In First Out).
:
,
.
:
(
) c.
2.1.2
2
,
,
.
(
),
:
1.
(d)
.
2.
() .
3.
() .
4.
()
:
(empty)
(full)
.
() ().
(
)
1
0
.
7
2.1.3
3 (3
)
-100 100.
.
()
push(55)
(55)
1
push(-7)
(-7, 55)
1
push(16)
(16, -7, 55)
1
pop
(-7, 55)
16
push(-8)
(-8, -7, 55)
1
push(23)
(-8, -7, 55)
0
pop
(-7, 55)
-8
pop
(55)
-7
pop
()
55
pop
()
101
2.1.4
top_
.
_idx = -1 _idx = n-1
.
_idx lên 1
.
:
[0 4]
_idx
.
.
top_idx
E[0]
E[1]
E[2]
E[3]
E[4]
-1
?
?
?
?
?
push(55)
0
55
?
?
?
?
1
push(-7)
1
55
-7
?
?
?
1
push(16)
2
55
-7
16
?
?
1
pop
1
55
-7
16
?
?
16
push(-8)
2
55
-7
-8
?
?
1
pop
1
55
-7
-8
?
?
-8
pop
0
55
-7
-8
?
?
-7
.
[i]
>top_idx.
(
?).
8
(
10 2):
#include <stdio.h>
#include <stdlib.h>
const int MAX_ELEMENT = 100; // so phan tu toi da cua stack la 100
// khai bao stack chua cac so nguyen
typedef struct
{
int * data; // khai bao mang dong
int top_idx;
} stack;
// ham khoi tao stack rong
void init(stack *s);
void push(stack * s, int d);
void pop(stack *s);
int top(const stack *s);
int size(const stack *s);
int empty(const stack *s);
int full(const stack *s);
// ham giai phong bo nho danh cho stack
void clear(stack *s);
int main()
{
int n;
int bit;
stack s;
init(&s);
printf("Nhap so nguyen n = ");
scanf("%d", &n);
while(n)
{
push(&s, n%2);
n /= 2;
}
while(!empty(&s))
{
bit = top(&s);
pop(&s);
printf("%d", bit);
}
clear(&s);
return 0;
9
}
void init(stack *s)
{
s->data = (int*)malloc(MAX_ELEMENT * sizeof(int));
s->top_idx = -1;
}
void clear(stack *s)
{
if(s->data != NULL)
free(s->data);
s->top_idx = -1;
}
void push(stack *s, int d)
{
s->data[++s->top_idx] = d;
}
void pop(stack *s)
{
s->top_idx ;
}
int top(const stack *s)
{
return s->data[s->top_idx];
}
int size(const stack *s)
{
return s->top_idx+1;
}
int empty(const stack * s)
{
return (s->top_idx==-1)?(1):(0);
}
int full(const stack * s)
{
return (s->top_idx==MAX_ELEMENT-1)?(1):(0);
}
,
(
).
?
2 byte (1
).
10
.
.
.
c
.
2.1.5
1
(
{}, (), []).
{()} {()({})} {((} {(})
.
(
), } hay ]) y
(
),
.
:
while not end of Input
S = next sumbol
if(s is opening symbol)
push(s)
else //
if(stack.empty)
else
R = stack.top()
stack.pop()
if(!match(s,r))
If(!stack.empty())
:
1. Input: {()}
s = {, push{,
s = (, push (,
s = ), r = pop = (, r,s match
s = }, r = pop = {, r,s match
End of Input,
=>
.
: Input = { ( ) ( { ) } } (sai)
Input = { ( { } ) { } ( ) }
11
2
.
(
,
) (prefix notation, hay
Polish notation) (postfix notation,
).
(operand)
(operator). .
, y,
.
,
+, -, *,
/, ^ ().
ion1 2
1
2
+ y*
*x y z+
.
.
3 .
(infix notation):
,
, . 2 + 3 * (7
3)
(prefix notation):
1920.
,
. + * 2 3 7
(postfix notation):
,
. 2 3 7 *.
:
.
.
.
.
:
()
+ (
), - (
), !
+ (
), - (
)
<, <=, >, >=
==, !=
12
&&
||
2 + 4
* 5 22 4
5,
2
.
2*7/3
,
.
.
,
,
.
4 (
nguyên).
.
:
.
,
,
,
.
(5),
.
.
3
:
5 * (( (9+8) * (4 * 6) ) + 7)
(postfix):
5 8 9 + 4 6 * * 7 + *
.
.
2.2. - Queue
2.2.1
(
)
. (First In First Out).
,
,
13
.
2.2.2
,
,
:
1. push(d):
i.
2. pop(): .
3. front(): .
4. back(): .
5. size(): .
6. empty():
.
7. full():
().
:
( )
push(7)
( 7 )
push(8)
( 7, 8 )
push(5)
( 7, 8, 5 )
pop()
( 8, 5 )
7
pop()
( 5 )
8
2.2.3
(
):
.
, .
push(d)
: 1
pop()
:
O(n).
?
,
(pop)
1:
,
() (
)
.
. Khi push()
1
0.
?
14
,
.
#include <stdio.h>
#include <stdlib.h>
const int MAX_ELEMENT = 100; // so phan tu toi da cua queue la 100
// khai bao queue chua cac so nguyen
typedef struct
{
int * data; // khai bao mang dong
int head;
int tail;
int cap; // luu so phan tu cua hang doi
} queue;
// ham khoi tao queue rong
void init(queue *q);
void push(queue * s, int d);
void pop(queue *q);
int front(const queue *q);
int back(const queue *q);
int size(const queue *q);
int empty(const queue *q);
int full(const queue *q);
// ham giai phong bo nho danh cho queue
void clear(queue *q);
int main()
{
int a[] = {3, 5, 1, 8};
int n = 4;
int i;
int d;
queue q;
init(&q);
for(i=0;i<n;i++)
push(&q, a[i]);
while(!empty(&q))
{
d = front(&q);
printf("%d ", d);
pop(&q);
}
clear(&q);
return 0;
15
}
void init(queue *q)
{
q->data = (int*)malloc(MAX_ELEMENT * sizeof(int));
q->head = q->tail = -1;
q->cap = 0;
}
void clear(queue *q)
{
if(q->data != NULL)
free(q->data);
q->head = q->tail = -1;
q->cap = 0;
}
void push(queue *q, int d)
{
q->tail = (q->tail + 1) % MAX_ELEMENT;
q->data[q->tail] = d;
if(q->cap==0)
// neu hang doi rong thi sau khi push
// ca head va tail deu chi vao 1 phan tu
q->head = q->tail;
q->cap++;
}
void pop(queue *q)
{
q->head = (q->head + 1)%MAX_ELEMENT;
q->cap ;
if(q->cap==0)
q->head = q->tail = -1;
}
int front(const queue *q)
{
return q->data[q->head];
}
int back(const queue *q)
{
return q->data[q->tail];
}
int size(const queue *q)
{
return q->cap;
}
16
int empty(const queue *q)
{
return (q->cap==0)?(1):(0);
}
int full(const queue *q)
{
return (q->cap==MAX_ELEMENT-1)?(1):(0);
}
2.2.4
T
[0 3], ,
,
,
?
.
head
tail
E[0]
E[1]
E[2]
E[3]
R
-1
-1
?
?
?
?
push(55)
0
0
55
?
?
?
push(-7)
0
1
55
-7
?
?
push(16)
0
2
55
-7
16
?
pop()
1
2
55
-7
16
?
55
push(-8)
1
3
55
-7
16
-8
pop()
2
3
55
-7
16
-8
-7
pop()
3
3
55
-7
16
-8
16
push(11)
3
4
11
-7
16
-8
2.2.5
:
.
,
,
:
:
, (a, b)
(), duy(Breadth First
Search).
: (
)
(
()) (prefix).
x
.
17
2.3.
Linked list
2.3.1
(linked list)
(node),
(field)
(data)
(link) (point to) (
).
(i+1)
.
.
(point to)
.
(
)
(single linked list), m
, ,
.
2.3.2 t.
,
:
1. push_front(d):
.
2. push_back(d):
.
3. pop_front(): .
4. pop_back(): .
5. erase(): .
6. insert(): .
7. size():
.
8. empty():
.
9. begin(): .
10. end(): .
11. sort():
().
12. merge():
.
13. clear(): .
14. find(): .
18
:
2.3.3
typedef struct Node
{
// truong du lieu
int data;
struct Node * next;
} NodeType;
:
NodeType * head, * tail;
head = new node;
headnext = NULL;
(empty
)
,
0.
0,
.
(point to) .
.
(node)
(
) .
.
:
struct node * A;
A = new node;
Akey = X;
Anext = Inext;
Inext = A;
19
2
(
) .
.
(node)
,
.
:
inext = xnext;
(
,
).
(move)
:
.
:
3 tham
(
).
:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
// khai bao cau truc cho mot nut cua danh sach
typedef struct Node
{
// truong du lieu
int data;
struct Node * next;
20
} NodeType;
// khai bao kieu danh sach
typedef struct
{
NodeType * head;
NodeType * tail;
// so phan tu cua danh sach
int spt;
}LList;
// ham khoi tao danh sach
void init(LList * list);
// ham them 1 phan tu vao dau danh sach
void push_front(LList *list, int d);
// ham them mot phan tu vao cuoi danh sach
void push_back(LList *list, int d);
// ham xoa phan tu o cuoi danh sach
int pop_back(LList * list);
// ham xoa phan tu o dau danh sach
int pop_front(LList * list);
// ham tra ve phan tu dau tien
int begin(const LList * list);
// ham tra ve phan tu cuoi cung
int end(const LList * list);
void insertAfter(LList * list, NodeType * p);
void insertBefore(LList * list, NodeType * p);
void eraseAfter(LList * list, NodeType * p);
void eraseBefore(LList * list, NodeType * p);
// ham in danh sach
void printList(LList list);
// ham sap xep danh sach
void sort(LList *list);
// ham tim kiem trong danh sach
NodeType * find(LList *, int d);
// giai phong toan bo danh sach
void clear(LList * list);
// ham tron hai danh sach, ket qua luu trong danh sach thu nhat
void merge(LList *list1, const LList *list2);
// ham kiem tra danh sach lien ket co rong khong
int empty(const LList *list);
int main()
{
LList myList;
LList list2;