Professional Documents
Culture Documents
CODE
CODE
Sortmssv(t); srand(time(NULL));
} Output(phead);
//them node p vao dau danh sach//
//Tác vu Xuat danh sach int x;
NODEPTR p=phead; printf("nhap gia tri them o dau danh sach: ");
if(p==NULL) scanf("%d",&x);
while(p!=NULL){ Output(phead);
printf("%5d",p->info); int m;
} NODEPTR q;
} printf("\n");
printf("nhap gia tri them o vi tri bat ki: "); #include<stdio.h> //DANH SÁCH ĐẶC//
scanf("%d",&m); #include<conio.h>
printf("nhap vi tri muon them: "); #include<string.h>
scanf("%d",&pos); #define max 100
if(pos==0) typedef struct danhsachdac
InsertFirst(phead,m); {
else int num;
{ int a[max];
q=NodePointer(phead,pos-1); }
if(q==NULL) list;
printf("vi tri them khong hop le"); void init (list&plist)//khoi tao danh sach//
else {
InsertAfter(q,m); plist.num=0;//plist so phan tu//
} }
Output(phead); int listsize(list plist)
return 0; {
} return plist.num;//xax dinh so phan tu//
}
int isempty (list plist)//kiem tra danh sach rong//
{
return(plist.num==0);//bang 1 la rong, 0 la nguoc lai//
}
int isfull(list plist)//kiem tra danh sach day//
{
return(plist.num==max);
} {
void input(list &plist) int i;
{ if(isempty(plist)==1)
int i; {
do printf("khong co phan tu");
{ return;
printf("nhap so phan tu: "); }
scanf("%d",&plist.num); else
}while (plist.num<0); {
printf("phan tu xuat ra la ");
if(isfull(plist)==1) for(int i=0;i<plist.num;i++)
{ {
printf("day du phan tu"); printf("%3d",plist.a[i]);
return; }
} printf("\n");
else }
{ }
for(int i=0;i<plist.num;i++) int retrieve(list plist, int pos)//pos giong kieu index//truy xuat phan tu//
{ {
printf("nhap phan tu thu %d: ",i); if(pos==0||pos>listsize(plist))
scanf("%d",&plist.a[i]); {
} printf("sai vi tri");
} return 0;
} }
void output(list plist) else if(isempty(plist))
{ }
printf("phan tu rong"); plist.a[pos]=x;
} plist.num++;
else return plist.a[pos]; }
} }
void insert(list &plist, int pos, int x) }
{ int search(list plist, int a)
int i; {
if (pos<0||pos>plist.num) int i=0;//i la vi tri index//
{ while(plist.a[i]!=a && i<plist.num)
printf("Vi tri chen sai "); i++;
return ; if(i==plist.num)
} return -1;//tuc la khong tim thay gia tri trong day//
else else return i;//tra ve gia tri tai vi tri tim duoc//
{ }
if(isempty(plist) ) void sort(list &plist)
{ {
printf("Danh sach rong "); int i,j,tam;
return; for(i=0;i<plist.num-1;i++)//nho hon plist.num-1 la o khuc
plist.num-2, plist.num-1 la index cuoi cung//
}
{
else
for(j=i+1;j<plist.num;j++)//no se chay het i dau, roi chay
{ het j. xong sang tang i, lai so i voi het j//
for (i=plist.num-1;i>=pos;i-- ) {
{ if(plist.a[i] >plist.a[j])//muon giam dan thi
plist.a[i+1]=plist.a[i]; doi dau lai//
{ scanf("%d",&x);
tam=plist.a[i]; printf("nhap vi tri muon them: ");
plist.a[i]=plist.a[j]; scanf("%d",&pos);
plist.a[j]=tam; insert(plist,pos,x);
} output(plist);
} int a;
} printf("nhap gia tri can tim: ");
} scanf("%d",&a);
int main(int argc, char** argv) int findval=search(plist,x);
{ if (findval==-1)
list plist; printf("khong tim thay gia tri %d",x);
init (plist); else
input(plist); printf("gia tri %d can tim tai vi tri %d",a,findval);
output(plist); printf("\n");
int pos;
printf("nhap vi tri truy xuat: "); sort(plist);
scanf("%d",&pos); printf("danh sach tang dan co ");
int val=retrieve(plist,pos); output(plist);
if(val==0) return 0;
printf("khong the truy xuat vi tri"); }
else
printf("gia tri cua vi tri %d la %d",pos,val);
printf("\n");
int x;
printf("gia tri phan tu nhap them: ");
#include <iostream> //BUBBLE SORT// for (j = n - 1; j > i; j--)
#include <time.h> {
#include <stdlib.h> if (M[j] < M[j - 1])// neu co nghich the
using namespace std; {
//Nhap mang tu dong int temp = M[j];
void Input(int M[], int n) M[j] = M[j - 1];
{ M[j - 1] = temp;
srand(time(NULL)); }
for(int i=0;i<n;i++) }
M[i]=rand()%200-50; }
}
// for(int i=0;i<n;i++) int main(int argc, char** argv) {
//scanf("%d",&M[i]); int n;
} printf("Nhap kich thuoc mang: ");
//Xuat mang cac phan tu scanf("%d",&n);
void Output(int M[], int n) int M[n];//khai bao mang voi n phan tu
{ Input(M,n);
for(int i=0;i<n;i++) printf("Mang cac gia tri sau khi nhap (chua sap xep): ");
printf("%4d",M[i]); Output(M,n);
} BubbleSort(M,n);
// Trien khai thuat giai printf("\nMang cac gia tri sau khi sap xep: ");
void BubbleSort(int M[], int n) Output(M,n);
{ int i, j; return 0;
for (i = 0; i < n - 1; i++) }
{
#include <iostream> //TREE// p->pright=NULL;
using namespace std; return p;
typedef struct nodetype{ }
int data; int InsertNode(NODEPTR &root, int x)
struct nodetype *pleft; {
struct nodetype *pright; if(root!=NULL)
}Node; {
if(root->data==x) //neu da co x trong cay
typedef Node * NODEPTR; return -1;
//Khoi tao cay if (root->data>x)
void Init(NODEPTR &p) return InsertNode(root->pleft,x);
{ else
p=NULL; return InsertNode(root->pright,x) ;
} }
//Kiem tra cay co rong root=CreateNode(x) ;
int IsEmpty(NODEPTR p) if (root==NULL)
{ return 0;//khong du bo nho
return p==NULL; root->data=x;
} root->pleft=root->pright=NULL;
//Tao nut return 1;// them thanh cong
NODEPTR CreateNode(int x) }
{ void PreOrder_NLR(NODEPTR root)
NODEPTR p =new Node; {
p->data=x; if(root !=NULL)
p->pleft=NULL; {
printf("%4d",root->data); if ( root == NULL )
PreOrder_NLR(root->pleft); return NULL;
PreOrder_NLR(root->pright); else if( root->pleft == NULL )
} return root;
} else
void PostOrder_LRN(NODEPTR root) return FindMin( root->pleft );
{ }
if(root !=NULL) NODEPTR FindMax(NODEPTR root)
{ {
PostOrder_LRN(root->pleft); if (root != NULL )
PostOrder_LRN(root->pright); while (root->pright != NULL )
printf("%4d",root->data); root = root->pright;
} return root;
} }
void InOrder_LNR(NODEPTR root) int Delete(NODEPTR &root, int X)
{ {
if(root !=NULL) NODEPTR p;
{ if ( root == NULL )
InOrder_LNR(root->pleft); return 0 ; // cây rỗng, không tìm thấy
printf("%4d",root->data); else if ( X < root ->data) // xóa trên cây con trái
InOrder_LNR(root->pright); return Delete(root->pleft,X);
} else if ( X > root->data ) // xóa trên cây con phải
} return Delete(root->pright,X);
NODEPTR FindMin(NODEPTR root) else if ( root->pleft && root->pright ) // Có hai con
{ {
p = FindMax(root->pleft); //tìm nút có khóa lớn nhất trên con trái }while (true);
root->data = p ->data;//sao chép findmax thế root }
return Delete(root->pleft,root->data); //Nhap cay C2 (tham khao)
} void Input2(NODEPTR &root)
else // có 1 con hoac không có con {
{ int i,n,x;
p = root; printf("Nhap so nut:");scanf("%d",&n);
if ( root-> pleft == NULL ) // xử lý như không có con for(i=0;i<n;i++)
root = root->pright; {
else if ( root->pright == NULL ) printf("Nut %d: ",i+1);
root = root->pleft; scanf("%d",&x);
delete p; InsertNode(root,x) ;
} }
return 1; }
} int main(int argc, char** argv) {
//Nhap cay C1 NODEPTR root;
void Input(NODEPTR &root) Init(root);
{ printf("\n\n1: Nhap cac nut trong cay\n");
int x; Input(root);
do{ printf("\n Cay nhi phan tim kiem BST theo NLR:\n ");
cout<<"Nhap gia tri: "; PreOrder_NLR(root);
cin>>x; printf("\n Cay nhi phan tim kiem BST theo LNR:\n");
int kq=InsertNode(root,x); InOrder_LNR(root);
if(kq==0||kq==-1) printf("\n Cay nhi phan tim kiem BST theo LRN:\n");
break; PostOrder_LRN(root);
printf("\n"); #include <stdio.h> //DANH SÁCH LIÊN KẾT TÌM CHẴN LẺ//
printf("gia tri nho nhat la: "); #include <conio.h>
NODEPTR kq=FindMin(root); #include <string.h>
printf("%d\n",kq->data);//data là gọi dữ liệu trong cái nút// #include <stdlib.h>
printf("gia tri lon nhat la: "); #define MAXLIST 100
NODEPTR kt=FindMax(root); typedef struct node{
printf("%d\n",kt->data); int info;
int X; node* next;
printf("nhap nut de xoa: ",X); }Node;
scanf("%d",&X); typedef Node* NODEPTR;
Delete(root,X); void Init(NODEPTR &phead);
printf("cay sau khi xoa:\n"); int IsEmpty(NODEPTR phead);
PreOrder_NLR(root); NODEPTR CreateNode(int x);
printf("\n"); void InsertLast(NODEPTR &phead, int x);
InOrder_LNR(root); void Input(NODEPTR &phead);
Printf("\n"); void Output(NODEPTR phead);
PostOrder_LRN(root); void IncreaseSort(NODEPTR &phead);
return 0; void DecreaseSort(NODEPTR &phead);
} NODEPTR TimKiemChan(NODEPTR phead, int x);
void LeTang(NODEPTR &phead);
void ChanGiam(NODEPTR &phead);
//Hàm chính
int main()
{
//khai báo các bien quan lý danh sách
Node* phead; //bien tro den nút dau tiên trong danh sách LeTang(phead);
Init(phead); //khoi tao danh sách liên ket ban dau chua có Output(phead);
nút nào
break;
int x;
case 2:
int key;
ChanGiam(phead);
printf("\nNhap danh sach: Nhan phim Enter 2 lan de ket
thuc!\n"); Output(phead);
Input(phead); break;
Output(phead);
do{ return 0;
printf("\n Nhap gia tri chan can tim trong danh sach: "); }
if(result != NULL){ }
printf("Tim thay phan tu %d trong danh sach", // Tác vu kiem tra rong
result->info); int IsEmpty(NODEPTR phead){
} return (phead==NULL);
else }
printf("Khong tim thay phan tu %d trong danh // Tác vu tao nut x
sach", x);
NODEPTR CreateNode(int x){
printf("\nNhap 1 de sap xep le tang dan, 2 de sap xep so
chan giam dan: "); NODEPTR p=new Node;
switch(key){ p->next=NULL;
case 1: return p;
}
NODEPTR TimKiemChan(NODEPTR phead, int x) ptemp->next=tam;
{ }
int n; }
NODEPTR p = phead; // Nhap danh sach
while(p!=NULL){ void Input(NODEPTR &phead)
if(p->info == x){ {
break; NODEPTR p;
} char s[MAXLIST];
p = p->next; int x;
} Init(phead);
return p; while(1)
} {
// Tác vu InsertLast gets(s);
void InsertLast(NODEPTR &phead, int x){ if(*s==0) break;
NODEPTR tam=CreateNode(x); x=atoi(s); //phai khai bao #include <stdlib.h>
NODEPTR p; InsertLast(phead,x);
p = phead; }
if (phead==NULL) }
phead=tam; //Tác vu Xuat danh sach
else void Output(NODEPTR phead){
{ NODEPTR p=phead;
NODEPTR ptemp=phead; if(p==NULL)
while (ptemp->next!=NULL) printf("\n Danh sach bi rong");
ptemp = ptemp->next; while(p!=NULL){
printf("%5d",p->info); if(p->info > q->info){
p=p->next; temp = p->info;
} p->info = q->info;
} q->info = temp;
void IncreaseSort(NODEPTR &phead){ }
NODEPTR p, q; }
int temp; void ChanGiam(NODEPTR &phead)
for(p = phead; p->next != NULL; p = p->next){ {
for(q = p->next; q != NULL; q = q->next){ NODEPTR p, q;
if(p->info > q->info){ int temp;
temp = p->info; for(p = phead; p->next != NULL; p = p->next){
p->info = q->info; if(p->info % 2 == 0)
q->info = temp; for(q = p->next; q != NULL; q = q->next){
} if( q->info % 2 == 0 )
} if(p->info < q->info){
} temp = p->info;
} p->info = q->info;
void LeTang(NODEPTR &phead) q->info = temp;
{ }
NODEPTR p, q, x; }
int temp; }
for(p = phead; p->next != NULL; p = p->next) }
if(p->info % 2 != 0) void DecreaseSort(NODEPTR &phead){
for(q = p->next; q != NULL; q = q->next) NODEPTR p, q;
if( q->info % 2 != 0 ) int temp;
for(p = phead; p->next != NULL; p = p->next){ printf("nhap: ");
for(q = p->next; q != NULL; q = q->next){ scanf("%d",&i);
if(p->info < q->info){ ChuyenCoSo10SangCoSo2(i);//không cần printf nha má, do nó printf
phía trên kia ròi//
temp = p->info;
return 0;
p->info = q->info;
}
q->info = temp;
#include <iostream>//đề bài là tính tổng số tự nhiên theo thứ tự
} 1,2,3,4,...,n//
} int TongS(int n)
} {
} if(n==0)
#include<stdio.h> //CHUYỂN CƠ SỐ TỪ 10 SANG 2// return 0;
#include<stdlib.h> return TongS(n-1)+n;
void ChuyenCoSo10SangCoSo2(int n)//đề bài là chuyển cơ số thập }
phân sang nhị phân//
int main()
{
{
int t;//t là phần dư//
int n;
if(n<=0)//điều kiện sai má ơi, n>=0 là sai//
printf("nhap n: ");
return;// C++ hỗ trợ sử dụng kiểu dữ liệu void cho những hàm không
có giá trị trả về// scanf("%d",&n);
t=n%2; int kq=TongS(n);
ChuyenCoSo10SangCoSo2(n/2); printf("%d",kq);
printf("%d",t); return 0;
} }
int main() #include <iostream>//đề bài là nhập số thứ tự n và ra giá trị của số
fibo ở thứ tự đó//
{
int DayFibonacci(int n)
int i;
{ printf("nhap n: ");
if(n==1|n==2) scanf("%d",&n);
return 1; int kq=GiaiThua(n);
return DayFibonacci(n-1)+DayFibonacci(n-2); printf("%d!=""%d",n,kq);
} return 0;
int main() }
{ //để n cỡ 100 là stack overhead liền, tràn bộ nhớ, tràn đệ quy//
int n;
printf("nhap so thu tu n: ");
scanf("%d",&n);
int kq=DayFibonacci(n);
printf("%d",kq);
return 0;
}
#include <iostream> //TÍNH GIAI THỪA//
int GiaiThua(int n)
{
if(n==1|n==0)
return 1;
return GiaiThua(n-1)*n;
}
int main()
{
int n;