Professional Documents
Culture Documents
LÝ THUYẾT CTDL
LÝ THUYẾT CTDL
LÝ THUYẾT CTDL
- Hàm ListDelete
void ListDelete(LIST &L,int k) {
LIST x, y;
if(L!= NULL) {
y= NULL; x =L;
while(x!=NULL && x->key!=k) {
y=x; x=x->next;
}
if(x!=NULL) {
if(y==NULL) L=x->next; //nút cần xóa ở đầu DS
else y->next=x->next; // Loại bỏ nút x khỏi DS
delete(x); // Xóa khỏi bộ nhớ
}
}
}
- Duyệt DS: ListWalk
void ListWalk(LIST L) {
if(L!=NULL) {
cout<key)<<' ';
ListWalk(L->next);
}
}
2. DANH SÁCH LIÊN KẾT KÉP
- Định nghĩa DSLK kép
typedef struct CELL *LIST;
struct CELL {
int key;
LIST prev, next;
};
LIST L;
- Hàm khởi tạo DSLK đôi
void ListInitialize(LIST &L) {
L=NULL;
}
- Hàm ListSearch
LIST ListSearch(LIST L, int k) {
LIST x;
x=L;
while(x!=NULL && x->key!=k) x=x->next;
return x;
}
- Hàm ListInsert
void ListInsert(LIST &L, int k) {
LIST x;
x=new(CELL); x->key=k;
x->next=NULL; x->prev=NULL x->next=L;
if(L!=NULL) L->prev=x;
L = x;
x->prev=NULL;
}
- Hàm ListDelete
void ListDelete(LIST &L, int k) {
LIST x=L;
while(x!=NULL && x->key!=k) x=x->next;
if(x!=NULL) {
if(x->prev!=NULL) x->prev->next=x->next;
else L = x->next;
if(x->next!=NULL) x->next->prev=x->prev;
delete x;
}
}
3. STACK ( Last in, First out LIFO ) , ĐPT là O(1)
Biểu diễn Stack bằng Mảng
- Định nghĩa Stack
#define MAX 200
struct STACK{
int a[MAX];
int top;
int N; //kich thuoc stack
};
STACK S;
- Hàm StackInitialize
void StackInitialize(STACK &S) {
S.top=0;
S.N=30;
}
- Hàm StackEmpty
bool StackEmpty(STACK S) {
if(S.top==0) return true;
else return false;
}
- Hàm StackFull
bool StackFull(STACK S) {
if(S.top==S.N) return true;
else return false;
}
- Push
void Push(STACK &S, int x) {
if(StackFull(S)) cout<<"overflows";
else{
S.top=S.top+1;
S.a[S.top]=x;
}
}
- Pop
int pop(STACK &S) {
if(StackEmpty(S)) { cout<<"underflows"; return -1;}
else {
S.top=S.top-1;
return S.a[S.top+1];
}
}
Biểu diễn Stack bằng DSLK
- Định nghĩa Stack
typedef struct CELL *STACK;
struct CELL {
int Obj;
STACK next;
};
STACK S;
- Hàm StackInitialize
void StackInitialize(STACK & S) {
S=NULL;
}
- Hàm StackEmpty
bool StackEmpty(STACK S) {
if(S==NULL) return true;
else return false;
}
- Hàm StackFull
bool StackFull(STACK S) {
return false;
}
- Push
void Push(STACK &S, int x) {
STACK p;
p=new(CELL);
p->Obj=x;
p->next=S;
S=p;
}
- Pop
int Pop(STACK &S) {
int x; STACK p;
if(StackEmpty(S)) {cout<<"underflows"; return -1;}
else{
p=S;
S=p->next;
x=p->Obj;
delete(p);
return x;
}
}
4. QUEUE ( FIRST IN, FIRST OUT FIFO ) , ĐPT là O(1)
BIỂU DIỄN QUEUE BẰNG MẢNG
- Định nghĩa Queue
typedef struct QUEUE{
int a[MAX];
int head, tail;
int n;
};
QUEUE Q;
- Hàm QueueInitialize
void QueueInitialize(QUEUE &Q) {
Q.head=Q.tail=1;
Q.n=20; //tùy chọn
}
- Hàm Enqueue
void Enqueue(QUEUE &Q, int x) {
if(queue_full(Q)) cout<<"overflow";
else{
Q.a[Q.tail]=x;
if(Q.tail==Q.n) Q.tail=1;
else Q.tail=Q.tail+1;
}
}
- Hàm Dequeue
int dequeue(QUEUE &Q) {
int x;
if(queue_empty(Q)){ cout<<"underflow"; return -1;}
else {
x=Q.a[Q.head];
if(Q.head==Q.n) Q.head=1;
else Q.head=Q.head+1;
return x;
}
}
- Hàm QueueEmpty
bool QueueEmpty(QUEUE Q) {
if(Q.head==Q.tail) return true;
else return fasle;
}
- Hàm QueueFull
bool QueueFull(QUEUE Q) {
if((Q.tail==Q.n&&Q.head==1)||(Q.head==Q.tail+1))
return true;
else return fasle;
}
BIỂU DIỄN QUEUE BẰNG DSLK
- Định nghĩa Queue
typedef struct CELL *LIST;
struct CELL{
int Obj;
LIST next;
};
struct QUEUE{
LIST head, tail;
};
QUEUE Q;
- Hàm QueueInitialize
void QueueInitialize(QUEUE &Q) {
Q.head=Q.tail=NULL;
}
- Hàm Enqueue
void Enqueue(QUEUE &Q,int x) {
LIST p;
p=new(CELL);
p->Obj=x;
p->next=NULL;
if(Q.head==NULL) Q.head=p;
else Q.tail ->next=p;
Q.tail=p;
}
- Hàm Dequeue
int Dequeue(QUEUE &Q) {
int x;
LIST p;
if(QueueEmpty(Q)){cout<<"underflows"; return -1;}
else {
p=Q.head;
x=p->Obj;
if(Q.head==Q.tail) {Q.head=Q.tail=NULL;}
else Q.head=p->next;
delete p;
return x;
}
}
- Hàm QueueEmpty
bool QueueEmpty(QUEUE Q) {
if(Q.head==NULL) return true;
else return false;
}
5. TREE ( CÂY )
CÂY NHỊ PHÂN
- Định nghĩa cây nhị phân
typedef struct CELL *TREE;
struct CELL {
int key; //trong thực tế có thêm nhiều dữ liệu khác
TREE left, right;
};
TREE T;
- Duyệt theo trung thứ tự ( inorder tree walk )
void InorderTreeWalk(TREE x) {
if(x!=NULL) {
InorderTreeWalk(x->left);
cout<key)<<' ';
InorderTreeWalk(x->right);
}
}
CÂY NHỊ PHÂN TÌM KIẾM, ĐPT là O(h) với h là chiều cao cây
Nếu y là một nút trong cây con trái của nút x thì key[y]< key[x]
Nếu y là một nút trong cây con phải của nút x thì key[y]> key[x]
SELECTIONSORT
void SelectionSort(int A[], int n) {
for(int i=0;i<n-1;i++){
int min =i;
for(int j=i+1;j<n;j++){
if(A[j]<A[min]) min=j; // Đổi thành A[j]>A[min] nếu giảm dần
swap(A[i],A[min]);
}
}
INSERTIONSORT
void InsertionSort(int A[], int n) {
for(int j=1;j<n;j++){
int key = A[j];
int i = j -1;
while(i>=0 && A[i]>key) { // chuyển thành A[i] < key nếu giảm
dần
A[i+1]=A[i];
i=i-1;
}
A[i+1]=key;
}
}
BUMBLESORT
void BubbleSort(int A[], unsigned n) {
for(int i=n-1;i>=1;i--) {
for(int j=0; j<=i-1; j++)
if(A[j]>A[j+1]) // Đổi thành A[j]<A[j+1] nếu giảm dần
Swap(A[j], A[j+1]);
}
}
HEAPSORT
void heapify(int a[],int n,int i){
int left = 2*i + 1;
int right = 2*i + 2;
int largest = i;
if(left < n && a[left] > a[largest]){
largest = left;
}
if(right < n && a[right] > a[largest]){
largest = right;
}
if(largest != i){
swap(a[i],a[largest]);
heapify(a,n,largest);
}
}
//Sap xep tang dan
void HeapSort(int a[],int n){
//Xay dung MaxHeap
for(int i=n/2 -1;i>=0;i--){
heapify(a,n,i);
}
//
for(int i=n-1;i>=0;i--){
swap(a[i],a[0]);
heapify(a,i,0);
}
}
// Sap xep giam dan thi sua phan heapify. Cu the la so sanh 2 thang con la left va right nho hon
thang cha ( largest or smallest )
MERGESORT
void merge(int a[], int l, int m,int r){
vector<int> x(a+l,a+m+1);
vector<int> y(a+m+1,a+r+1);
int i=0, j=0;
while(i < x.size() && j < y.size()){
if(x[i]<=y[j]){
a[l]=x[i];
++l;
++i;
}
else{
a[l]=y[j];
++l;
++j;
}
}
while(i < x.size()){
a[l]=x[i];
++l;
++i;
}
while(j < y.size()){
a[l]=y[j];
++l;
++j;
}
}
// phuong thuc tach mang
void MergeSort(int a[], int l, int r){
if(l>=r) return;
int m=(l+r)/2;
MergeSort(a,l,m);
MergeSort(a,m+1,r);
merge(a,l,m,r);
}
QUICKSORT
int Partition(int A[], int p, int r) {
int i, j, x;
x=A[r];
i=p-1;
for(j=p; j<r ; j++)
if(A[j] <=x){
i=i+1;
exchange(A[i], A[j]); //hoán vị A[i] và A[j]
}
exchange(A[i+1], A[r]);
return i+1;
}