Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 25

#include <iostream> //TÌM KIẾM NHỊ PHÂN// return mid;//Thấy x tại mid

#include <time.h> else if (x < M[mid])


#include <stdlib.h> right = mid - 1;
using namespace std; else
//Nhap mang left = mid + 1;
void Input(int M[], int n) } while (left <= right);
{ return -1; // Tìm hết dãy mà không có x
for(int i=0;i<n;i++) }
scanf("%d",&M[i]); int main(int argc, char** argv) {
} int n=13;
//Xuat mang cac phan tu int M[]={1, 5, 15, 19, 25, 27, 29, 31,33,45,55,88,100};//khai bao
mang voi n phan tu
void Output(int M[], int n)
int x;
{
printf("Cac gia tri cua mang: \n");
for(int i=0;i<n;i++)
Output(M,n);
printf("%4d",M[i]);
printf("\nnhap gia tri can tim:");
}
scanf("%d",&x);
// Trien khai thuat giai thuat
int kq=BinarySearch(M,n,x);//tham so dung thu tu
int BinarySearch(int M[],int n, int x)
if(kq==-1)
{
printf("khong tim thay gia tri ");
int left = 0, right = n-1;
else
int mid;
printf("tim thay gia tri tai %d",kq);
do
return 0;
{
}
mid = (left + right) / 2;
if (x == M[mid])
#include <iostream> //TÌM KIẾM TUYẾN TÍNH// return -1;// tìm hết mảng nhưng không có x
#include <time.h> }
#include <stdlib.h> //cach 2
using namespace std; //for (i = 0; (i < n) && (M[i] != x); i++);
//Nhap mang //if (i == n)
void Input(int M[], int n) //return -1;// tìm hết mảng nhưng không có x
{ //else
for(int i=0;i<n;i++) //return i; // M[i] là phần tử có khoá x
scanf("%d",&M[i]); int main(int argc, char** argv) {
} int n=8;
//Xuat mang cac phan tu int M[]={25, 20, 9, 5 ,7 ,4, 13, 17};//khai bao mang voi n phan tu
void Output(int M[], int n) int x;
{ printf("Cac gia tri cua mang: \n");
for(int i=0;i<n;i++) Output(M,n);
printf("%4d",M[i]); printf("\nnhap gia tri can tim:");
} scanf("%d",&x);
// Trien khai thuat giai thuat int kq=LinearSearch(M,x,n);
int LinearSearch(int M[],int x, int n) if(kq==-1)
{ printf("khong tim thay gia tri ");
int i; else
for (i = 0; i < n; i++) printf("tim thay gia tri %d tai %d",x,kq);
{ return 0;
if(M[i] == x) }
return i; // M[i] là phần tử có khoá x
}
#include <stdio.h> //BÀI KIỂM TRA LMS DANH SÁCH LIÊN }
KẾT//
// Tác vu kiem tra rong
#include <conio.h>
int IsEmpty(List t){
#include <stdlib.h>
return (t.phead==NULL);
#include <string.h>
}
//Khai bao bien sinh vien kieu cau truc
//Tao node trong danh sach
typedef struct sinhvien {
NODEPTR CreateNode(sinhvien x){
char ten[40];
NODEPTR p= new Node;
char mssv [10];
if (p==NULL) exit(1);
float dtb;
p->info=x;
}sinhvien;
p->next=NULL;
//Khai bao node
return p;
typedef struct node
}
{
//Thao tac nhap thong tin sinh vien
sinhvien info;
void Input (sinhvien &x){
struct node* next;
printf("\nNhap ma so sv : "); fflush(stdin); gets(x.mssv);
}Node;
printf("\nNhap ho ten: "); fflush(stdin); gets(x.ten);
typedef Node* NODEPTR;
printf("\nNhap diem trung binh: "); fflush(stdin); scanf("%f",
//Tao danh sach &x.dtb);
typedef struct List { }
Node *phead; // Them vao dau danh sach
Node *ptail; void InsertHead(List &t, NODEPTR p){
}List; if (t.phead==NULL){
//Khoi tao t.phead=t.ptail=p;
void Init(List &t){ }
t.phead = t.ptail = NULL; else{
p->next = t.phead; void nhap (List &t){
t.phead=p; int a;
} sinhvien x;
} char check;
//Them vao cuoi danh sach printf("\nNhap so sinh vien trong danh sach: ");
void InsertLast (List &t, NODEPTR p){ scanf("%d",&a);
if (t.phead==NULL){ printf("\nNhap thong tin tu ban phim. ");
t.phead=t.ptail=p; printf("\nNhan phim bat ki de bat dau nhap");
} printf("\nNhan 0 de dung nhap !\n");
else{ do{
t.ptail->next=p; check=getch();
t.ptail=p; if(check=='0') break;
} Input(x);
} NODEPTR p=CreateNode(x);
//Ham xac dinh vi tri InsertLast(t,p);
NODEPTR xacdinhvitri(List t, int pos){ } while (1);
int vt=0; }
NODEPTR p=t.phead; //Xuat thong tin sinh vien
while (p!=NULL && vt<pos){ void output(sinhvien x){
vt++; printf("\n MSSV: %s Ho ten: %s Diem tb:
%.2f",x.mssv,x.ten,x.dtb);
p=p->next;
fflush(stdin);
}
}
return p;
//Ham xuat danh sach sv
}
void xuat(List t){
//Nhap danh sach sv
NODEPTR p;
p=t.phead; }
if(p==NULL) void Sortdtb(List &t)
printf("\n Danh sach rong!!!"); {
while(p!=NULL){ NODEPTR p,q;
output(p->info); p=t.phead;
p=p->next; sinhvien temp;
} for(p=t.phead;p->next!=NULL;p=p->next)
} {
//Sap xep theo thu tu tang dan dtb (dung vong lap while) for (q=p->next;q!=NULL;q=q->next)
void sapxep (List &t){ {
NODEPTR p, q, min; if(p->info.dtb<q->info.dtb)
p=t.phead; {
sinhvien temp; temp=p->info;
while (p!=t.ptail){ p->info=q->info;
min=p; q->info=temp;
q=p->next; }
while (q!=NULL){ }
if(q->info.dtb<min->info.dtb) min=q; }
q=q->next; }
} void Sortmssv(List &t)
temp=p->info; {
p->info=min->info; NODEPTR p,q;
min->info=temp; p=t.phead;
p=p->next; sinhvien temp;
} for(p=t.phead;p->next!=NULL;p=p->next)
{ NODEPTR p,q,tam;
for (q=p->next;q!=NULL;q=q->next) sinhvien x;
{ Init(t);
if(strcmp(p->info.mssv,q->info.mssv)>0) int pos;
{ do{
temp=p->info; printf("\n\t ----Menu---- ");
p->info=q->info; printf("\n----Moi ban chon mot trong cac thao tac
sau----");
q->info=temp;
printf("\n1.Nhap danh sach sinh vien");
}
printf("\n2.Xuat danh sach sinh vien");
}
printf("\n3.Sap xep ds giam dan theo DTB");
}
printf("\n4.Sap xep ds tang dan theo MSSV");
}
printf("\n5.Xoa toan bo danh sach");
// Xoa toan bo dssv
printf("\n0.Thoat chuong trinh\n");
void DeleteAll (List &t){
printf("\nChon thao tac:");
NODEPTR p = t.phead;
scanf("%d",&chon);
while(p!=NULL){
fflush(stdin);
NODEPTR nextNode = p->next;
switch(chon){
delete p;
case 1: //Nhap danh sach
p = nextNode;}
nhap(t);
t.phead=NULL;//node dau bang rong
break;
printf("\nDa xoa toan bo danh sach sinh vien!\n");
case 2: //Xuat sanh sach
}
xuat(t);
int main (){
break;
int chon;
case 3: //Sap xep ds giam dan theo dtb
List t;
Sortdtb(t); #include <iostream> //INTERCHANGE SORT//
printf("\n Da sap xep danh sach giam dan #include <time.h>
theo dtb!\n");
#include <stdlib.h>
printf("\n Danh sach sinh vien hien tai: \
n"); using namespace std;

xuat(t); //Nhap mang tu dong

break; void Input(int M[], int n)

case 4: //Sap xep ds tang dan theo mssv {

Sortmssv(t); srand(time(NULL));

printf("\n Da sap xep danh sach tang dan for(int i=0;i<n;i++)


theo mssv!\n"); M[i]=rand()%100-40;//cho random so
printf("\n Danh sach sinh vien hien tai: \ }
n");
//Xuat mang cac phan tu
xuat(t);
void Output(int M[], int n)
break;
{
case 5: //Xoa toan bo ds
for(int i=0;i<n;i++)
DeleteAll(t);
printf("%4d",M[i]);
break;
}
default: chon=0;
// Trien khai thuat giai
break;
void InterchangeSort(int M[], int n)
}
{
} while (chon!=0);
int i, j;
return 0;
for (i = 0; i < n - 1; i++)
}
{
for (j = i + 1; j < n; j++)
{
if (M[j] < M[i]) #include <iostream> //SELECTION SORT//
{ #include <time.h>
int temp = M[i]; #include <stdlib.h>
M[i] = M[j]; using namespace std;
M[j] = temp; //Nhap mang tu dong
} void Input(int M[], int n)
} {
} srand(time(NULL));
} for(int i=0;i<n;i++)
int main(int argc, char** argv) M[i]=rand()%100-40;//cho random so
{ }
int M[]={100,3,60,35,2}; //Xuat mang cac phan tu
int n=5; void Output(int M[], int n)
printf("truoc khi InterchangeSort: \n"); {
Output(M,n); for(int i=0;i<n;i++)
InterchangeSort(M,n); printf("%4d",M[i]);
printf("\nsau khi InterchangeSort: \n"); }
Output(M,n); // Trien khai thuat giai
return 0; void SelectionSort(int M[], int n)
} {
int min;
for(int i=0;i<n-1;i++)
{
min = i;
for(int j=i+1;j<n;j++)
{ #include <stdio.h> //DANH SÁCH LIÊN KẾT//
if (M[j] < M[min]) #include <conio.h>
min = j; #include <string.h>
} #include <stdlib.h>
if(min!=i) #define max 100
{ typedef struct node
int temp = M[i]; {
M[i] = M[min]; int info;
M[min] = temp; node* next;
} }Node;
} typedef Node* NODEPTR;
} //khoi tao
int main(int argc, char** argv) void Init(NODEPTR &phead){
{ phead=NULL;
int M[]={3,5,1,6,12,7,4,10,2}; }
int n=9; // Tác vu kiem tra rong
printf("truoc khi select sort: \n"); int IsEmpty(NODEPTR phead){
Output(M,n); return (phead==NULL);
SelectionSort(M,n); }
printf("\nsau khi select sort: \n"); // Tác vu tao nut x
Output(M,n); NODEPTR CreateNode(int x){
return 0; NODEPTR p=new Node;
} p->info=x;
p->next=NULL;
return p;
} phead=p;
// Tác vu InsertLast }
void InsertLast(NODEPTR &phead, int x){ }
NODEPTR tam=CreateNode(x); // Tác vu nodePointer: xac dinh nut thu i.
NODEPTR p; NODEPTR NodePointer(NODEPTR phead, int i){
p = phead; NODEPTR q=phead;
if (phead==NULL) int vitri=0;
phead=tam; while(q!=NULL && vitri<i){
else q=q->next;
{ vitri++;
NODEPTR ptemp=phead; }
while (ptemp->next!=NULL) return q;
ptemp = ptemp->next; }
ptemp->next=tam; //them node p vao vi tri bat ki//
} void InsertAfter(NODEPTR q, int m)
} {
//them node p vao dau danh sach// NODEPTR tam;
void InsertFirst(NODEPTR &phead, int x) if(q!=NULL)
{ {
NODEPTR p=CreateNode(x); tam=CreateNode(m);
if(phead==NULL) tam->next=q->next;
phead=p; q->next=tam;
else }
{ }
p->next=phead; // Nhap danh sach
void Input(NODEPTR &phead) //Hàm chính
{ int main()
NODEPTR p; {
char s[max]; //khai báo các bien quan lý danh sách
int x; Node* phead; //bien tro den nút dau tiên trong danh sách
Init(phead); Init(phead); //khoi tao danh sách liên ket ban dau chua có
nút nào
while(1)
//Nhap danh sach
{
printf("\n Nhap danh sach: Nhan phim Enter 2 lan de ket
gets(s); thuc! ");//khi nhap danh sach nho enter moi phan tu, khong mac cong no
if(*s==0) break; dinh lai//

x=atoi(s); //phai khai bao #include <stdlib.h> Input(phead);

InsertLast(phead,x); //Xuat danh sach

} printf("\n Danh sach sau khi nhap: ");

} Output(phead);
//them node p vao dau danh sach//
//Tác vu Xuat danh sach int x;

void Output(NODEPTR phead){ printf("\n");

NODEPTR p=phead; printf("nhap gia tri them o dau danh sach: ");

if(p==NULL) scanf("%d",&x);

printf("\n Danh sach bi rong"); InsertFirst(phead,x);

while(p!=NULL){ Output(phead);

printf("%5d",p->info); int m;

p=p->next; int pos;

} 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;

printf("\nDanh sach sau khi nhap: "); }

Output(phead);
do{ return 0;

printf("\n Nhap gia tri chan can tim trong danh sach: "); }

scanf("%d", &x); //khoi tao

}while(x%2!=0); void Init(NODEPTR &phead){

NODEPTR result = TimKiemChan(phead, x); phead=NULL;

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;

scanf("%d", &key); p->info=x;

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;

You might also like