Post on 23-Nov-2014
transcript
TAP HOP#define M (N/8+1)typedef unsigned char taphop[M];void taprong(taphop s){
int i;for(i=0;i<M;i++)
s[i]=0;}void thempt(taphop s,int x){
s[x/8]=s[x/8]|(1<<x%8);}void loaipt(taphop s,int x){
unsigned char k=1<<(x%8);s[x/8]=s[x/8]&~k;
}int thuocve(taphop s,int x){
return s[x/8]&(1<<x%8);}void hoi(taphop a,taphop b,taphop c){
int i;for(i=0;i<M;i++)
c[i]=a[i]|b[i];}
STACK#include <stdlib.h>#include <string.h>typedef struct {
elem e[Max];int top;
} stack;void create_stack(stack &s){
s.top = -1;}int empty_stack(stack s){
return s.top == -1;}void push(stack &s, elem &x){
if(s.top == Max - 1) exit(0);// stdlib.hmemcpy(&s.e[++s.top], &x, sizeof(elem));
}void pop(stack &s, elem &x){
if(s.top == -1) exit(0);memcpy(&x, &s.e[s.top--], sizeof(elem));
}
QUEUE#include <stdlib.h>#include <string.h>
typedef struct {
elemQ e[MAXQ];int front, rear;
}queue;
void createqueue(queue &q){
q.front=q.rear=0;}int emptyqueue(queue q){
return(q.front==q.rear);}void addqueue(queue &q, elemQ &x){
int newr=(q.rear+1)%MAXQ;if(q.front == newr)
exit(0);memcpy(&q.e[q.rear],&x,sizeof(elemQ));q.rear=newr;
}void removequeue(queue &q, elemQ &x){
if(emptyqueue(q))exit(0);
memcpy(&x,&q.e[q.front],sizeof(elemQ));q.front=(q.front+1)%MAXQ;
}
LIST#include"memory.h"typedef struct nodet {
elem data;struct nodet *next;
}node;typedef node *list;void creatlist(list &l){
l=NULL;}int emptylist(list &l){
return(l==NULL);}void insertlist(list &l,elem &x,list p){
list newp= new node;memcpy(&newp->data,&x,sizeof(elem));if(p==NULL){
newp->next=l;l=newp;
}else{
newp->next=p->next;p->next=newp;
}}void deletelist(list &l,list p){
Trang 1
list t;if(p==NULL){
t=l;l=t->next;
}else{
t=p->next;p->next=t->next;
}delete t;
}list searchlist(list l,elem x,list &p,int (*comp)(elem,elem)){
list c;c=l;p=NULL;while(c!=NULL && comp(x,c->data)!=0){
p=c;c=c->next;
}return c;
}list searchorderlist(list l,elem x,list &p,int (*comp)(elem,elem)){
list c;c=l;p=NULL;while(c!=NULL && comp(x,c->data)>0){
p=c;c=c->next;
}if(c!=NULL && comp(x,c->data)<0)
return NULL;else
return c;}void getdata(list l,elem &x){
memcpy(&x,&l->data,sizeof(elem));}list skip(list l){
return (l->next);}BAI TAP CAU TRUC DU LIEU VA GIAI THUAT
TAP HOP#include<iostream.h>#include<iomanip.h>#include<math.h>#define N 1000#include"taphop.cpp"
void main(){
int i,n,k;double can;taphop s;cout<<"Nhap n:";cin>>n;can=sqrt(n);taprong(s);for(i=0;i<=n;i++)
thempt(s,i);i=2;while(i<=can){
k=i*i;while(k<=n){
loaipt(s,k);k=k+i;
}do{i++;}while(!thuocve(s,i));cout<<"Ket qua la:";for (i=2;i<=n;i++)
if(thuocve(s,i))cout<<setw(4)<<i;
}}
STACK#include <iostream.h>#include <string.h>#define Max 32typedef int elem;#include "STACK.CPP"//VAO SAU RA TRUOC
//HAM NHAP CAC PHAN TU VAO STACKvoid nhap(stack &s){
int x,n;cout<<"\nNhap vao bao nhieu so ";cin>>n;cout<<"Nhap vao ";for(;n>0;n--){
cin>>x;push(s,x);
}}//HAM XUAT SO HE 2,8,16 CUA STACKvoid xuat(stack &s){
char h16[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};int x;while(!empty_stack(s)){
Trang 2
pop(s,x);cout<<h16[x]<<" ";
}}//DOI TU HE 10 SANG HE 2,8,16void he10sang(stack &s,int N,int he){
int x;while(N>0){
x=N%he;push(s,x);N=N/he;
}}//HAM CHUYEN TU HE 2,8,16 SANG HE 10int sang10(stack &s,int he){
int T=0; int mu=1,x;while(!empty_stack(s)){
pop(s,x);T=T+x*mu;mu=mu*he;
}return T;
}
void main(){
int N; stack s;cout<<"Nhap n: "; cin>>N;create_stack(s);he10sang(s,N,2);cout<<"\nKet qua he 2 la: "; xuat(s);he10sang(s,N,8);cout<<"\nKet qua he 8 la: "; xuat(s);he10sang(s,N,16);cout<<"\nKet qua he 16 la: "; xuat(s);cout<<"\n\nNhap vao so he 2 doi sang he
10"; nhap(s);cout<<"Chuyen he 2 sang he 10
"<<sang10(s,2);cout<<"\n\nNhap vao so he 8 doi sang he
10"; nhap(s);cout<<"Chuyen he 8 sang he 10
"<<sang10(s,8);cout<<"\n\nNhap vao so he 16 doi sang he
10"; nhap(s);cout<<"Chuyen he 16 sang he 10
"<<sang10(s,16);cout<<"\n";
}
#include <iostream.h>#include <string.h>#define Max 100typedef char elem;
#include "STACK.CPP"
//HAM DAO CHUOI BANG STACKvoid daotu(stack &s1,char N[]){
int i; char k;for(i=strlen(N)-1;i>-2;i--)
if(N[i]!=' ' && i!=-1)push(s1,N[i]);
else{
while (!empty_stack(s1)){
pop(s1,k);cout<<k;
}if(i!=-1)
cout<<" ";}
}
void main(){
char N[100]; stack s1;create_stack(s1);cout<<"Nhap vao chuoi ki tu ";cin.getline(N,100);daotu(s1,N);cout<<"\n";
}
HAM TINH TOAN MOT BIEU THUC#include <iostream.h>#include <string.h>#define Max 100typedef char elem[7];#include "STACK.CPP"
//HAM KIEM TRA XEM CO PHAI LA DAUint ktdau(char s){
if(s=='+' || s=='-'|| s=='*'|| s=='/' || s==')' || s=='(')
return 1;return 0;
}//KIEM TRA DO UU TIEN DAUint uutien(char s1){
switch(s1){
case'*':case'/':return 2;case'+':case'-':return 1;
}return 0;
}//HAM TACH CHUOI VAO STACKvoid tach( char *s, stack &s1){
char tu[7];
Trang 3
int i,d=strlen(s),k=0,kt=0;for(i=0;i<d;i++)
if(ktdau(s[i])==1){
tu[0]=s[i];tu[1]='\0';push(s1,tu);
}else{
tu[k++]=s[i];tu[k]='\0';if(ktdau(s[i+1])==1 ||
(i+1==d)){
push(s1,tu);k=0;
}}
}//HAM DAO STACKvoid daostack(stack &s1){
char tu[7];stack s2,s3;create_stack(s2);create_stack(s3);while(!empty_stack(s1)){
pop(s1,tu);push(s2,tu);
}while(!empty_stack(s2)){
pop(s2,tu);push(s3,tu);
}while(!empty_stack(s3)){
pop(s3,tu);push(s1,tu);
}}//TRUNG TO s2 SANG HAU TO s1void trung_hau( stack &s1,stack &s2){
char moi[7], old[7];int kt=1;stack dau;create_stack(dau);while(!empty_stack(s2)){
pop(s2,moi);if(ktdau(moi[0])==0)
push(s1,moi);else
if(moi[0]=='(')push(dau,moi);
elseif(moi[0]==')')
{
pop(dau,old);
while(old[0]!='('){
push(s1,old);
pop(dau,old);}
}else{
if(empty_stack(dau)==1)
push(dau,moi);else{
kt=1;do{
pop(dau,old);
if(uutien(old[0])>=uutien(moi[0]))
push(s1,old);
else
{
kt=0;
push(dau,old);
}}w
hile(kt==1 && empty_stack(dau)==0);
push(dau,moi);}
}}while(!empty_stack(dau)){
pop(dau,old);push(s1,old);
}}//TINH TOAN TU 3 CHUOIvoid tinhtoan(char *s1,char s2[7],char dau[7]){
switch(dau[0]){
Trang 4
case '+':itoa(atoi(s1)+atoi(s2),s1,10);break;
case '-':itoa(atoi(s1)-atoi(s2),s1,10);break;
case '*':itoa(atoi(s1)*atoi(s2),s1,10);break;
case '/':itoa(atoi(s1)/atoi(s2),s1,10);break;
}}//DOI TU HAU TO SANG SOvoid hau_so(stack &s1){
char t1[7],t2[7],t3[7];stack s2;create_stack(s2);while(!empty_stack(s1)){
pop(s1,t1);if(ktdau(t1[0])==0)
push(s2,t1);else{
pop(s2,t2);pop(s2,t3);tinhtoan(t3,t2,t1);push(s2,t3);
}}pop(s2,t1);push(s1,t1);
}void main(){
char s[100],tu[7];stack s1,s2;create_stack(s1);create_stack(s2);cout<<"Nhap lieu\n";cin.getline(s,100);tach(s,s2);daostack(s2);trung_hau(s1,s2);daostack(s1);hau_so(s1);while(!empty_stack(s1)){
pop(s1,tu);cout<<tu<<" ";
}cout<<"\n";
}
#include <iostream.h>#include <string.h>#define Max 32typedef char elem[2];#include "STACK.CPP"
//PHAN TICH MOT SO THANH THUA SO NGUYEN TO 126=2*3*3*7void ptnguyento(long n){
int i=2;while(n>1){
if(n%i==0){
if(n==i)cout<<i;
elsecout<<i<<"*";
n=n/i;}else
i++;}
}//PHAN TICH MOT SO THANH THUA SO NGUYEN TO THEO THU TU NGUOC VA CO MU//1500=5^3 * 3 * 2^2void ptnguyento2(long n){
int i=2,mu=0; stack s;char tu[2];create_stack(s);while(n>1){
if(n%i==0){
mu++;n=n/i;if(n%i!=0){
if(mu>1){
itoa(mu,tu,10);push(s,tu);
push(s,"^");}itoa(i,tu,10);push(s,tu);if(n!=1)
push(s,"*");mu=0;
}}else
i++;}while(!empty_stack(s)){
pop(s,tu );cout<<tu;
}}
Trang 5
void main(){
long n;cout<<"Nhap vao so n ";cin>>n;ptnguyento(n); cout<<"\n";ptnguyento2(n); cout<<"\n";
}
#include <iostream.h>#include <string.h>#define Max 100typedef struct
{int i;char A,B,C;
}elem;#include "STACK.CPP"
//THAP HA NOIvoid main(){
int n; char A,B,C; stack s;create_stack(s);cout<<"Nhap n "; cin>>n;elem bo;bo.i=n; bo.A='A';bo.B='B'; bo.C='C';push(s,bo);do{
pop(s,bo);n=bo.i;A=bo.A;B=bo.B;C=bo.C;if(bo.i==1)
cout<<"\nChuyen tu "<<A<<" sang "<<C;
else{
bo.i=n-1;bo.A=B;bo.B=A;bo.C=C;push(s,bo);
bo.i=1;bo.A=A;bo.B=' ';bo.C=C;push(s,bo);
bo.i=n-1;bo.A=A;bo.B=C;bo.C=B;push(s,bo);
}}while(!empty_stack(s));
cout<<"\n";}
QUEUE:SAP XEP DAY SO BANG THUAT GIAI REDIS SORT#include <iostream.h>#include<stdlib.h>#include<time.h>#include<iomanip.h>#define MAXQ 100typedef int elemQ;#include "QUEUE.CPP"#define N 70//VAO TRUOC RA TRUOC
//HAM TAO MANG 1 CHIEU NGAU NHIENvoid tao(int *a, int &n){
do{
cout<<"Nhap vao so phan tu ";cin>>n;
}while(n<1);int *p=a,*q=p+n;srand((int)time(NULL)); for(;p<q;p++)
*p=rand()%101;}//HAM XUAT MANG 1 CHIEUvoid xuat(int *a,int n){
int *p=a,*q=p+n;for(;p<q;p++)
cout<<setw(5)<<*p;}//TIM MAX CUA MANG 1 CHIEUint Max(int a[],int n){
int *p=a,*q=p+n,*k=a;for(;p<q;p++)
if(*p>*k)k=p;
return *k;}//HOAN VI 2 PHAN TUvoid hoanvi(int &a,int &b){
int tg=a;a=b;b=tg;
}//HAM SO CHU SOint SCS(long n){
int d=1; n=abs(n);for(;n>9;d++)
n=n/10;return d;
}
Trang 6
void main(){
int a[N],n,x,i,k,top=0,end=10,lt=1,j;//Khoi tao 20 hang doi qqueue q[21];for(i=0;i<20;i++)
createqueue(q[i]);//Tao mang ngau nhientao(a,n);xuat(a,n);//So chu so cua phan tu lon nhatk=SCS(Max(a,n))-1;//Bo ca so vao hang doifor(i=0;i<n;i++){
j=a[i]%10;addqueue(q[j],a[i]);
}//Bat dau sap xepdo{
lt=lt*10; k--;for(i=0;i<10;i++){
while(!emptyqueue(q[i+top]))
{
removequeue(q[i],x);j=(x/lt)%10;
addqueue(q[end+j],x);}
}hoanvi(top,end);
}while(k>0);//Xuat ra day da sap xepcout<<"\nGIAI BANG QUEUE\n";for(i=0;i<20;i++){
while(!emptyqueue(q[i])){
removequeue(q[i],x);cout<<setw(5)<<x;
}}cout<<"\n";
}//Bai doi chuoi thanh so thuc Dung QUEUE.#include<iostream.h>#include<math.h>#define Max 10#define ch 20typedef int elem;#include"QUEUE.CPP"void main(){
queue l1,l2;createqueue(l1);
createqueue(l2);float n=0.0,m=0.0,p=0.0;char a[ch];int d=1,i=0,dem=0,x;cout<<"\n hay nhap vao chuoi can doi:";cin.getline(a,ch);while(a[i]==' '|| a[i]=='-'){
if(a[i]=='-')d=-d;
i++;}while(a[i]>='0'&&a[i]<='9'){
int so =a[i]-'0';addqueue(l1,so);i++;
}i++;while(a[i]>='0'&&a[i]<='9'){
int so1=a[i]-'0';addqueue(l2,so1);dem++;i++;
}while(!emptyqueue(l1)){
removequeue(l1,x);n=n*10+x ;
}while(!emptyqueue(l2)){
removequeue(l2,x);m=m*10+x;
}m=m/float(pow(10,dem));p=(m+n)*d;
cout<<p;}#include <iostream.h>#include<stdlib.h>#include<time.h>#include<iomanip.h>#define MAXQ 20typedef char elemQ;#include "QUEUE.CPP"
//VAO TRUOC RA TRUOC//HAM DOI CHUOI THANH SO NGUYENint a_toi(char *s){
int n=0,d=strlen(s),dau=1;char *i=s,x;queue q; createqueue(q);//Bo chuoi so vao QUEUEfor(;!(48<*i && *i<58);i++)
if(*i=='-')addqueue(q,*i);
Trang 7
elsereturn 0;
for(;48<*i && *i<58;i++)addqueue(q,*i);
//DOI CHUOI THANH SOwhile(!emptyqueue(q)){
removequeue(q,x);if(x=='-')
dau=-dau;else
n=n*10+x-'0';}return dau*n;
}//HAM DOI CHUOI THANH SO THUClong double a_tof(char *s){
long double n=0,f=0;int d=strlen(s),dau=1;char *i=s,x;queue q,q1; createqueue(q);createqueue(q1);//Bo chuoi so vao QUEUEfor(;!(48<*i && *i<58);i++)
if(*i=='-')addqueue(q,*i);
elsereturn 0;
for(;48<*i && *i<58;i++)addqueue(q,*i);
if(*i=='.')for(++i;48<*i && *i<58;i++)
addqueue(q1,*i);//DOI CHUOI THANH SOwhile(!emptyqueue(q)){
removequeue(q,x);if(x=='-')
dau=-dau;else
n=n*10+x-'0';}while(!emptyqueue(q1)){
removequeue(q1,x);f=f*10+x-'0';
}while(f>1)
f=f/10;return dau*(n+f);
}
void main(){
char s[20];cout<<"\nBan nhap vao chuoi ki tu doi sang
so\n"; cin.getline(s,20);cout<<"\nham a_toi "<<a_toi(s)<<"\natoi
cua thu vien "<<atoi(s);cout<<"\nham a_tof "<<a_tof(s)<<"\natof
cua thu vien "<<atof(s)<<"\n";
}
LIEN KET DON
#include <iostream.h>typedef int elem;#include "list.cpp"
//HAM NHAP MANG SO DUONG BANG LIST THEO THU TU BAN PHIMvoid nhapdem(list &l){
creatlist(l);list p; int x;p=l=new node; //pt demdo{
cout<<"Nhap vao phan tu ";cin>>x;
if(x>0){
l->next=new node;l=l->next;l->data=x;
}}while(x>0);l->next=NULL;l=p->next;delete p;
}//HAM XUAT MANG SO BANG LISTvoid xuat(list l){
while(l!=NULL){
cout<<l->data<<" ";l=l->next; //tro toi phan tu
tiep theo cua list}
}//HAM TACH CHAN LEvoid tach_chanle(list l, list &l1, list &l2){
creatlist(l1); creatlist(l2);list p1,p2;l1=p1=new node;l2=p2=new node;while(l!= NULL){
if(l->data %2 == 0){
insertlist(l2,l->data,p2); p2 = p2->next;
}else
Trang 8
{insertlist(l1,l->data,p1);p1=p1->next;
}l = l->next;
}p1->next=NULL;p2->next=NULL;p1=l1; p2=l2; //Gan p1=l1 de delete p1
xoa vung deml1=l1->next;l2=l2->next;delete p1;delete p2;
}//dung new node de cap phat vung nho khi can chep gia tri//SAP XEP MANG TANGvoid sxtang(list &l){
list p,pm,c,cm,t;p=new node; //Tao mot phan tu rac tai
dau listp->next=l;l=p;t=NULL;while(l->next!=NULL){
pm=p=l;cm=c=l->next;while(c!=NULL){ //Tim thang lon I dua ra
dau list tif(cm->data<c->data){
pm=p;cm=c;
}p=c;c=c->next;
}pm->next=cm->next;cm->next=t;t=cm;
}delete l;l=t;
}//HAM DAO LISTvoid dao(list &l){
list p=NULL,q;while(l!=NULL){
q=l;l=q->next;q->next=p;p=q;
}l=p;
}
//LAY RA MOT DANH SACH CON TANG (TACH RA) TRA CON TRO VE DAU LISTlist first(list &l){
if(l==NULL) return NULL;list p,c;p=c=l;l=l->next;while(l!=NULL && p->data<=l->data){ p=l;
l=l->next;}p->next=NULL;return c;
}//HAM TRON 2 MANG TANG THANH 1 MANG TANG TRA CON TRO VE CUOIlist merge(list &l,list l1, list l2){
list p,c;l=p=new node;while( l1!= NULL && l2!=NULL){
if(l1->data < l2->data){
l->next=l1;l1=l1->next;
}else{
l->next=l2;l2=l2->next;
}l = l->next;
}if(l1!=NULL)
l->next=l1;else
l->next=l2;l1=l2=NULL;while(l->next!=NULL)
l=l->next;c=l;l=p->next;delete p;return c;
}//HAM SAP XEP BANG PHUONG PHAP TRON MANGvoid mergesort(list &l){
list p,q,r,s,l1,l2;int d;do{
d=0;p=q=new node;while(l!=NULL)
Trang 9
{l1=first(l);l2=first(l);s=merge(r,l1,l2);q->next=r;q=s;d++;
}l=p->next;delete p;
}while(d>1);}
//CO 2 DS TANG NGHIEM NGAT, dem so pt co dong thoi trong 2 dsint dem(list l1,list l2){
int d=0;while(l1!=NULL && l2!=NULL)
if(l1->data<l2->data)l1=l1->next;
elseif(l1->data>l2->data)
l2=l2->next;else{
l1=l1->next;l2=l2->next;d++;
}return d;
}//CO 2 DS TANG NGHIEM NGAT dem so pt co trong ds 1,khong co trong ds2int dem2(list l1,list l2){
int d=0;while(l1!=NULL && l2!=NULL)
if(l1->data<l2->data){
d++;l1=l1->next;
}else
if(l1->data>l2->data)l2=l2->next;
else{
l1=l1->next;l2=l2->next;
}while(l1!=NULL){
d++;l1=l1->next;
}return d;
}//XOA PHAN TU TRUNG NHAU TRONG LIST TANG KHONG NGHIEM NGAT
void xoa_trung(list &l){
list p,t;t=p=l; t=p->next;while(t!=NULL)
if(p->data==t->data){
p->next=t->next;t=p->next;
}else{
p=t;t=p->next;
}}
void main(){
list l,l1,l2;creatlist(l); creatlist(l1);creatlist(l2);nhapdem(l); xuat(l);cout<<"\nTACH RA THANH 2 DANH
SACH CHAN LE\n";tach_chanle(l,l1,l2);xuat(l1); cout<<"\n"; xuat(l2);cout<<"\nSAP XEP TANG LIST\n";sxtang(l1); xuat(l1); cout<<"\n";sxtang(l2); xuat(l2);cout<<"\nTRON 2 MANG TANG THANH 1
MANG TANG\n";merge(l,l1,l2); xuat(l);cout<<"\nLAY RA MOT DANH SACH CON\
n";nhapdem(l); xuat(l);cout<<"\nDanh sach con la\n";l1=first(l); xuat(l1); cout<<"\n";xuat(l); cout<<"\n";cout<<"\nDAO NGUOC LIST\n";dao(l); xuat(l);cout<<"\nSAP XEP BANG PHUONG PHAP
TRON\n";nhapdem(l); xuat(l);cout<<"\nSAU KHI SAP XEP BANG
PHUONG PHAP TRON\n";mergesort(l); xuat(l);cout<<"\nNHAP VAO HAI MANG TANG
TUYET DOI\n";creatlist(l1); creatlist(l2);nhapdem(l1); xuat(l1); cout<<"\n";nhapdem(l2); xuat(l2);cout<<"\nDEM SO PHAN TU CO TRONG
MANG L1 VA L2 "<<dem(l1,l2);cout<<"\nDEM SO PHAN TU CO TRONG
MANG L1 KHONG CO TRONG L2 "<<dem2(l1,l2);cout<<"\nXOA PT TRUNG NHAU TRONG
MANG TANG\n";
Trang 10
creatlist(l); nhapdem(l); xuat(l);cout<<"\nXOA PHAN TU TRUNG NHAU
TRONG MANG TANG L1\n";xoa_trung(l); xuat(l);cout<<"\n";
}
#include <iostream.h>typedef struct
{double he;int mu;
}elem;#include "list.cpp"
//LUU 1 DA THUC TRONG DSLK GIAM DAN THEO SO MUvoid luu_dathuc(list &l){
creatlist(l);list p; double a; int mu;p=l=new node; //pt demdo{
cout<<"\nhe so ";cin>>a;cout<<"so mu ";cin>>mu;if(mu>=0){
l->next=new node;l=l->next;l->data.he=a;l->data.mu=mu;
}}while(mu>0);l->next=NULL;l=p->next;delete p;
}//HAM XUAT MANG SO BANG LISTvoid xuat_dathuc(list l){
while(l!=NULL){
if(l->data.mu==0)cout<<l->data.he;
elsecout<<l->data.he<<"X"<<l-
>data.mu;l=l->next;if(l!=NULL && l->data.he>=0)
cout<<" + ";} //Khong duoc dao if(l->data.he>=0 && l!
=NULL)}//HAM CONG HAI 2 DA THUCvoid cong_dathuc(list &l,list l1,list l2)
{creatlist(l); list p;p=l=new node;while(l1!=NULL && l2!=NULL)
if(l1->data.mu == l2->data.mu){
if(l1->data.he + l2->data.he !=0)
{l->next=new node;l=l->next;l->data.he=l1-
>data.he + l2->data.he;l->data.mu=l1-
>data.mu;}l1=l1->next;l2=l2->next;
}else
if(l1->data.mu > l2->data.mu)
{l->next=new node;l=l->next;l->data.he=l1-
>data.he;l->data.mu=l1-
>data.mu;l1=l1->next;
}else{
l->next=new node;l=l->next;l->data.he=l2-
>data.he;l->data.mu=l2-
>data.mu;l2=l2->next;
}l->next=NULL;l=p->next;delete p;
}//HAM TINH GIA TRI CUA DA THUCdouble giatri(list l,double x){
double kq=l->data.he;int d=l->data.mu;l=l->next;for(d=d-1;d>=0;d--)
if(l!=NULL && l->data.mu==d){
kq=kq*x + l->data.he;l=l->next;
}else
kq=kq*x;return kq;
Trang 11
}
void main(){
double x; list l,l1,l2;creatlist(l); creatlist(l1);creatlist(l2);luu_dathuc(l1); xuat_dathuc(l1);cout<<"\n";luu_dathuc(l2); xuat_dathuc(l2);cout<<"\n";cong_dathuc(l,l1,l2); xuat_dathuc(l);cout<<"\nTinh gia tri cua da thuc vua cong\
n";cout<<"Nhap vao x "; cin>>x;cout<<"Gia tri cua da thuc vua cong la
"<<giatri(l,x)<<"\n";}#include<iostream.h>#include<string.h>#include<stdio.h>#include<math.h>#include<stdlib.h> typedef int elem;#include"dlist1.cpp"
//HAM NHAPvoid nhap(dlist &a){
char s; int sa;cout<<"\nNhap vao so ";cout.flush();createdlist(a);while((s=getchar())!='\n'){
sa=s-'0';insertdlist(a,sa,a.last);
}}//HAM XUATvoid xuat(dlist &a){
nodeptr p;p=a.first;while(p!=NULL){
cout<<p->data;p=p->next; //tro toi phan tu
tiep theo cua list}
}//HAM CONG SO LONvoid cong(dlist a,dlist b,dlist &c){
nodeptr pa,pb;int x,nho=0;createdlist(c);pa=a.last;pb=b.last;while(pa!=NULL && pb!=NULL){
nho=nho + pa ->data +pb ->data;
x=nho%10;insertdlist(c,x,NULL);nho=nho/10;pa= pa ->prev;pb= pb ->prev;
}while(pa!=NULL){
nho=nho+pa ->data;x=nho%10;insertdlist(c,x,NULL);nho=nho/10;pa= pa ->prev;
}
while(pb!=NULL){
nho=nho+pb ->data;x=nho%10;insertdlist(c,x,NULL);nho=nho/10;pb=pb->prev;
}if(nho>0)
insertdlist(c,nho,NULL);}
void main(){
dlist a,b,c;nhap(a);nhap(b);cout<<"Sau khi cong lai la\n";cong(a,b,c); xuat(c);cout<<"\n";
}
DLIST PHUONG#include<iostream.h>#include<stdio.h>#include<string.h>#include<iomanip.h>
typedef struct nodet{elem data;struct nodet *next,*prev;
}node;typedef node *nodeptr;typedef struct{
nodeptr first, last;}dlist;
//KHOI TAO DLISTvoid createdlist (dlist &l){
Trang 12
l.first= l.last = NULL;} //KIEM TRA DLIST RONGint emptydlist(dlist l){
return (l.first == NULL);}//CHEN VAO DANH SACH LIEN KET DOIvoid insertdlist(dlist &l, elem &x,nodeptr p){
nodeptr q,newp;newp= new node;memcpy(&newp->data, &x,sizeof(elem));q=(p == NULL ? l.first : p ->next);newp->next = q;if(p == NULL)
l.first=newp;else
p ->next = newp;newp ->prev = p;if(q==NULL)
l.last = newp;else
q->prev=newp;}//XOA 1 PHAN TU TRONG DANH SACHvoid deletedlist(dlist &l, nodeptr p){
nodeptr q,t;t=(p == NULL ? l.first : p ->next);q= t->next;if(p == NULL)
l.first = q;else
p ->next=q;if(q==NULL)
l.last = p;else
q->prev = p;delete t;
}//HAM SAO CHEP DLISTvoid copy_dlist(dlist a,dlist &b){
nodeptr pa=a.last;createdlist(b);while(pa!=NULL){
insertdlist(b,pa->data,NULL);pa=pa->prev;
}}//HAM XOA TAT CA CAC PHAN TUvoid deleteall(dlist &l){
while(l.first!=NULL)deletedlist(l,NULL);
}DLIST QUANG
#include<iostream.h>#include<string.h>#include<stdio.h>
typedef struct nodet{elem data;struct nodet *next,*prev;
}node;
typedef node *nodeptr;
typedef struct{nodeptr first, last;
}dlist;
void createdlist (dlist &l){
l.first= l.last = NULL;}
int emptydlist(dlist l){
return (l.first == NULL);}
void insertdlist(dlist &l, elem &x,nodeptr p){
nodeptr q,newp;newp= new node;memcpy(&newp->data, &x,sizeof(elem));q=(p == NULL ? l.first : p ->next);newp->next = q;if(p == NULL)
l.first=newp;else
p ->next = newp;newp ->prev = p;if(q==NULL)
l.last = newp;else
q->prev=newp;}
void deletedlist(dlist &l, nodeptr p){
nodeptr q,t;t=(p == NULL ? l.first : p ->next);q= t->next;if(p == NULL)
l.first = q;else
p ->next=q;if(q==NULL)
l.last = p;else
q->prev = p;delete t;
}
#include<iostream.h>
Trang 13
#include<stdio.h>typedef int elem;#include"DLIST.cpp"
//HAM NHAPvoid nhap(dlist &a){
char s; int sa;cout<<"\nNhap vao so ";cout.flush(); //loai bo getcharcreatedlist(a);while((s=getchar())!='\n'){
sa=s-'0';insertdlist(a,sa,a.last);
}}//HAM XUATvoid xuat(dlist &a){
nodeptr p;p=a.first;while(p!=NULL){
cout<<p->data;p=p->next; //tro toi phan tu
tiep theo cua list}
}//HAM CONG SO LONvoid cong(dlist a,dlist b,dlist &c){
nodeptr pa,pb;int x,nho=0;createdlist(c);pa=a.last;pb=b.last;while(pa!=NULL && pb!=NULL){
nho=nho + pa ->data +pb ->data;x=nho%10;insertdlist(c,x,NULL);nho=nho/10;pa= pa ->prev;pb= pb ->prev;
}while(pa!=NULL){
nho=nho+pa ->data;x=nho%10;insertdlist(c,x,NULL);nho=nho/10;pa= pa ->prev;
}while(pb!=NULL){
nho=nho+pb ->data;x=nho%10;insertdlist(c,x,NULL);nho=nho/10;
pb=pb->prev;}if(nho>0)
insertdlist(c,nho,NULL);}
//HAM NHAN SO LON VOI SO Nvoid nhan(dlist a,dlist &b,int n){
createdlist(b);nodeptr pa;int x,nho=0;pa=a.last;while(pa!=NULL){
nho=nho+pa ->data * n;x=nho%10;insertdlist(b,x,NULL);nho=nho/10;pa= pa ->prev;
}if(nho>0)
insertdlist(b,nho,NULL);}//HAM NHAN HAI SO LON VOI NHAUvoid nhan2(dlist a,dlist b,dlist &c){
dlist tam; int d=-1,i,khong=0;nodeptr pa,pb;createdlist(c);pa=a.last; pb=b.last;while(pb!=NULL){
createdlist(tam);for(++d,i=0;i<d;i++)
insertdlist(tam,khong,NULL);nhan(a,tam,pb->data);cong(tam,c,c);pb= pb ->prev;
}}//HAM TINH GIAI THUA CUA MOT SO LONvoid tinhgiaithua(dlist &a,int N){
int so=1,i=N;createdlist(a);if(N<2)
insertdlist(a,so,NULL);else{
while(i>0){
so=i%10;insertdlist(a,so,NULL);i=i/10;
}for(i=N-1;i>1;i--)
nhan(a,a,i);}
Trang 14
}
void main(){
int n; dlist a,b,c;nhap(a); nhap(b);cout<<"Sau khi cong lai la\n";cong(a,b,c); xuat(c);cout<<"\nBAN NHAP VAO SO A\n";nhap(a);cout<<"\nBan nhap vao so N "; cin>>n;cout<<"\nSau khi nhan A voi so N\n";nhan(a,b,n); xuat(b);nhap(a); nhap(b);cout<<"Sau khi nhan lai la\n";nhan2(a,b,c); xuat(c);cout<<"Ban nhap vao mot so de tinh giai
thua ";cin>>n; tinhgiaithua(a,n); xuat(a);cout<<"\n";
}--sf—OANH#include<string.h>#include<iomanip.h>typedef struct nodet{
elem data;
struct nodet *next,*prev;}node;
typedef node *nodeptr;typedef struct{
nodeptr first,last;}dlist;
void createdlist(dlist &l){
l.first=l.last=NULL;}int emptydlist(dlist l){
return l.first==NULL;}
void nhapdlist(dlist &l){
nodeptr p,newp;int x;p=l.first=l.last=new node;do{
cin>>x;if(x>=0){
newp=new node;memcpy(&newp-
>data,&x,sizeof(elem));newp->next=NULL;l.first->next=newp;newp->prev=l.first;l.last=newp;l.first=l.first->next;
}}while(x>=0);l.first=p->next;
delete p;l.first->prev=NULL;
}
void xuatdlist(dlist l){
while(!(emptydlist(l))){
cout<<l.first->data<<setw(2);l.first=l.first->next;
}}
void insertdlist(dlist &l,elem &x,nodeptr p){
nodeptr q,newp;newp=new node;memcpy(&newp->data,&x,sizeof(elem));q=(p==NULL?l.first:p->next);newp->next=q;if(p==NULL)
l.first=newp;else
p->next=newp;newp->prev=p;if(q==NULL)
l.last=newp;else
q->prev=newp;}
void deletedlist(dlist &l,nodeptr p){
nodeptr t,q;t=(p==NULL?l.first:p->next);q=t->next;if(p==NULL)
l.first=q;else
p->next=q;if(q==NULL)
l.last=p;else
q->prev=p;delete t;
}
void saochep(dlist a,dlist &b)
Trang 15
{nodeptr pa=a.last;createdlist(b);while(pa!=NULL){
insertdlist(b,pa->data,NULL);pa=pa->prev;
}}void cong(dlist a,dlist b,dlist &c){
nodeptr pa,pb;int x;int nho=0;createdlist(c);pa=a.last;pb=b.last;while(pa!=NULL&&pb!=NULL){
nho=nho+(pa->data)+(pb->data);x=nho%10;insertdlist(c,x,NULL);nho=nho/10;pa=pa->prev;pb=pb->prev;
}while(pa!=NULL){
nho=nho+(pa->data);x=nho%10;insertdlist(c,x,NULL);nho=nho/10;pa=pa->prev;
}while(pb!=NULL){
nho=nho+(pb->data);x=nho%10;insertdlist(c,x,NULL);nho=nho/10;pb=pb->prev;
}if(nho>0)
insertdlist(c,nho,NULL);}
void nhan1(dlist a,int n,dlist &b){
nodeptr pa;int x,nho=0;createdlist(b);pa=a.last;while(pa!=NULL){
nho=nho+(pa->data)*n;x=nho%10;insertdlist(b,x,NULL);nho=nho/10;pa=pa->prev;
}
if(nho>0)insertdlist(b,nho,NULL);
}void nhan(dlist a,dlist b, dlist &l){
dlist d,c;nodeptr pb;int x=0,dem=0,t;pb=b.last;createdlist(l);insertdlist(l,x,NULL);while(pb!=NULL){
nhan1(a,pb->data,d);if(dem>0){
t=dem;while(t>0){
insertdlist(d,x,d.last);t--;
}}cong(d,l,c);saochep(c,l);dem++;pb=pb->prev;
}}void deleteall(dlist &l){
while(l.first!=NULL)deletedlist(l,NULL);
}
TREE PHUONG#include<iostream.h>#include<iomanip.h>#include<memory.h>
typedef struct nodet {elem data;struct nodet *left,*right;
}node;typedef node *tree;
//DUYET TIEN TUvoid NLR(tree t){
if(t!=NULL){
cout<<t->data<<" "; //xu ly tNLR(t->left);NLR(t->right);
}}//DUYET TRUNG TUvoid LNR(tree t)
Trang 16
{if(t!=NULL){
LNR(t->left);cout<<t->data<<" "; //xu ly tLNR(t->right);
}}//DUYET HAU TUvoid LRN(tree t){
if(t!=NULL){
LRN(t->left);LRN(t->right);cout<<t->data<<" "; //xu ly t
}}//DEM SO NUTint sonut(tree t){
if(t==NULL)return 0;
return(1+sonut(t->left)+sonut(t->right));}//TAO CAY RONG void MakeNullTree(tree *t){
(*t)=NULL; } //KIEM TRA CAY RONG int EmptyTree(tree t){
return t==NULL; } //XAC DINH CON TRAI CUA MOT NUTtree trai(tree t){
if (t!=NULL)return t->left;
return NULL; } //XAC DINH CON PHAI CUA MOT NUTtree phai(tree t){
if (t!=NULL)return t->right;
return NULL; } //KIEM TRA NUT LAint la(tree t){
if(t!=NULL)return(t->left==NULL)&&(t-
>right==NULL); return NULL;
} //DEM SO LAint sola(tree t){
if(t==NULL)return 0;
return(la(t)+sola(t->left)+sola(t->right));}//KIEM TRA NUT MOT CONint nut_1con(tree t){
if(t!=NULL)if(t->left==NULL)
return (t->right!=NULL);else
return (t->right==NULL);return NULL;
}//DEM SO NUT MOT CONint dem_1con(tree t){
if(t==NULL)return 0;
return nut_1con(t)+dem_1con(t->left)+dem_1con(t->right);}//KIEM TRA NUT HAI CONint nut_2con(tree t){
if(t!=NULL)return(t->left!=NULL)&&(t->right!
=NULL); return NULL;
}//DEM SO NUT HAI CONint dem_2con(tree t){
if(t==NULL)return 0;
return nut_2con(t)+dem_2con(t->left)+dem_2con(t->right);}//HAM DEM SO NUT TREN MUC Kint muck(tree t,int k){
if(t==NULL) return 0;if(k>1)
return muck(t->left,k-1)+muck(t->right,k-1);
return 1;}//HAM DEM SO NUT TREN MUC LE (cho m=1) CHAN (m=0)int dem_le(tree t,int m){
if(t==NULL) return 0;return m%2+dem_le(t->left,m+1)+dem_le(t-
>right,m+1);}//HAM DEM SO NUT TRONG (khong phai la)int dem_nuttrong(tree t){
if(t==NULL) return 0;if(t->left==NULL && t->right==NULL)
Trang 17
return 0;return 1+dem_nuttrong(t->left)
+dem_nuttrong(t->right);}//HAM DEM SO NUT CO GIA TRI BANG Xint dem_x(tree t,int x){
if(t==NULL) return 0;if(t->data==x)
return 1+dem_x(t->left,x)+dem_x(t->right,x);
return dem_x(t->left,x)+dem_x(t->right,x);}//HAM TINH CHIEU CAO CAYint max(int a,int b){
if(a>b) return a;else return b;
}int h(tree t){
if(t==NULL) return 0;return 1+ max(h(t->left),h(t->right));
}//HAM TINH CHIEU RONG CUA CAY (Muc co nhieu nut nhat)int w(tree t){
int hight=h(t),wight=0,k,i;for(i=1;i<=hight;i++){
k=muck(t,i);if(k>wight) wight=k;
}return wight;
}//HAM NHAP CAY THEO TIEN TU (Muc co nhieu nut nhat)void nhap(tree &t){
elem x;cout<<"Nhap vao gia tri: ";cin>>x;if(x>0){
t=new node;t->data=x;nhap(t->left);nhap(t->right);
}else
t=NULL;}//HAM IN RA CAY NHI PHAN (cho m=1)void in_cay(tree t,int m){
if(t!=NULL){
in_cay(t->left,m+1);cout<<"\n"<<setw(4*m)<<t->data;
in_cay(t->right,m+1);}
}//HAM TINH TONG CAC NUT TREN CAYint tong_nut(tree t){
if(t==NULL)return 0;
return t->data+tong_nut(t->left)+tong_nut(t->right);}//HAM XOA CA CAYvoid xoacay(tree &t){
if(t!=NULL){
xoacay(t->left);xoacay(t->right);delete t;t=NULL;
}}//HAM XOA CAC NUT LA LAvoid xoa_la(tree &t){
if(t!=NULL)if(la(t)==1){
delete t;t=NULL;
}else{
xoa_la(t->left);xoa_la(t->right);
}}//HAM TIM MIN CUA CAY //HAM TIM MIN CUA 3 GIA TRIint min3(int a,int b,int c){
int min=a;if(b<min) min=b;if(c<min) min=c;return min;
}//HAM TIM MIN CUA 2 GIA TRIint min2(int a,int b){
if(a<b) return a;return b;
}int min_tree(tree t){
if(t!=NULL){
if(t->left==NULL && t->right==NULL) return t->data;
if(t->left==NULL) return min2(t->data,min_tree(t->right));
Trang 18
if(t->right==NULL) return min2(t->data,min_tree(t->left));
return min3(t->data,min_tree(t->left),min_tree(t->right));
}return 0;
}//HAM TIM MAX CUA CAY //HAM TIM MAX CUA 3 GIA TRIint max3(int a,int b,int c){
int max=a;if(b>max) max=b;if(c>max) max=c;return max;
}int max_tree(tree t){
if(t==NULL) return 0;return max3(t->data,max_tree(t-
>left),max_tree(t->right));}//HAM THEM MOT NUT VAO TREE (Neu cay rong them vao goc//nguoc lai them vao cay con thap hon trong 2 cay con cua goc//Neu 2 cay con bang nhau them vao cay ben traivoid chen_tree(tree &t,elem x){
if(t==NULL){
t=new node;t->data=x;t->left=t->right=NULL;
}else{
tree newx=new node;newx->data=x;if(h(t->left)<=h(t->right)){
newx->left=t->left;newx->right=NULL;t->left=newx;
}else{
newx->right=t->right;newx->left=NULL;t->right=newx;
}}
}//HAM HOAN DOI TAT CA CAY CON BEN TRAI VA CAY CON BEN PHAI TUONG UNG//Muon hoan doi cay t1 goi ham hoandoi_tree(t1,&t1)void hoandoi_tree(tree t1,tree &t2)
{if(t1==NULL)
t2=NULL;else{
t2=new node;t2->data=t1->data;hoandoi_tree(t1->left,t2->right);hoandoi_tree(t1->right,t2->left);
}}//HAM CHEP CAYvoid chepcay(tree t1,tree &t2){
if(t1==NULL)t2=NULL;
else{
t2=new node;t2->data=t1->data;chepcay(t1->left,t2->left);chepcay(t1->right,t2->right);
}}
CAY NHI PHAN BST//HAM CHEN X VAO CUOI CAY BSTvoid inserttree(tree &t,elem x){
if(t==NULL){
t=new node;t->data=x;t->left=t->right=NULL;
}else
if(x<t->data)inserttree(t->left,x);
elseif(x>t->data)
inserttree(t->right,x);}//HAM TIM BIA PHAIvoid del(tree &r,tree &q){
if(r->right!=NULL)del(r->right,q);
else{
q->data=r->data;q=r;r=r->left;
}}//HAM XOA 1 PHAN TUvoid deletetree(tree &t,elem x){
if(t!=NULL)if(x<t->data)
deletetree(t->left,x);
Trang 19
elseif(x>t->data)
deletetree(t->right,x);
else{
tree q=t;if(t->right==NULL)
t=t->left;else
if(t->right == NULL)
t=t->right;else
del(t->left,q);delete q;
}}//HAM TIM X TREN CAY NHI PHANtree searchtree(tree t,elem x){
if(t==NULL)return NULL;
elseif(x<t->data)
return searchtree(t->left,x);else
return searchtree(t->right,x);}
#include <iostream.h>typedef int elem;#include "TREE.cpp"
void main(){
tree t; int k;cout<<"\nNhap vao cay theo cach duyet
tien tu\n"; nhap(t);cout<<"IN RA CAY NHI PHAN\n";in_cay(t,1);cout<<"\nXuat cay theo cach duyet tien tu\
n"; NLR(t);cout<<"\nXuat cay theo cach duyet trung
tu\n"; LNR(t);cout<<"\nXuat cay theo cach duyet hau tu\
n"; LRN(t);cout<<"\nSo nut cua cay la :"<<sonut(t);cout<<"\nSo la cua cay la :"<<sola(t);cout<<"\nSo nut 1 con cua cay
la :"<<dem_1con(t);cout<<"\nSo nut 2 con cua cay
la :"<<dem_2con(t);cout<<"\nChieu cao cua cay la "<<h(t);cout<<"\nDem so nut tren muc k ";cin>>k;
cout<<"So nut tren muc "<<k<<" la "<<muck(t,k);
cout<<"\nTong cac gia tri cac nut tren cay la "<<tong_nut(t);
cout<<"\nDem cac nut co tren muc le "<<dem_le(t,1);
cout<<"\nDem cac nut co tren muc chan "<<dem_le(t,0);
cout<<"\nDem cac nut trong (khong la la) "<<dem_nuttrong(t);
cout<<"\nDem so nut co gia tri bang x ";cin>>k;cout<<"So nut co gia tri bang "<<k<<" la
"<<dem_x(t,k);cout<<"\nMax cua cay la "<<max_tree(t);cout<<"\nMin cua cay la "<<min_tree(t);cout<<"\nIN RA CAY NHI PHAN DA XOA
HET CAC LA\n"; xoa_la(t); in_cay(t,1);cout<<"\nThem x vao cay "; cin>>k;cout<<"\nChen vao nhanh co chieu cao
nho hon\nChen vao nhanh trai neu 2
nhanh bang nhau"; chen_tree(t,k);cout<<"\nIN RA CAY NHI PHAN DA
CHEN\n"; in_cay(t,1);cout<<"\n";
}***NANG CAO****#include <iostream.h>#include <string.h>#include <ctype.h>#include<stdlib.h>typedef char elem[4];#include "TREE.cpp"
void nhap_toan(tree &t){
elem x;cout<<"Nhap vao gia tri: ";cin.getline(x,4);if(x[0]!='0'){
t=new node;strcpy(t->data,x);nhap_toan(t->left);nhap_toan(t->right);
}else
t=NULL;}//HAM IN BIEU THUC TOAN CAY NHI PHAN (cho m=1)void in_cay(tree t,int m){
if(t!=NULL){
in_cay(t->left,m+1);cout<<"\n"<<setw(4*m)<<t->data;in_cay(t->right,m+1);
}
Trang 20
}//HAM TINH GIA TRI CUA BIEU THUCint tinh(tree t){
if(t!=NULL){
if(t->data[0]<'0'){
if(t->data[0]=='+')return tinh(t->left)
+tinh(t->right);if(t->data[0]=='-')
return tinh(t->left)-tinh(t->right);
if(t->data[0]=='*')return tinh(t-
>left)*tinh(t->right);if(t->data[0]=='/')
return tinh(t->left)/tinh(t->right);
}else
return atoi(t->data);}return 0;
}
void main(){
tree t; cout<<"\nNhap vao cay theo cach duyet
tien tu\n"; nhap_toan(t);cout<<"IN RA CAY NHI PHAN\n";in_cay(t,1);cout<<"\nGIA TRI CUA BIEU THUC LA
"<<tinh(t);cout<<"\n";
}
****Ôn tập***DANH SACH LIEN KET DON#include <iostream.h>typedef int elem;#include "LIST.cpp"
//HAM NHAP MANG SO DUONG BANG LIST THEO THU TU BAN PHIMvoid nhapdem(list &l){
creatlist(l);list p; int x;p=l=new node; //pt demdo{
cout<<"Nhap vao phan tu ";cin>>x;
if(x>0){
l->next=new node;l=l->next;
l->data=x;}
}while(x>0);l->next=NULL;l=p->next;delete p;
}//HAM XUAT MANG SO BANG LISTvoid xuat(list l){
while(l!=NULL){
cout<<l->data<<" ";l=l->next; //tro toi phan tu
tiep theo cua list}
}//HAM CHEN PHAN TU VAO CUOI DANH SACHvoid chen_cuoi(list &l,elem &x){
list p=l;while(p!=NULL && p->next!=NULL)
p=p->next;insertlist(l,x,p);
}//HAM SO SANHint ss(elem a,elem b){
return a-b;}//HAM DEM SO PHAN TU BANG LISTint dem_x(list l,int x){
if(l==NULL) return 0;int dem=0;do{
if(l->data==x)++dem;
l=l->next;}while(l!=NULL);return dem;
}//HAM SAO CHEP DANH SACHvoid copy_list(list l1,list &l2){
creatlist(l2);list p;p=l2=new node;while(l1!=NULL){
l2->next=new node;l2=l2->next;l2->data=l1->data;l1=l1->next;
}l2->next=NULL;l2=p->next;delete p;
}
Trang 21
//HAM HUY TOAN BO DANH SACHvoid huy(list l){
while(l!=NULL)deletelist(l,NULL);
}//HAM CHEN SO 0 VAO SAU CAC PHAN TU CHANvoid chen0(list &l){
list p=l; int khong=0;while(p!=NULL){
if(p->data%2==0){
insertlist(l,khong,p);p=p->next;
}p=p->next;
}}//HAM CHEN X VAO LIST TANGvoid chen_tang(list &l,elem x){
list p,q;int chen=0;//p sau q 1 buocp=NULL; q=l;while(chen==0 && q!=NULL){
if(x<=q->data){
insertlist(l,x,p);chen=1;
}p=q; q=q->next;
}if(chen==0)
insertlist(l,x,p);}//XOA TAT CA CAC PHAN TU BANG Xvoid xoa_x(list &l,elem x){
list p,q;//p sau q 1 buocp=NULL; q=l;while(q!=NULL)
if(x==q->data){
q=q->next;deletelist(l,p);
}else{
p=q;q=q->next;
}}//DOI M PHAN TU CUOI CUA DANH SACH LEN DAU// Nhap 1 2 3 4 5 nhap m=2 Kq 4 5 1 2 3
void doi_cuoi(list &l,int m){
list p,q;p=q=l;//Cho q chay len cach p m nodefor(;q && m>0;m--)
q=q->next;if(q) //q!=NULL{
//Cho q chay den cuoifor(;q->next;q=q->next,p=p-
>next);//Luc nay q=5 p=3 q->next=l;l=p->next;p->next=NULL;
}}
void main(){
list l,p,l1; int x;creatlist(l);nhapdem(l); xuat(l);/*cout<<"\nNhap vao so x "; cin>>x;cout<<"Kiem tra x co trong danh sach
khong";if(searchlist(l,x,p,ss)==NULL)
cout<<"\nKhong co";else
cout<<"\nx co trong danh sach";cout<<"\nSo phan tu bang x trong danh
sach "<<dem_x(l,x);cout<<"\nChen phan tu x vao cuoi danh
sach\n";chen_cuoi(l,x); xuat(l);cout<<"\nXoa tat ca cac phan tu bang x\
n";xoa_x(l,x); xuat(l);cout<<"\nSao chep list l1 vao list l2\n";copy_list(l,l1); xuat(l);cout<<"\nChen so 0 vao sau cac ptu
chan\n";chen0(l); xuat(l);cout<<"\nChen so x list tang sao cho
bao dam tinh tang dan\n";cout<<"Nhap vao list tang\n";nhapdem(l); xuat(l);cout<<"\nNhap vao so x "; cin>>x;chen_tang(l,x);xuat(l);*/cout<<"\nNhap vao m de doi m phan tu
cuoi ra dau\n"; cin>>x;doi_cuoi(l,x); xuat(l);
cout<<"\n";}******DANH SACH LIEN KET DOI****#include <iostream.h>typedef int elem;#include "DLIST.cpp"
Trang 22
//HAM SO VAO DLIST TU LAMvoid nhap_dlist2(dlist &l){
nodeptr p,newp; int x;p=l.first=new node; //Tao phan tu demdo{
cout<<"Nhap vao "; cin>>x;if(x>0){
newp=new node;p->next=newp;newp->prev=p;p=p->next;p->data=x;
}}while(x>0);p->next=NULL; //pt cuoi tro toi NULLl.last=p; //con tro last =pt cuoip=l.first; //cho p tro toi ptu deml.first=l.first->next; //Bo ptu deml.first->prev=NULL; //ptu dau
tro ve NULLdelete p; //Xoa ptu dem
}//HAM NHAP BANG INSERTDLISTvoid nhap_dlist(dlist &l){
int x; cout<<"Nhap vao "; cin>>x;while(x>0){
insertdlist(l,x,l.last);cout<<"Nhap vao "; cin>>x;
}}//XUAT DLISTvoid xuat_dlist(dlist l){
while(!emptydlist(l)){
cout<<l.first->data<<" ";l.first=l.first->next;
}}//HAM DAO NGUOC DLIST L1 VAO L2//muon dao list l1 dao_dlist(l1,l1)void dao_dlist(dlist l1,dlist &l2){
createdlist(l2);while(l1.last!=NULL){
insertdlist(l2,l1.last->data,l2.last);l1.last=l1.last->prev;
}}//HAM DEM SO PHAN TU CUA DANH SACHint dem_dlist(dlist l){
int dem=0;while(!emptydlist(l))
{dem++;l.first=l.first->next;
}return dem;
}//HAM TIM MAX CUA DLIST (tra ve con tro den ptu lon nhat)nodeptr max_dlist(dlist l){
nodeptr m=l.first;while(l.first!=NULL){
if(l.first->data>m->data)m=l.first;
l.first=l.first->next;}return m;
}//HAM SAP XEP TANG CAC PHAN TU CUA DANH SACH L1 TRA VE L2//Neu muon SXDS l1 ta goi sapxep_dlist(l1,l1)void sapxep_dlist(dlist l1,dlist &l2){
createdlist(l2); nodeptr m;while(!emptydlist(l1)){
m=max_dlist(l1);insertdlist(l2,m->data,NULL);deletedlist(l1,m->prev);
}}/*Mot so luu y ve han chen va deleteinsert(NULL) chen dauinsert(l.last) chen cuoiinsert(l.frist) chen sau pt thu 1insert(p) chen sau pt pDe doi bai tren thanh sx giam ta thayinsertdlist(l2,m->data,l.last);*/
//HAM TACH CHAN LE DANH SACH DOIvoid tach_chanle(dlist l,dlist &l1,dlist &l2){
createdlist(l1); createdlist(l2);//Tao phan tu deminsertdlist(l2,l.first->data,NULL);insertdlist(l1,l.first->data,NULL);//Chep cac so chan le vao cuoi l2,l1while(l.first!=NULL){
if(l.first->data%2==0)insertdlist(l2,l.first-
>data,l2.last);else
insertdlist(l1,l.first->data,l1.last);
l.first=l.first->next;}//Xoa phan tu dem o daudeletedlist(l1,NULL);
Trang 23
deletedlist(l2,NULL);}
//HAM LAY RA MOT DANH SACH CON TANGvoid con_dlist(dlist &l,dlist &l1){
createdlist(l1); elem x;if(l.first!=NULL)
x=l.first->data;while(l.first!=NULL && x<=l.first->data){
x=l.first->data;insertdlist(l1,x,l1.last);deletedlist(l,NULL);
}}//HAM TRON 2 MANG TANG THANH 1 MANG TANG TRA CON TRO VE CUOIvoid merge(dlist &l,dlist l1, dlist l2){
createdlist(l);nodeptr p=l.first=new node;while( l1.first!= NULL && l2.first!=NULL){
if(l1.first->data < l2.first->data){
p->next=l1.first;l1.first->prev=p;l1.first=l1.first->next;
}else{
p->next=l2.first;l2.first->prev=p;l2.first=l2.first->next;
}p=p->next;
}//Gan phan con lai vao lif(l1.first!=NULL){ p->next=l1.first;
l1.first->prev=p;} else {
p->next=l2.first;l2.first->prev=p;
}//Tro toi phan tu cuoiwhile(p->next!=NULL)
p=p->next;//Xu ly phan cuoil.last=p; //con tro last =pt cuoip=l.first; //cho p tro toi ptu deml.first=l.first->next; //Bo ptu deml.first->prev=NULL; //ptu dau
tro ve NULLdelete p; //Xoa ptu dem
}
void main(){
dlist l,l1,l2; nodeptr tam;createdlist(l); createdlist(l1);createdlist(l2);/*nhap_dlist(l); xuat_dlist(l);cout<<"\nCopy vao DSLK khac\n";copy_dlist(l,l1); xuat_dlist(l1);cout<<"\nDao nguoc danh sach\n";dao_dlist(l,l); xuat_dlist(l);cout<<"\nSo phan tu cua danh sach la
"<<dem_dlist(l);cout<<"\nPhan tu lon nhat cua danh sach
la :"<<max_dlist(l)->data;sapxep_dlist(l,l);cout<<"\nSap xep lai danh sach theo thu tu
tang\n"; xuat_dlist(l);cout<<"\nTach thanh 2 danh sach chan le\
n";tach_chanle(l,l1,l2);cout<<"\nDANH SACH LE\n";xuat_dlist(l1);cout<<"\nDANH SACH CHAN\n";xuat_dlist(l2);cout<<"\nLAY RA DANH SACH CON
TANG\n"; con_dlist(l,l1);cout<<"\nDanh sach me\n";xuat_dlist(l);cout<<"\nDanh sach con\n";xuat_dlist(l1);*/cout<<"\nNHAP VAO HAI DANH SACH
TANG DE TRON LAI\n";cout<<"\nNhap vao DS thu 1\n";
nhap_dlist(l1); xuat_dlist(l1);cout<<"\nNhap vao DS thu 2\n";
nhap_dlist(l2); xuat_dlist(l2);cout<<"\nTron 2 danh sach thanh 1\n";
merge(l,l1,l2); xuat_dlist(l);cout<<"\n";
}*******GIAI DE THI NAM 2007 - 2008**********Câu 1#include <iostream.h>typedef struct {
int msmh;int sl;double tt;
}elem;#include "LIST.cpp"
//HAM NHAP THONG TINvoid nhapdem(list &l){
list p; int x;p=l=new node; //pt demdo{
cout<<"\nNhap vao ma so mat hang "; cin>>x;
if(x>0){
l->next=new node;
Trang 24
l=l->next;l->data.msmh=x;cout<<"Nhap vao so luong
"; cin>>l->data.sl;cout<<"Nhap vao thanh
tien "; cin>>l->data.tt;}
}while(x>0);l->next=NULL;l=p->next;delete p;
}//HAM IN RA GIA TRI TRUNG BINH CONGdouble tb_gia(list l){
double T=0; int dem=0;while(l!=NULL){
dem++;T=T + l->data.tt / l->data.sl;l=l->next;
}return T/dem;
}//HAM TAO RA MOT DANH SACH LIEN KET TANG NGHIEM NGATvoid ghep(list &l,list l1, list l2){
list p;l=p=new node;while( l1!= NULL && l2!=NULL)
if(l1->data.msmh == l2->data.msmh)
{l->next=new node;l=l->next;l->data.msmh=l1-
>data.msmh;l->data.sl=l1->data.sl + l2-
>data.sl;l->data.tt=l1->data.tt + l2-
>data.tt;l1=l1->next;l2=l2->next;
}else{
if(l1->data.msmh < l2->data.msmh)
{ l->next=l1;l1=l1->next;
} else {l->next=l2;l2=l2->next;
} l = l->next;}
if(l1!=NULL)l->next=l1;
elsel->next=l2;
while(l->next!=NULL)l=l->next;
l=p->next;delete p;
}
//HAM XUAT DU LIEUvoid xuat(list l){
while(l!=NULL){
cout<<"\n\nMa so mat hang "<<l->data.msmh;
cout<<"\nSo luong "<<l->data.sl;cout<<"\nThanh tien "<<l->data.tt;l=l->next; //tro toi phan tu
tiep theo cua list}
}void main(){
list l1,l2,l;creatlist(l1); creatlist(l2); creatlist(l);cout<<"\nNhap vao l1\n";nhapdem(l1); xuat(l1);cout<<"\nNhap vao l2\n";nhapdem(l2); xuat(l2);cout<<"\nTinh Trung Binh gia cac mat hang
l1 "<<tb_gia(l1);cout<<"\nGhep 2 list thanh 1 list";ghep(l,l1,l2); xuat(l);cout<<"\n";
}#include <iostream.h>#include <string.h>#include <ctype.h>#include<stdlib.h>typedef char elem[4];#include "TREE.cpp"
void nhap_toan(tree &t){
elem x;cout<<"Nhap vao gia tri: ";cin.getline(x,4);if(x[0]!='0'){
t=new node;strcpy(t->data,x);nhap_toan(t->left);nhap_toan(t->right);
}else
t=NULL;}//HAM IN BIEU THUC TOAN CAY NHI PHAN (cho m=1)void in_cay(tree t,int m){
if(t!=NULL)
Trang 25
{in_cay(t->left,m+1);cout<<"\n"<<setw(4*m)<<t->data;in_cay(t->right,m+1);
}}//HAM TINH GIA TRI CUA BIEU THUCint tinh(tree t){
if(t!=NULL){
if(t->data[0]<'0'){
if(t->data[0]=='+')return tinh(t->left)
+tinh(t->right);if(t->data[0]=='-')
return tinh(t->left)-tinh(t->right);
if(t->data[0]=='*')return tinh(t-
>left)*tinh(t->right);if(t->data[0]=='/')
return tinh(t->left)/tinh(t->right);
}else
return atoi(t->data);}return 0;
}//DEM SO LA (DEM SO TOAN HANGint sola(tree t){
if(t==NULL) return 0;if(t->left==NULL && t->right==NULL)
return 1;return sola(t->left)+sola(t->right);
}//KIEM TRA NUT HAI CONint nut_2con(tree t){
if(t!=NULL)return(t->left!=NULL)&&(t->right!
=NULL); return NULL;
}//DEM SO NUT HAI CON (DEM SO PHEP TOAN)int dem_2con(tree t){
if(t==NULL)return 0;
return nut_2con(t)+dem_2con(t->left)+dem_2con(t->right);}
void main(){
tree t;
cout<<"\nNhap vao cay theo cach duyet tien tu\n"; nhap_toan(t);
cout<<"IN RA CAY NHI PHAN\n";in_cay(t,1);cout<<"\nSo Toan Hang cua cay la
"<<sola(t);cout<<"\nSo Bieu Thuc cua cay la
"<<dem_2con(t);cout<<"\nGIA TRI CUA BIEU THUC LA
"<<tinh(t);cout<<"\n";
}--CAY NHI PHAN BST---#include <iostream.h>#include <string.h>#include <ctype.h>#include<stdlib.h>typedef int elem;#include "TREE.cpp"
//20 10 30 5 14 25 40 1 12 18 28 16 0//HAM NHAP CAY BSTvoid nhap_BST(tree &t){
int x;do{
cin>>x;if(x>0)
inserttree(t,x);}while(x>0);
}//HAM DUYET BFS (DUYET THEO TUNG MUC)//ham in cac nut o muc kvoid in_muck(tree t,int k,int i=1){
if(t)if(i==k)
cout<<setw(4)<<t->data;else{
in_muck(t->left,k,i+1);in_muck(t->right,k,i+1);
}}void duyet_BFS(tree t){
int i,cao=h(t);for(i=1;i<=cao;i++){
in_muck(t,i);cout<<"\n";
}}//TIM MUC CUA NODE CHUA SO X CHO TRUOCint tim_mucx(tree t,int x,int m) //m=1{
if(t==NULL) return 0;if(t->data==x) return m;if(t->data>x) //Goc lon hon nhanh trai
Trang 26
tim_mucx(t->left,x,m+1);else
tim_mucx(t->right,x,m+1);}
void main(){
int x,i,m=1; tree t=NULL; cout<<"Nhap vao cay theo cach duyet tien
tu\n"; nhap_BST(t);cout<<"\nIN RA CAY NHI PHAN\n";in_cay(t);cout<<"\nXuat cay tang dan\n"; LNR(t);cout<<"\nXuat cay giam dan\n"; RNL(t);cout<<"\nDuyet cay BFS\n";duyet_BFS(t);cout<<"\nNhap vao so x ban can tim ";cin>>x;m=tim_mucx(t,x,1);if(m)
cout<<"\nSo "<<x<<" dang o muc "<<m;
elsecout<<"\nKhong tim thay "<<x;
cout<<"\n";}
Trang 27