You are on page 1of 21

Selection Sort

-(Tăng dần)

Void SelectionSort(int a[],int n){

int min;

for(int i=0;i<n-1;i++){

min = i;

for(int j=i+1;j<n;j++)

if(a[j]<a[min])

min = j;

if(i != min) swap(a[min], a[i]);

Bubble Sort

-(Tăng dần)

Void BubbleSort(int a[],int n){

int i,j;

for(int i=0;i<n-1;i++)

for(int j=n-1;j>i;j--)

if(a[j]<a[j-1])

swap(a[i], a[j-1]);

Insertion Sort

-(Tăng dần)

Void InsertionSort(int a[],int n){

for(int i=1;i<n;i++){

int x = a[i]; int pos = i -1;

while ((pos >= 0 ) && (a[pos] > x)){

a[pos+1] = a[pos];

pos--;

a[pos+1] = x;

}
}

Quick Sort

-(Tăng dần)

Void QuickSort(int a[],int left,int right){

int x = a[left];

int i = left + 1, j = right;

while (i <= j){

while (a[i] < x ) i++;

while (a[j] > x ) i--;

if ( i < j ){

swap (a[i], a[j]);

i--;

i++;

swap(a[left], a[j]);

if(left < j – 1)

QuickSort(a, left, j – 1);

if(j + 1 < right)

QuickSort(a, j + 1, right);

HeapSort

-(Tăng dần)

void HeapSort ( int a[], int n){

int i, j, size=n-1, k;

while(size>0){

k = (int)(size-1)/2;

while(k>=0){

i=k;

while(i*2<size)

j=2*i+1;

if ((j+1<=size)&&(a[j]<a[j+1]))
j=j+1;

if(a[i]<a[j]{

swap(a[i],a[j]);

i=j;

else

break;

k--;

swap(a[0],a[size]);

size--;

Liên kết đơn:

-Thêm nội dung x vào đầu danh sách:

void insertFirst(Node* &pHead, int x) {

Node *newNode ;

new_node = creatNode(x);

if (pHead == NULL)

addHead(x);

else{

new_node->next = pHead; pHead = new_node;

-Thêm node có nội dung x vào cuối danh sách:

void insertLast(Node* &pHead, int x) {

Node *newNode ;

new_node = creatNode(x);

if (pHead == NULL)

addHead(pHead, x);

else{

Node *p;
p = pHead;

while (p -> next != NULL)

p = p -> next;

p->next = new_node;

-Thêm node có nội dung x sau node p

void InsertAfter(Node* &pHead, Node *p, int x) {

if (pHead == NULL)

cout<<”Danh sach trong!”;

else{

Node* new_node;

new_node = creatNode(x);

new_node->next = p->next;

p->next = new_node;

-xóa bỏ phân tử ở đầu danh sách

void deleteFirst(Node* &pHead) {

if (pHead == NULL)

cout<<”Danh sach trong!”;

else{

Node *p;

P = pHead;

pHead = pHead->next;

delete p;

p = NULL;

-xóa bỏ phần tử ở cuối danh sách

void DeleteLast(Node* &pHead) {

if (Head == NULL)

cout<<”Danh sach trong!”;


else{

Node *p = pHead;

while (p->next != NULL)

p=p->next;

Node *q = pHead;

While (q->next != p)

q = q->next;

q->next=NULL;

delete p;

p = NULL;

-xóa node p trong danh sách

void deleteNode(Node* &pHead, Node *p) {

if (pHead == NULL)

cout<<”Danh sach trong!”;

else{

Node *q= pHead;

While (q->next !=p)

q = q->next;

q->next = p->next;

delete p;

p = NULL;

- Xóa node sau node p trong danh sách


- void deleteAfter(Node* &pHead, Node *p) {
- Node* q;
- If(p->next == NULL)
- Cout<<”Khong the xoa nut sau p!”;
- else{
- q = q->next;
- p->next = q->next;
- delete q;
- q = NULL;
- }
- }
-Xóa all
- void deleteAll(Node* &pHead, Node *p) {
- Node *p;
- While(pHead != NULL){
- p = pHead;
- pHead = Head -> next;
- delete p;
- }
- p = NULL;
- }
- Tìm kiếm phần tử x trong ds
- void searchValue(Node* &pHead, int x) {
- Node* p;
- p=pHead;
- while(p != NULL && p->data != x)
- p = p->next;
- return p;
- }
- Sắp xếp danh sách theo thứ tự tăng dần
- void sortList(Node* &pHead) {
- Node* q,*min,*p = Head;
- while(p != NULL){
min = p; q=p->next;
- while (q!=NULL){
- if(q->info < min->info)
- min = q;
- q = q->next;
- }
- swap(p->info,min->info);
- p = p -> next;
- }
- }

Liên kết đơn vòng

//insertFirst: them node co noi dung x vao dau danh sach

void insertFirst(Node* &pList, int x){

Node* new_node;

new_node = creatNode(x);

if(pList == NULL)

addHead(pList,x);

else{

new_node->next = pList->next;

pList->next = new_node;

}
}

//insertLast: chen node co noi dung x vao cuoi danh sach

void insertLast(Node* &pList, int x){

Node* new_node;

new_node = creatNode(x);

if(pList == NULL)

addHead(pList,x);

else{

new_node->next = pList->next;

pList->next = new_node;

pList = new_node;

//insertAfter: chen vao sau node p

void insertAfter(Node* &pList, Node* &p, int x){

if(pList == NULL)

cout << "Danh Sach trong!";

else{

Node* new_node;

new_node = creatNode(x);

new_node->next = p->next;

p->next = new_node;

//insertBefore: chen node vao truoc node p

void insertBefore(Node* &pList, Node* p, int x){

Node* new_node;

new_node = creatNode(x);

if(pList == NULL)

cout << "Danh sach trong!";


else{

Node* q = pList->next;

while(q->next != p)

q = q->next;

new_node->next = p;

q->next = new_node;

//deleteFirst: xoa node dau

void deleteFirst(Node* &pList){

if(pList == NULL)

cout << "Danh sach trong!";

else if(pList == pList->next){

delete pList;

pList = NULL;

else{

Node* p = pList->next;

pList->next = p->next;

delete p;

p = NULL;

//deleteLast: xoa node cuoi

void deleteLast(Node* &pList){

if(pList == NULL)

cout << "Danh sach trong!";

else if(pList == pList->next){

delete pList;

pList = NULL;

}
else{

Node* p = pList;

while(pList->next != p)

pList = pList->next;

pList->next = p->next;

delete p;

p = NULL;

//deleteNode: xoa bo node p

void deleteNode(Node* &pList, Node* &p){

if(pList == NULL)

cout << "Danh sach trong!";

else if(p == pList->next){

deleteFirst(pList);

return;

else if(p == pList){

deleteLast(pList);

return;

else{

Node* q = pList->next;

while(q->next != p)

q = q->next;

q->next = p->next;

delete p;

p = NULL;

//deleteAfter: xoa sau node p


void deleteAfter(Node* &pList, Node* &p){

if(pList == NULL)

cout << "Danh sach trong!";

else if(pList == pList->next){

cout << "Khong co node sau p";

return;

else{

Node* q = p->next;

p->next = q->next;

delete q;

q = NULL;

//deleteAll: xoa toan bo danh sach

void deleteAll(Node* &pList){

Node* p;

while(pList->next != NULL){

p = pList->next;

pList->next = p->next;

delete p;

p = NULL;

//SearchValue: tim kiem node

Node* SearchValue(Node* &pList, int x){

if(pList == NULL)

return NULL;

Node* p = pList->next;

while(p != pList->next && p->data != x)

p = p->next;
return p;

Liên kết đôi vòng

//insertFirst: chen node co noi dung x vao dau danh sach

void insertFirst(Node* &pHead, int x){

if(pHead == NULL)

addHead(pHead,x);

else{

Node* new_node;

new_node = creatNode(x);

Node* pTail = pHead->prev;

new_node->next = pHead;

pHead->prev = new_node;

new_node->prev = pTail;

pTail->next = new_node;

pHead = new_node;

//insertLast: chen node co noi dung x vao cuoi danh sach

void insertLast(Node* &pHead, int x){

if(pHead == NULL)

addHead(pHead,x);

else{

Node* new_node;

new_node = creatNode(x);

Node* pTail = pHead->prev;

new_node->next = pHead;

pHead->prev = new_node;

new_node->prev = pTail;

pTail->next = new_node;

}
//insertAfter: chen node vao sau node p

void insertAfter(Node* &pHead, Node* p, int x){

if(pHead == NULL)

addHead(pHead,x);

else if(p->next == pHead)

insertLast(pHead,x);

else{

Node* new_node, *q;

new_node = creatNode(x);

q = p->next;

new_node->prev = p;

new_node->next = q;

p->next = new_node;

q->prev = new_node;

//insertBefore: chen vao truoc node p

void insertBefore(Node* &pHead, Node* p, int x){

if(pHead == NULL)

addHead(pHead,x);

else if(p->prev == pHead->prev)

insertFirst(pHead,x);

else{

Node* new_node, *q;

new_node = creatNode(x);

q = p->prev;

new_node->prev = q;

new_node->next = p;

q->next = new_node;

p->prev = new_node;

}
}

//countNode: dem so node co trong danh sach

int countNode(Node* pHead){

int dem = 0;

Node* p = pHead;

do{

dem++;

p = p->next;

}while(p != pHead);

return dem;

//insertPostK: chen node vao mot vi tri nhap tu ban phim

void insertPostK(Node* &pHead){

int x;

cout << "\nNhap gia tri can chen: ";

cin >> x;

int vitri;

int n = countNode(pHead);

do{

cout << "Nhap vi tri can chen: ";

cin >> vitri;

}while(vitri <= 0 || vitri > n);

//deleteFirst: xoa node dau trong danh sach

void deleteFirst(Node* &pHead){

if(pHead == NULL)

return;

else if(pHead->prev == pHead){

delete pHead;

pHead = NULL;
}

else{

Node* p = pHead;

Node* pTail = pHead->prev;

pHead = pHead->next;

pHead->prev = pTail;

pTail->next = pHead;

delete p;

p = NULL;

//deleteLast: xoa node cuoi danh sach

void deleteLast(Node* &pHead){

if(pHead == NULL)

return;

else if(pHead->prev == pHead){

delete pHead;

pHead = NULL;

else{

Node* p = pHead->prev;

Node* pTail = p->prev;

pHead->prev = pTail;

pTail->next = pHead;

delete p;

p = NULL;

//deleteNode: xoa node p nao do

void deleteNode(Node* &pHead, Node* p){

if(pHead == NULL){
cout << "Danh sach trong!";

return;

else if(p == pHead){

deleteFirst(pHead);

return;

else{

Node* left, *right;

left = p->prev;

right = p->next;

left->next = right;

right->prev = left;

delete p;

p = NULL;

//SearchValue: tim kiem node trong danh sach

Node* SearchValue(Node* pHead, int x){

if(pHead == NULL)

return NULL;

Node* p = pHead;

do{

if(p->data == x)

return p;

else

p = p->next;

}while(p != pHead);

return NULL;

//SortList: sap xep danh sach


void SortList(Node* &pHead){

Node* p, *q, *pTail;

pTail = pHead->prev;

for(p = pHead; p != pTail; p = p->next)

for(q = p->next; q != pHead; q = q->next)

if(p->data > q->data)

swap(p->data, q->data);

-Liên kết đôi

//insertFirst: chen node co noi dung x vao dau

void insertFirst(Node* &pHead, int x){

if(pHead == NULL)

addHead(pHead,x);

else{

Node* new_node;

new_node = creatNode(x);

new_node->next = pHead;

pHead->prev = new_node;

pHead = new_node;

//insertLast: chen node co noi dung x vao cuoi danh sach

void insertLast(Node* &pHead, int x){

if(pHead == NULL)

addHead(pHead,x);

else{

Node* new_node;

new_node = creatNode(x);

Node* p = pHead;

while(p->next != NULL)

p = p->next;

new_node->prev = p;
p->next = new_node;

//insertAfter: chen node vao sau node p

void insertAfter(Node* &pHead, Node* p, int x){

if(pHead == NULL)

addHead(pHead,x);

else if(p->next == NULL)

insertLast(pHead,x);

else{

Node* new_node, *q;

new_node = creatNode(x);

q = p->next;

new_node->prev = p;

new_node->next = q;

p->next = new_node;

q->prev = new_node;

//insertBefore: chen node vao truoc node p

void insertBefore(Node* &pHead, Node* p, int x){

if(pHead == NULL)

addHead(pHead,x);

else if(p->prev == NULL)

insertFirst(pHead,x);

else{

Node* new_node, *q;

new_node = creatNode(x);

q = p->prev;

new_node->prev = q;

new_node->next = p;
q->next = new_node;

p->prev = new_node;

//countNode: Dem so node trong danh sach

int countNode(Node* &pHead){

int dem = 0;

Node* p = pHead;

while(p != NULL){

dem++;

p = p->next;

return dem;

//insertNodechotruoc: them mot node vao vi tri cho truoc

void insertNodechotruoc(Node* &pHead){

int x;

cout << "\nNhap gia tri can chen: ";

cin >> x;

int vitri;

int n = countNode(pHead);

do{

cout << "Nhap vi tri can chen: ";

cin >> vitri;

}while(vitri <= 0 || vitri > n);

//deleteFirst: xoa node dau trong danh sach

void deleteFirst(Node* &pHead){

if(pHead == NULL)

return;
else if(pHead->prev = pHead->next){

delete pHead;

pHead = NULL;

else{

Node* p = pHead;

pHead = pHead->next;

pHead->prev = NULL;

delete p;

p = NULL;

//deleteLast: xoa node cuoi trong danh sach

void deleteLast(Node* &pHead){

if(pHead == NULL)

return;

else if(pHead->prev == pHead->next){

delete pHead;

pHead = NULL;

else{

Node* p = pHead;

while(p->next != NULL)

p = p->next;

Node* q = p->prev;

q->next = NULL;

delete p;

p = NULL;

//deleteNode: xoa node p bat ki


void deleteNode(Node* &pHead, Node* p){

if(pHead == NULL){

cout << "Danh sach trong!";

return;

else if(p->prev == NULL){ //p la node dau

deleteFirst(pHead);

return;

else if(p->next == NULL){ //p la node cuoi

deleteLast(pHead);

return;

else{

Node* left, *right;

left = p->prev;

right = p->next;

left->next = right;

right->prev = left;

delete p;

p = NULL;

//deleteNodebatki: xoa node nhap tu ban phim

void deleteNodebatki(Node* &pHead){

int vitri;

int n = countNode(pHead);

do{

cout << "Nhap vi tri can xoa: ";

cin >> vitri;

}while(vitri <= 0 || vitri > n);

if(vitri == 1)
deleteFirst(pHead);

else if(vitri == n)

deleteLast(pHead);

//SearchValue: tim kiem node trong danh sach

Node* SearchValue(Node* &pHead, int x){

if(pHead == NULL)

return NULL;

Node* p = pHead;

while(p != NULL && p->data != x)

p = p->next;

return p;

//SortList: sap xep danh sach

void SortList(Node* &pHead){

Node* p, *q;

for(p = pHead; p->next != NULL; p = p->next)

for(q = p->next; q != NULL; q = q->next)

if(p->data > q->data)

swap(p->data, q->data);

You might also like