Вы находитесь на странице: 1из 27

TAP HOP elemQ e[MAXQ];

#define M (N/8+1) int front, rear;


typedef unsigned char taphop[M]; }queue;
void taprong(taphop s)
{ void createqueue(queue &q)
int i; {
for(i=0;i<M;i++) q.front=q.rear=0;
s[i]=0; }
} int emptyqueue(queue q)
void thempt(taphop s,int x) {
{ return(q.front==q.rear);
s[x/8]=s[x/8]|(1<<x%8); }
} void addqueue(queue &q, elemQ &x)
void loaipt(taphop s,int x) {
{ int newr=(q.rear+1)%MAXQ;
unsigned char k=1<<(x%8); if(q.front == newr)
s[x/8]=s[x/8]&~k; exit(0);
} memcpy(&q.e[q.rear],&x,sizeof(elemQ));
int thuocve(taphop s,int x) q.rear=newr;
{ }
return s[x/8]&(1<<x%8); void removequeue(queue &q, elemQ &x)
} {
void hoi(taphop a,taphop b,taphop c) if(emptyqueue(q))
{ exit(0);
int i; memcpy(&x,&q.e[q.front],sizeof(elemQ));
for(i=0;i<M;i++) q.front=(q.front+1)%MAXQ;
c[i]=a[i]|b[i]; }
}
LIST
STACK #include"memory.h"
#include <stdlib.h> typedef struct nodet {
#include <string.h> elem data;
typedef struct { struct nodet *next;
elem e[Max]; }node;
int top; typedef node *list;
} stack; void creatlist(list &l)
void create_stack(stack &s) {
{ l=NULL;
s.top = -1; }
} int emptylist(list &l)
int empty_stack(stack s) {
{ return(l==NULL);
return s.top == -1; }
} void insertlist(list &l,elem &x,list p)
void push(stack &s, elem &x) {
{ list newp= new node;
if(s.top == Max - 1) exit(0);// stdlib.h memcpy(&newp->data,&x,sizeof(elem));
memcpy(&s.e[++s.top], &x, sizeof(elem)); if(p==NULL)
} {
void pop(stack &s, elem &x) newp->next=l;
{ l=newp;
if(s.top == -1) exit(0); }
memcpy(&x, &s.e[s.top--], sizeof(elem)); else
} {
newp->next=p->next;
QUEUE p->next=newp;
#include <stdlib.h> }
#include <string.h> }
void deletelist(list &l,list p)
typedef struct { {

Trang 1
list t; void main()
if(p==NULL) {
{ int i,n,k;
t=l; double can;
l=t->next; taphop s;
} cout<<"Nhap n:";
else cin>>n;
{ can=sqrt(n);
t=p->next; taprong(s);
p->next=t->next; for(i=0;i<=n;i++)
} thempt(s,i);
delete t; i=2;
} while(i<=can)
list searchlist(list l,elem x,list &p,int (*comp) {
(elem,elem)) k=i*i;
{ while(k<=n)
list c; {
c=l; loaipt(s,k);
p=NULL; k=k+i;
while(c!=NULL && comp(x,c->data)!=0) }
{ do
p=c; {
c=c->next; i++;
} }while(!thuocve(s,i));
return c; cout<<"Ket qua la:";
} for (i=2;i<=n;i++)
list searchorderlist(list l,elem x,list &p,int (*comp) if(thuocve(s,i))
(elem,elem)) cout<<setw(4)<<i;
{ }
list c; }
c=l;
p=NULL; STACK
while(c!=NULL && comp(x,c->data)>0) #include <iostream.h>
{ #include <string.h>
p=c; #define Max 32
c=c->next; typedef int elem;
} #include "STACK.CPP"
if(c!=NULL && comp(x,c->data)<0) //VAO SAU RA TRUOC
return NULL;
else //HAM NHAP CAC PHAN TU VAO STACK
return c; void nhap(stack &s)
} {
void getdata(list l,elem &x) int x,n;
{ cout<<"\nNhap vao bao nhieu so ";
memcpy(&x,&l->data,sizeof(elem)); cin>>n;
} cout<<"Nhap vao ";
list skip(list l) for(;n>0;n--)
{ {
return (l->next); cin>>x;
} push(s,x);
BAI TAP CAU TRUC DU LIEU VA GIAI THUAT }
}
TAP HOP //HAM XUAT SO HE 2,8,16 CUA STACK
#include<iostream.h> void xuat(stack &s)
#include<iomanip.h> {
#include<math.h> char h16[16]={'0','1','2','3','4',
#define N 1000 '5','6','7','8','9','A','B','C','D','E','F'};
#include"taphop.cpp" int x;
while(!empty_stack(s))
{

Trang 2
pop(s,x); #include "STACK.CPP"
cout<<h16[x]<<" ";
} //HAM DAO CHUOI BANG STACK
} void daotu(stack &s1,char N[])
//DOI TU HE 10 SANG HE 2,8,16 {
void he10sang(stack &s,int N,int he) int i; char k;
{ for(i=strlen(N)-1;i>-2;i--)
int x; if(N[i]!=' ' && i!=-1)
while(N>0) push(s1,N[i]);
{ else
x=N%he; {
push(s,x); while (!empty_stack(s1))
N=N/he; {
} pop(s1,k);
} cout<<k;
//HAM CHUYEN TU HE 2,8,16 SANG HE 10 }
int sang10(stack &s,int he) if(i!=-1)
{ cout<<" ";
int T=0; int mu=1,x; }
while(!empty_stack(s)) }
{
pop(s,x); void main()
T=T+x*mu; {
mu=mu*he; char N[100]; stack s1;
} create_stack(s1);
return T; cout<<"Nhap vao chuoi ki tu ";
} cin.getline(N,100);
daotu(s1,N);
cout<<"\n";
}
void main()
{ HAM TINH TOAN MOT BIEU THUC
int N; stack s; #include <iostream.h>
cout<<"Nhap n: "; cin>>N; #include <string.h>
create_stack(s); #define Max 100
he10sang(s,N,2); typedef char elem[7];
cout<<"\nKet qua he 2 la: "; xuat(s); #include "STACK.CPP"
he10sang(s,N,8);
cout<<"\nKet qua he 8 la: "; xuat(s); //HAM KIEM TRA XEM CO PHAI LA DAU
he10sang(s,N,16); int ktdau(char s)
cout<<"\nKet qua he 16 la: "; xuat(s); {
cout<<"\n\nNhap vao so he 2 doi sang he if(s=='+' || s=='-'|| s=='*'|| s=='/' || s==')' ||
10"; nhap(s); s=='(')
cout<<"Chuyen he 2 sang he 10 return 1;
"<<sang10(s,2); return 0;
cout<<"\n\nNhap vao so he 8 doi sang he }
10"; nhap(s); //KIEM TRA DO UU TIEN DAU
cout<<"Chuyen he 8 sang he 10 int uutien(char s1)
"<<sang10(s,8); {
cout<<"\n\nNhap vao so he 16 doi sang he switch(s1)
10"; nhap(s); {
cout<<"Chuyen he 16 sang he 10 case'*':case'/':return 2;
"<<sang10(s,16); case'+':case'-':return 1;
cout<<"\n"; }
} return 0;
}
#include <iostream.h> //HAM TACH CHUOI VAO STACK
#include <string.h> void tach( char *s, stack &s1)
#define Max 100 {
typedef char elem; 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) pop(dau,old);
{
tu[0]=s[i]; while(old[0]!='(')
tu[1]='\0'; {
push(s1,tu);
}
else push(s1,old);
{
tu[k++]=s[i]; pop(dau,old);
tu[k]='\0'; }
if(ktdau(s[i+1])==1 || }
(i+1==d)) else
{ {
push(s1,tu);
k=0; if(empty_stack(dau)==1)
}
} push(dau,moi);
} else
//HAM DAO STACK {
void daostack(stack &s1)
{ kt=1;
char tu[7]; do
stack s2,s3; {
create_stack(s2);
create_stack(s3); pop(dau,old);
while(!empty_stack(s1))
{ if(uutien(old[0])>=uutien(moi[0]))
pop(s1,tu);
push(s2,tu); push(s1,old);
}
while(!empty_stack(s2)) else
{
pop(s2,tu); {
push(s3,tu);
} kt=0;
while(!empty_stack(s3))
{ push(dau,old);
pop(s3,tu);
push(s1,tu); }
} }w
} hile(kt==1 && empty_stack(dau)==0);
//TRUNG TO s2 SANG HAU TO s1
void trung_hau( stack &s1,stack &s2) push(dau,moi);
{ }
char moi[7], old[7]; }
int kt=1; }
stack dau; while(!empty_stack(dau))
create_stack(dau); {
while(!empty_stack(s2)) pop(dau,old);
{ push(s1,old);
pop(s2,moi); }
if(ktdau(moi[0])==0) }
push(s1,moi); //TINH TOAN TU 3 CHUOI
else void tinhtoan(char *s1,char s2[7],char dau[7])
if(moi[0]=='(') {
push(dau,moi); switch(dau[0])
else {
if(moi[0]==')')

Trang 4
case //PHAN TICH MOT SO THANH THUA SO
'+':itoa(atoi(s1)+atoi(s2),s1,10);break; NGUYEN TO 126=2*3*3*7
case '-':itoa(atoi(s1)- void ptnguyento(long n)
atoi(s2),s1,10);break; {
case int i=2;
'*':itoa(atoi(s1)*atoi(s2),s1,10);break; while(n>1)
case {
'/':itoa(atoi(s1)/atoi(s2),s1,10);break; if(n%i==0)
} {
} if(n==i)
//DOI TU HAU TO SANG SO cout<<i;
void hau_so(stack &s1) else
{ cout<<i<<"*";
char t1[7],t2[7],t3[7]; n=n/i;
stack s2; }
create_stack(s2); else
while(!empty_stack(s1)) i++;
{ }
pop(s1,t1); }
if(ktdau(t1[0])==0) //PHAN TICH MOT SO THANH THUA SO
push(s2,t1); NGUYEN TO THEO THU TU NGUOC VA CO MU
else //1500=5^3 * 3 * 2^2
{ void ptnguyento2(long n)
pop(s2,t2); {
pop(s2,t3); int i=2,mu=0; stack s;
tinhtoan(t3,t2,t1); char tu[2];
push(s2,t3); create_stack(s);
} while(n>1)
} {
pop(s2,t1); if(n%i==0)
push(s1,t1); {
} mu++;
void main() n=n/i;
{ if(n%i!=0)
char s[100],tu[7]; {
stack s1,s2; if(mu>1)
create_stack(s1); {
create_stack(s2);
cout<<"Nhap lieu\n"; itoa(mu,tu,10);
cin.getline(s,100); push(s,tu);
tach(s,s2);
daostack(s2); push(s,"^");
trung_hau(s1,s2); }
daostack(s1); itoa(i,tu,10);
hau_so(s1); push(s,tu);
while(!empty_stack(s1)) if(n!=1)
{
pop(s1,tu); push(s,"*");
cout<<tu<<" "; mu=0;
} }
cout<<"\n"; }
} else
i++;
#include <iostream.h> }
#include <string.h> while(!empty_stack(s))
#define Max 32 {
typedef char elem[2]; pop(s,tu );
#include "STACK.CPP" cout<<tu;
}
}

Trang 5
void main() cout<<"\n";
{ }
long n; QUEUE:
cout<<"Nhap vao so n ";cin>>n; SAP XEP DAY SO BANG THUAT GIAI REDIS
ptnguyento(n); cout<<"\n"; SORT
ptnguyento2(n); cout<<"\n"; #include <iostream.h>
} #include<stdlib.h>
#include<time.h>
#include <iostream.h> #include<iomanip.h>
#include <string.h> #define MAXQ 100
#define Max 100 typedef int elemQ;
typedef struct #include "QUEUE.CPP"
{ #define N 70
int i; //VAO TRUOC RA TRUOC
char A,B,C;
}elem; //HAM TAO MANG 1 CHIEU NGAU NHIEN
#include "STACK.CPP" void tao(int *a, int &n)
{
//THAP HA NOI do
void main() {
{ cout<<"Nhap vao so phan tu ";
int n; char A,B,C; stack s; cin>>n;
create_stack(s); }while(n<1);
cout<<"Nhap n "; cin>>n; int *p=a,*q=p+n;
elem bo; srand((int)time(NULL));
bo.i=n; bo.A='A'; for(;p<q;p++)
bo.B='B'; bo.C='C'; *p=rand()%101;
push(s,bo); }
do //HAM XUAT MANG 1 CHIEU
{ void xuat(int *a,int n)
pop(s,bo); {
n=bo.i; int *p=a,*q=p+n;
A=bo.A; for(;p<q;p++)
B=bo.B; cout<<setw(5)<<*p;
C=bo.C; }
if(bo.i==1) //TIM MAX CUA MANG 1 CHIEU
cout<<"\nChuyen tu int Max(int a[],int n)
"<<A<<" sang "<<C; {
else int *p=a,*q=p+n,*k=a;
{ for(;p<q;p++)
bo.i=n-1; if(*p>*k)
bo.A=B; k=p;
bo.B=A; return *k;
bo.C=C; }
push(s,bo); //HOAN VI 2 PHAN TU
void hoanvi(int &a,int &b)
bo.i=1; {
bo.A=A; int tg=a;
bo.B=' '; a=b;
bo.C=C; b=tg;
push(s,bo); }
//HAM SO CHU SO
bo.i=n-1; int SCS(long n)
bo.A=A; {
bo.B=C; int d=1; n=abs(n);
bo.C=B; for(;n>9;d++)
push(s,bo); n=n/10;
return d;
} }
}while(!empty_stack(s));

Trang 6
void main() createqueue(l2);
{ float n=0.0,m=0.0,p=0.0;
int a[N],n,x,i,k,top=0,end=10,lt=1,j; char a[ch];
//Khoi tao 20 hang doi q int d=1,i=0,dem=0,x;
queue q[21]; cout<<"\n hay nhap vao chuoi can doi:";
for(i=0;i<20;i++) cin.getline(a,ch);
createqueue(q[i]); while(a[i]==' '|| a[i]=='-')
//Tao mang ngau nhien {
tao(a,n); if(a[i]=='-')
xuat(a,n); d=-d;
//So chu so cua phan tu lon nhat i++;
k=SCS(Max(a,n))-1; }
//Bo ca so vao hang doi while(a[i]>='0'&&a[i]<='9')
for(i=0;i<n;i++) {
{ int so =a[i]-'0';
j=a[i]%10; addqueue(l1,so);
addqueue(q[j],a[i]); i++;
} }
//Bat dau sap xep i++;
do while(a[i]>='0'&&a[i]<='9')
{ {
lt=lt*10; k--; int so1=a[i]-'0';
for(i=0;i<10;i++) addqueue(l2,so1);
{ dem++;
while(! i++;
emptyqueue(q[i+top])) }
{ while(!emptyqueue(l1))
{
removequeue(q[i],x); removequeue(l1,x);
j=(x/lt)%10; n=n*10+x ;
}
addqueue(q[end+j],x); while(!emptyqueue(l2))
} {
} removequeue(l2,x);
hoanvi(top,end); m=m*10+x;
}while(k>0); }
//Xuat ra day da sap xep m=m/float(pow(10,dem));
cout<<"\nGIAI BANG QUEUE\n"; p=(m+n)*d;
for(i=0;i<20;i++)
{ cout<<p;
while(!emptyqueue(q[i])) }
{ #include <iostream.h>
removequeue(q[i],x); #include<stdlib.h>
cout<<setw(5)<<x; #include<time.h>
} #include<iomanip.h>
} #define MAXQ 20
cout<<"\n"; typedef char elemQ;
} #include "QUEUE.CPP"
//Bai doi chuoi thanh so thuc Dung QUEUE.
#include<iostream.h> //VAO TRUOC RA TRUOC
#include<math.h> //HAM DOI CHUOI THANH SO NGUYEN
#define Max 10 int a_toi(char *s)
#define ch 20 {
typedef int elem; int n=0,d=strlen(s),dau=1;
#include"QUEUE.CPP" char *i=s,x;
void main() queue q; createqueue(q);
{ //Bo chuoi so vao QUEUE
for(;!(48<*i && *i<58);i++)
queue l1,l2; if(*i=='-')
createqueue(l1); addqueue(q,*i);

Trang 7
else addqueue(q1,*i);
return 0; //DOI CHUOI THANH SO
for(;48<*i && *i<58;i++) while(!emptyqueue(q))
addqueue(q,*i); {
//DOI CHUOI THANH SO removequeue(q,x);
while(!emptyqueue(q)) if(x=='-')
{ dau=-dau;
removequeue(q,x); else
if(x=='-') n=n*10+x-'0';
dau=-dau; }
else while(!emptyqueue(q1))
n=n*10+x-'0'; {
} removequeue(q1,x);
return dau*n; f=f*10+x-'0';
} }
//HAM DOI CHUOI THANH SO THUC while(f>1)
long double a_tof(char *s) f=f/10;
{ return dau*(n+f);
long double n=0,f=0; }
int d=strlen(s),dau=1;
char *i=s,x; void main()
queue q,q1; createqueue(q); {
createqueue(q1); char s[20];
//Bo chuoi so vao QUEUE cout<<"\nBan nhap vao chuoi ki tu doi sang
for(;!(48<*i && *i<58);i++) so\n"; cin.getline(s,20);
if(*i=='-') cout<<"\nham a_toi "<<a_toi(s)<<"\natoi
addqueue(q,*i); cua thu vien "<<atoi(s);
else cout<<"\nham a_tof "<<a_tof(s)<<"\natof
return 0; cua thu vien "<<atof(s)<<"\n";
for(;48<*i && *i<58;i++)
addqueue(q,*i); }
if(*i=='.')
for(++i;48<*i && *i<58;i++)

}
LIEN KET DON //HAM XUAT MANG SO BANG LIST
#include <iostream.h> void xuat(list l)
typedef int elem; {
#include "list.cpp" while(l!=NULL)
{
//HAM NHAP MANG SO DUONG BANG LIST cout<<l->data<<" ";
THEO THU TU BAN PHIM l=l->next; //tro toi phan tu
void nhapdem(list &l) tiep theo cua list
{ }
creatlist(l); }
list p; int x; //HAM TACH CHAN LE
p=l=new node; //pt dem void tach_chanle(list l, list &l1, list &l2)
do {
{ creatlist(l1); creatlist(l2);
cout<<"Nhap vao phan tu "; list p1,p2;
cin>>x; l1=p1=new node;
if(x>0) l2=p2=new node;
{ while(l!= NULL)
l->next=new node; {
l=l->next; if(l->data %2 == 0)
l->data=x; {
} insertlist(l2,l->data,p2);
}while(x>0); p2 = p2->next;
l->next=NULL; }
l=p->next; else
delete p;

Trang 8
{ }
insertlist(l1,l->data,p1);
p1=p1->next; //LAY RA MOT DANH SACH CON TANG (TACH
} RA) TRA CON TRO VE DAU LIST
l = l->next; list first(list &l)
} {
p1->next=NULL; if(l==NULL) return NULL;
p2->next=NULL; list p,c;
p1=l1; p2=l2; //Gan p1=l1 de delete p1 p=c=l;
xoa vung dem l=l->next;
l1=l1->next; while(l!=NULL && p->data<=l->data)
l2=l2->next; { p=l;
delete p1; l=l->next;
delete p2; }
} p->next=NULL;
//dung new node de cap phat vung nho khi can return c;
chep gia tri }
//SAP XEP MANG TANG //HAM TRON 2 MANG TANG THANH 1 MANG
void sxtang(list &l) TANG TRA CON TRO VE CUOI
{ list merge(list &l,list l1, list l2)
list p,pm,c,cm,t; {
p=new node; //Tao mot phan tu rac tai list p,c;
dau list l=p=new node;
p->next=l; while( l1!= NULL && l2!=NULL)
l=p; {
t=NULL; if(l1->data < l2->data)
while(l->next!=NULL) {
{ l->next=l1;
pm=p=l; l1=l1->next;
cm=c=l->next; }
while(c!=NULL) else
{ //Tim thang lon I dua ra {
dau list t l->next=l2;
if(cm->data<c->data) l2=l2->next;
{ }
pm=p; l = l->next;
cm=c; }
} if(l1!=NULL)
p=c; l->next=l1;
c=c->next; else
} l->next=l2;
pm->next=cm->next; l1=l2=NULL;
cm->next=t; while(l->next!=NULL)
t=cm; l=l->next;
} c=l;
delete l; l=p->next;
l=t; delete p;
} return c;
//HAM DAO LIST }
void dao(list &l) //HAM SAP XEP BANG PHUONG PHAP TRON
{ MANG
list p=NULL,q; void mergesort(list &l)
while(l!=NULL) {
{ list p,q,r,s,l1,l2;
q=l; int d;
l=q->next; do
q->next=p; {
p=q; d=0;
} p=q=new node;
l=p; while(l!=NULL)

Trang 9
{ void xoa_trung(list &l)
l1=first(l); {
l2=first(l); list p,t;
s=merge(r,l1,l2); t=p=l; t=p->next;
q->next=r; while(t!=NULL)
q=s; if(p->data==t->data)
d++; {
} p->next=t->next;
l=p->next; t=p->next;
delete p; }
}while(d>1); else
} {
p=t;
//CO 2 DS TANG NGHIEM NGAT, dem so pt co t=p->next;
dong thoi trong 2 ds }
int dem(list l1,list l2) }
{
int d=0;
while(l1!=NULL && l2!=NULL)
if(l1->data<l2->data) void main()
l1=l1->next; {
else list l,l1,l2;
if(l1->data>l2->data) creatlist(l); creatlist(l1);
l2=l2->next; creatlist(l2);
else nhapdem(l); xuat(l);
{ cout<<"\nTACH RA THANH 2 DANH
l1=l1->next; SACH CHAN LE\n";
l2=l2->next; tach_chanle(l,l1,l2);
d++; xuat(l1); cout<<"\n"; xuat(l2);
} cout<<"\nSAP XEP TANG LIST\n";
return d; sxtang(l1); xuat(l1); cout<<"\n";
} sxtang(l2); xuat(l2);
//CO 2 DS TANG NGHIEM NGAT dem so pt co cout<<"\nTRON 2 MANG TANG THANH 1
trong ds 1,khong co trong ds2 MANG TANG\n";
int dem2(list l1,list l2) merge(l,l1,l2); xuat(l);
{ cout<<"\nLAY RA MOT DANH SACH
int d=0; CON\n";
while(l1!=NULL && l2!=NULL) nhapdem(l); xuat(l);
if(l1->data<l2->data) cout<<"\nDanh sach con la\n";
{ l1=first(l); xuat(l1); cout<<"\n";
d++; xuat(l); cout<<"\n";
l1=l1->next; cout<<"\nDAO NGUOC LIST\n";
} dao(l); xuat(l);
else cout<<"\nSAP XEP BANG PHUONG PHAP
if(l1->data>l2->data) TRON\n";
l2=l2->next; nhapdem(l); xuat(l);
else cout<<"\nSAU KHI SAP XEP BANG
{ PHUONG PHAP TRON\n";
l1=l1->next; mergesort(l); xuat(l);
l2=l2->next; cout<<"\nNHAP VAO HAI MANG TANG
} TUYET DOI\n";
while(l1!=NULL) creatlist(l1); creatlist(l2);
{ nhapdem(l1); xuat(l1); cout<<"\n";
d++; nhapdem(l2); xuat(l2);
l1=l1->next; cout<<"\nDEM SO PHAN TU CO TRONG
} MANG L1 VA L2 "<<dem(l1,l2);
return d; cout<<"\nDEM SO PHAN TU CO TRONG
} MANG L1 KHONG CO TRONG L2 "<<dem2(l1,l2);
//XOA PHAN TU TRUNG NHAU TRONG LIST cout<<"\nXOA PT TRUNG NHAU TRONG
TANG KHONG NGHIEM NGAT MANG TANG\n";

Trang 10
creatlist(l); nhapdem(l); xuat(l); {
cout<<"\nXOA PHAN TU TRUNG NHAU creatlist(l); list p;
TRONG MANG TANG L1\n"; p=l=new node;
xoa_trung(l); xuat(l); while(l1!=NULL && l2!=NULL)
cout<<"\n"; if(l1->data.mu == l2->data.mu)
} {
if(l1->data.he + l2->data.he
!=0)
{
#include <iostream.h> l->next=new node;
typedef struct l=l->next;
{ l->data.he=l1-
double he; >data.he + l2->data.he;
int mu; l->data.mu=l1-
}elem; >data.mu;
#include "list.cpp" }
l1=l1->next;
//LUU 1 DA THUC TRONG DSLK GIAM DAN l2=l2->next;
THEO SO MU }
void luu_dathuc(list &l) else
{ if(l1->data.mu > l2-
creatlist(l); >data.mu)
list p; double a; int mu; {
p=l=new node; //pt dem l->next=new node;
do l=l->next;
{ l->data.he=l1-
cout<<"\nhe so "; >data.he;
cin>>a; l->data.mu=l1-
cout<<"so mu "; >data.mu;
cin>>mu; l1=l1->next;
if(mu>=0) }
{ else
l->next=new node; {
l=l->next; l->next=new node;
l->data.he=a; l=l->next;
l->data.mu=mu; l->data.he=l2-
} >data.he;
}while(mu>0); l->data.mu=l2-
l->next=NULL; >data.mu;
l=p->next; l2=l2->next;
delete p; }
} l->next=NULL;
//HAM XUAT MANG SO BANG LIST l=p->next;
void xuat_dathuc(list l) delete p;
{ }
while(l!=NULL) //HAM TINH GIA TRI CUA DA THUC
{ double giatri(list l,double x)
if(l->data.mu==0) {
cout<<l->data.he; double kq=l->data.he;
else int d=l->data.mu;
cout<<l->data.he<<"X"<<l- l=l->next;
>data.mu; for(d=d-1;d>=0;d--)
l=l->next; if(l!=NULL && l->data.mu==d)
if(l!=NULL && l->data.he>=0) {
cout<<" + "; kq=kq*x + l->data.he;
} //Khong duoc dao if(l->data.he>=0 && l! l=l->next;
=NULL) }
} else
//HAM CONG HAI 2 DA THUC kq=kq*x;
void cong_dathuc(list &l,list l1,list l2) return kq;

Trang 11
} x=nho%10;
insertdlist(c,x,NULL);
void main() nho=nho/10;
{ pa= pa ->prev;
double x; list l,l1,l2; pb= pb ->prev;
creatlist(l); creatlist(l1); }
creatlist(l2); while(pa!=NULL)
luu_dathuc(l1); xuat_dathuc(l1);cout<<"\n"; {
luu_dathuc(l2); xuat_dathuc(l2);cout<<"\n"; nho=nho+pa ->data;
cong_dathuc(l,l1,l2); xuat_dathuc(l); x=nho%10;
cout<<"\nTinh gia tri cua da thuc vua insertdlist(c,x,NULL);
cong\n"; nho=nho/10;
cout<<"Nhap vao x "; cin>>x; pa= pa ->prev;
cout<<"Gia tri cua da thuc vua cong la }
"<<giatri(l,x)<<"\n";
} while(pb!=NULL)
#include<iostream.h> {
#include<string.h> nho=nho+pb ->data;
#include<stdio.h> x=nho%10;
#include<math.h> insertdlist(c,x,NULL);
#include<stdlib.h> nho=nho/10;
typedef int elem; pb=pb->prev;
#include"dlist1.cpp" }
if(nho>0)
//HAM NHAP insertdlist(c,nho,NULL);
void nhap(dlist &a) }
{
char s; int sa;
cout<<"\nNhap vao so ";
cout.flush();
createdlist(a);
while((s=getchar())!='\n')
{ void main()
sa=s-'0'; {
insertdlist(a,sa,a.last); dlist a,b,c;
} nhap(a);
} nhap(b);
//HAM XUAT cout<<"Sau khi cong lai la\n";
void xuat(dlist &a) cong(a,b,c); xuat(c);
{ cout<<"\n";
nodeptr p;
p=a.first; }
while(p!=NULL) DLIST PHUONG
{ #include<iostream.h>
cout<<p->data; #include<stdio.h>
p=p->next; //tro toi phan tu #include<string.h>
tiep theo cua list #include<iomanip.h>
}
} typedef struct nodet{
//HAM CONG SO LON elem data;
void cong(dlist a,dlist b,dlist &c) struct nodet *next,*prev;
{ }node;
nodeptr pa,pb; typedef node *nodeptr;
int x,nho=0; typedef struct{
createdlist(c); nodeptr first, last;
pa=a.last; }dlist;
pb=b.last;
while(pa!=NULL && pb!=NULL) //KHOI TAO DLIST
{ void createdlist (dlist &l)
nho=nho + pa ->data +pb ->data; {

Trang 12
l.first= l.last = NULL; #include<iostream.h>
} #include<string.h>
//KIEM TRA DLIST RONG #include<stdio.h>
int emptydlist(dlist l)
{ typedef struct nodet{
return (l.first == NULL); elem data;
} struct nodet *next,*prev;
//CHEN VAO DANH SACH LIEN KET DOI }node;
void insertdlist(dlist &l, elem &x,nodeptr p)
{ typedef node *nodeptr;
nodeptr q,newp;
newp= new node; typedef struct{
memcpy(&newp->data, &x,sizeof(elem)); nodeptr first, last;
q=(p == NULL ? l.first : p ->next); }dlist;
newp->next = q;
if(p == NULL) void createdlist (dlist &l)
l.first=newp; {
else l.first= l.last = NULL;
p ->next = newp; }
newp ->prev = p;
if(q==NULL) int emptydlist(dlist l)
l.last = newp; {
else return (l.first == NULL);
q->prev=newp; }
}
//XOA 1 PHAN TU TRONG DANH SACH void insertdlist(dlist &l, elem &x,nodeptr p)
void deletedlist(dlist &l, nodeptr p) {
{ nodeptr q,newp;
nodeptr q,t; newp= new node;
t=(p == NULL ? l.first : p ->next); memcpy(&newp->data, &x,sizeof(elem));
q= t->next; q=(p == NULL ? l.first : p ->next);
if(p == NULL) newp->next = q;
l.first = q; if(p == NULL)
else l.first=newp;
p ->next=q; else
if(q==NULL) p ->next = newp;
l.last = p; newp ->prev = p;
else if(q==NULL)
q->prev = p; l.last = newp;
delete t; else
q->prev=newp;
} }
//HAM SAO CHEP DLIST
void copy_dlist(dlist a,dlist &b) void deletedlist(dlist &l, nodeptr p)
{ {
nodeptr pa=a.last; nodeptr q,t;
createdlist(b); t=(p == NULL ? l.first : p ->next);
while(pa!=NULL) q= t->next;
{ if(p == NULL)
insertdlist(b,pa->data,NULL); l.first = q;
pa=pa->prev; else
} p ->next=q;
} if(q==NULL)
//HAM XOA TAT CA CAC PHAN TU l.last = p;
void deleteall(dlist &l) else
{ q->prev = p;
while(l.first!=NULL) delete t;
deletedlist(l,NULL); }
}
DLIST QUANG #include<iostream.h>

Trang 13
#include<stdio.h> pb=pb->prev;
typedef int elem; }
#include"DLIST.cpp" if(nho>0)
insertdlist(c,nho,NULL);
//HAM NHAP }
void nhap(dlist &a)
{ //HAM NHAN SO LON VOI SO N
char s; int sa; void nhan(dlist a,dlist &b,int n)
cout<<"\nNhap vao so "; {
cout.flush(); //loai bo getchar createdlist(b);
createdlist(a); nodeptr pa;
while((s=getchar())!='\n') int x,nho=0;
{ pa=a.last;
sa=s-'0'; while(pa!=NULL)
insertdlist(a,sa,a.last); {
} nho=nho+pa ->data * n;
} x=nho%10;
//HAM XUAT insertdlist(b,x,NULL);
void xuat(dlist &a) nho=nho/10;
{ pa= pa ->prev;
nodeptr p; }
p=a.first; if(nho>0)
while(p!=NULL) insertdlist(b,nho,NULL);
{ }
cout<<p->data; //HAM NHAN HAI SO LON VOI NHAU
p=p->next; //tro toi phan tu void nhan2(dlist a,dlist b,dlist &c)
tiep theo cua list {
} dlist tam; int d=-1,i,khong=0;
} nodeptr pa,pb;
//HAM CONG SO LON createdlist(c);
void cong(dlist a,dlist b,dlist &c) pa=a.last; pb=b.last;
{ while(pb!=NULL)
nodeptr pa,pb; {
int x,nho=0; createdlist(tam);
createdlist(c); for(++d,i=0;i<d;i++)
pa=a.last;
pb=b.last; insertdlist(tam,khong,NULL);
while(pa!=NULL && pb!=NULL) nhan(a,tam,pb->data);
{ cong(tam,c,c);
nho=nho + pa ->data +pb ->data; pb= pb ->prev;
x=nho%10; }
insertdlist(c,x,NULL); }
nho=nho/10; //HAM TINH GIAI THUA CUA MOT SO LON
pa= pa ->prev; void tinhgiaithua(dlist &a,int N)
pb= pb ->prev; {
} int so=1,i=N;
while(pa!=NULL) createdlist(a);
{ if(N<2)
nho=nho+pa ->data; insertdlist(a,so,NULL);
x=nho%10; else
insertdlist(c,x,NULL); {
nho=nho/10; while(i>0)
pa= pa ->prev; {
} so=i%10;
while(pb!=NULL) insertdlist(a,so,NULL);
{ i=i/10;
nho=nho+pb ->data; }
x=nho%10; for(i=N-1;i>1;i--)
insertdlist(c,x,NULL); nhan(a,a,i);
nho=nho/10; }

Trang 14
} newp=new node;
memcpy(&newp-
>data,&x,sizeof(elem));
newp->next=NULL;
l.first->next=newp;
newp->prev=l.first;
l.last=newp;
void main() l.first=l.first->next;
{ }
int n; dlist a,b,c; }while(x>=0);
nhap(a); nhap(b); l.first=p->next;
cout<<"Sau khi cong lai la\n";
cong(a,b,c); xuat(c); delete p;
cout<<"\nBAN NHAP VAO SO A\n"; l.first->prev=NULL;
nhap(a); }
cout<<"\nBan nhap vao so N "; cin>>n;
cout<<"\nSau khi nhan A voi so N\n"; void xuatdlist(dlist l)
nhan(a,b,n); xuat(b); {
nhap(a); nhap(b); while(!(emptydlist(l)))
cout<<"Sau khi nhan lai la\n"; {
nhan2(a,b,c); xuat(c); cout<<l.first->data<<setw(2);
cout<<"Ban nhap vao mot so de tinh giai l.first=l.first->next;
thua "; }
cin>>n; tinhgiaithua(a,n); xuat(a); }
cout<<"\n";
} void insertdlist(dlist &l,elem &x,nodeptr p)
--sf— {
OANH nodeptr q,newp;
#include<string.h> newp=new node;
#include<iomanip.h> memcpy(&newp->data,&x,sizeof(elem));
typedef struct nodet{ q=(p==NULL?l.first:p->next);
newp->next=q;
elem data; if(p==NULL)
l.first=newp;
struct nodet *next,*prev; else
}node; p->next=newp;
typedef node *nodeptr; newp->prev=p;
typedef struct{ if(q==NULL)
nodeptr first,last; l.last=newp;
}dlist; else
q->prev=newp;
void createdlist(dlist &l) }
{
l.first=l.last=NULL; void deletedlist(dlist &l,nodeptr p)
} {
int emptydlist(dlist l) nodeptr t,q;
{ t=(p==NULL?l.first:p->next);
return l.first==NULL; q=t->next;
} if(p==NULL)
l.first=q;
void nhapdlist(dlist &l) else
{ p->next=q;
nodeptr p,newp; if(q==NULL)
int x; l.last=p;
p=l.first=l.last=new node; else
do q->prev=p;
{ delete t;
cin>>x; }
if(x>=0)
{ void saochep(dlist a,dlist &b)

Trang 15
{ if(nho>0)
nodeptr pa=a.last; insertdlist(b,nho,NULL);
createdlist(b); }
while(pa!=NULL) void nhan(dlist a,dlist b, dlist &l)
{ {
insertdlist(b,pa->data,NULL); dlist d,c;
pa=pa->prev; nodeptr pb;
} int x=0,dem=0,t;
} pb=b.last;
void cong(dlist a,dlist b,dlist &c) createdlist(l);
{ insertdlist(l,x,NULL);
nodeptr pa,pb; while(pb!=NULL)
int x; {
int nho=0; nhan1(a,pb->data,d);
createdlist(c); if(dem>0)
pa=a.last; {
pb=b.last; t=dem;
while(pa!=NULL&&pb!=NULL) while(t>0)
{ {
nho=nho+(pa->data)+(pb->data);
x=nho%10; insertdlist(d,x,d.last);
insertdlist(c,x,NULL); t--;
nho=nho/10; }
pa=pa->prev; }
pb=pb->prev; cong(d,l,c);
} saochep(c,l);
while(pa!=NULL) dem++;
{ pb=pb->prev;
nho=nho+(pa->data); }
x=nho%10; }
insertdlist(c,x,NULL); void deleteall(dlist &l)
nho=nho/10; {
pa=pa->prev; while(l.first!=NULL)
} deletedlist(l,NULL);
while(pb!=NULL) }
{
nho=nho+(pb->data); TREE PHUONG
x=nho%10; #include<iostream.h>
insertdlist(c,x,NULL); #include<iomanip.h>
nho=nho/10; #include<memory.h>
pb=pb->prev;
}
if(nho>0) typedef struct nodet {
insertdlist(c,nho,NULL); elem data;
} struct nodet *left,*right;
}node;
void nhan1(dlist a,int n,dlist &b) typedef node *tree;
{
nodeptr pa; //DUYET TIEN TU
int x,nho=0; void NLR(tree t)
createdlist(b); {
pa=a.last; if(t!=NULL)
while(pa!=NULL) {
{ cout<<t->data<<" "; //xu ly t
nho=nho+(pa->data)*n; NLR(t->left);
x=nho%10; NLR(t->right);
insertdlist(b,x,NULL); }
nho=nho/10; }
pa=pa->prev; //DUYET TRUNG TU
} void LNR(tree t)

Trang 16
{ if(t==NULL)
if(t!=NULL) return 0;
{ return(la(t)+sola(t->left)+sola(t->right));
LNR(t->left); }
cout<<t->data<<" "; //xu ly t //KIEM TRA NUT MOT CON
LNR(t->right); int nut_1con(tree t)
} {
} if(t!=NULL)
//DUYET HAU TU if(t->left==NULL)
void LRN(tree t) return (t->right!=NULL);
{ else
if(t!=NULL) return (t->right==NULL);
{ return NULL;
LRN(t->left); }
LRN(t->right); //DEM SO NUT MOT CON
cout<<t->data<<" "; //xu ly t int dem_1con(tree t)
} {
} if(t==NULL)
//DEM SO NUT return 0;
int sonut(tree t) return nut_1con(t)+dem_1con(t->left)
{ +dem_1con(t->right);
if(t==NULL) }
return 0; //KIEM TRA NUT HAI CON
return(1+sonut(t->left)+sonut(t->right)); int nut_2con(tree t)
} {
//TAO CAY RONG if(t!=NULL)
void MakeNullTree(tree *t) return(t->left!=NULL)&&(t->right!
{ =NULL);
(*t)=NULL; return NULL;
} }
//KIEM TRA CAY RONG //DEM SO NUT HAI CON
int EmptyTree(tree t) int dem_2con(tree t)
{ {
return t==NULL; if(t==NULL)
} return 0;
//XAC DINH CON TRAI CUA MOT NUT return nut_2con(t)+dem_2con(t->left)
tree trai(tree t) +dem_2con(t->right);
{ }
if (t!=NULL) //HAM DEM SO NUT TREN MUC K
return t->left; int muck(tree t,int k)
return NULL; {
} if(t==NULL) return 0;
//XAC DINH CON PHAI CUA MOT NUT if(k>1)
tree phai(tree t) return muck(t->left,k-1)+muck(t-
{ >right,k-1);
if (t!=NULL) return 1;
return t->right; }
return NULL; //HAM DEM SO NUT TREN MUC LE (cho m=1)
} CHAN (m=0)
//KIEM TRA NUT LA int dem_le(tree t,int m)
int la(tree t) {
{ if(t==NULL) return 0;
if(t!=NULL) return m%2+dem_le(t->left,m+1)+dem_le(t-
return(t->left==NULL)&&(t- >right,m+1);
>right==NULL); }
return NULL; //HAM DEM SO NUT TRONG (khong phai la)
} int dem_nuttrong(tree t)
//DEM SO LA {
int sola(tree t) if(t==NULL) return 0;
{ if(t->left==NULL && t->right==NULL)

Trang 17
return 0; in_cay(t->right,m+1);
return 1+dem_nuttrong(t->left) }
+dem_nuttrong(t->right); }
} //HAM TINH TONG CAC NUT TREN CAY
//HAM DEM SO NUT CO GIA TRI BANG X int tong_nut(tree t)
int dem_x(tree t,int x) {
{ if(t==NULL)
if(t==NULL) return 0; return 0;
if(t->data==x) return t->data+tong_nut(t->left)+tong_nut(t-
return 1+dem_x(t->left,x)+dem_x(t- >right);
>right,x); }
return dem_x(t->left,x)+dem_x(t->right,x); //HAM XOA CA CAY
} void xoacay(tree &t)
//HAM TINH CHIEU CAO CAY {
int max(int a,int b) if(t!=NULL)
{ {
if(a>b) return a; xoacay(t->left);
else return b; xoacay(t->right);
} delete t;
int h(tree t) t=NULL;
{ }
if(t==NULL) return 0; }
return 1+ max(h(t->left),h(t->right)); //HAM XOA CAC NUT LA LA
} void xoa_la(tree &t)
//HAM TINH CHIEU RONG CUA CAY (Muc co {
nhieu nut nhat) if(t!=NULL)
int w(tree t) if(la(t)==1)
{ {
int hight=h(t),wight=0,k,i; delete t;
for(i=1;i<=hight;i++) t=NULL;
{ }
k=muck(t,i); else
if(k>wight) wight=k; {
} xoa_la(t->left);
return wight; xoa_la(t->right);
} }
//HAM NHAP CAY THEO TIEN TU (Muc co nhieu }
nut nhat) //HAM TIM MIN CUA CAY
void nhap(tree &t) //HAM TIM MIN CUA 3 GIA TRI
{ int min3(int a,int b,int c)
elem x; {
cout<<"Nhap vao gia tri: "; int min=a;
cin>>x; if(b<min) min=b;
if(x>0) if(c<min) min=c;
{ return min;
t=new node; }
t->data=x; //HAM TIM MIN CUA 2 GIA TRI
nhap(t->left); int min2(int a,int b)
nhap(t->right); {
} if(a<b) return a;
else return b;
t=NULL; }
} int min_tree(tree t)
//HAM IN RA CAY NHI PHAN (cho m=1) {
void in_cay(tree t,int m) if(t!=NULL)
{ {
if(t!=NULL) if(t->left==NULL && t-
{ >right==NULL) return t->data;
in_cay(t->left,m+1); if(t->left==NULL) return min2(t-
cout<<"\n"<<setw(4*m)<<t->data; >data,min_tree(t->right));

Trang 18
if(t->right==NULL) return min2(t- {
>data,min_tree(t->left)); if(t1==NULL)
return min3(t->data,min_tree(t- t2=NULL;
>left),min_tree(t->right)); else
} {
return 0; t2=new node;
} t2->data=t1->data;
//HAM TIM MAX CUA CAY hoandoi_tree(t1->left,t2->right);
//HAM TIM MAX CUA 3 GIA TRI hoandoi_tree(t1->right,t2->left);
int max3(int a,int b,int c) }
{ }
int max=a; //HAM CHEP CAY
if(b>max) max=b; void chepcay(tree t1,tree &t2)
if(c>max) max=c; {
return max; if(t1==NULL)
} t2=NULL;
int max_tree(tree t) else
{ {
if(t==NULL) return 0; t2=new node;
return max3(t->data,max_tree(t- t2->data=t1->data;
>left),max_tree(t->right)); chepcay(t1->left,t2->left);
} chepcay(t1->right,t2->right);
//HAM THEM MOT NUT VAO TREE (Neu cay }
rong them vao goc }
//nguoc lai them vao cay con thap hon trong 2 CAY NHI PHAN BST
cay con cua goc //HAM CHEN X VAO CUOI CAY BST
//Neu 2 cay con bang nhau them vao cay ben void inserttree(tree &t,elem x)
trai {
void chen_tree(tree &t,elem x) if(t==NULL)
{ {
t=new node;
if(t==NULL) t->data=x;
{ t->left=t->right=NULL;
t=new node; }
t->data=x; else
t->left=t->right=NULL; if(x<t->data)
} inserttree(t->left,x);
else else
{ if(x>t->data)
tree newx=new node; inserttree(t-
newx->data=x; >right,x);
if(h(t->left)<=h(t->right)) }
{ //HAM TIM BIA PHAI
newx->left=t->left; void del(tree &r,tree &q)
newx->right=NULL; {
t->left=newx; if(r->right!=NULL)
} del(r->right,q);
else else
{ {
newx->right=t->right; q->data=r->data;
newx->left=NULL; q=r;
t->right=newx; r=r->left;
} }
} }
} //HAM XOA 1 PHAN TU
//HAM HOAN DOI TAT CA CAY CON BEN TRAI void deletetree(tree &t,elem x)
VA CAY CON BEN PHAI TUONG UNG {
//Muon hoan doi cay t1 goi ham if(t!=NULL)
hoandoi_tree(t1,&t1) if(x<t->data)
void hoandoi_tree(tree t1,tree &t2) deletetree(t->left,x);

Trang 19
else cout<<"So nut tren muc "<<k<<" la
if(x>t->data) "<<muck(t,k);
deletetree(t- cout<<"\nTong cac gia tri cac nut tren cay
>right,x); la "<<tong_nut(t);
else cout<<"\nDem cac nut co tren muc le
{ "<<dem_le(t,1);
tree q=t; cout<<"\nDem cac nut co tren muc chan
if(t->right==NULL) "<<dem_le(t,0);
t=t->left; cout<<"\nDem cac nut trong (khong la la)
else "<<dem_nuttrong(t);
if(t->right cout<<"\nDem so nut co gia tri bang x ";
== NULL) cin>>k;
cout<<"So nut co gia tri bang "<<k<<" la
t=t->right; "<<dem_x(t,k);
else cout<<"\nMax cua cay la "<<max_tree(t);
cout<<"\nMin cua cay la "<<min_tree(t);
del(t->left,q); cout<<"\nIN RA CAY NHI PHAN DA XOA
delete q; 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
//HAM TIM X TREN CAY NHI PHAN nho hon\n
tree searchtree(tree t,elem x) Chen vao nhanh trai neu 2
{ nhanh bang nhau"; chen_tree(t,k);
if(t==NULL) cout<<"\nIN RA CAY NHI PHAN DA
return NULL; CHEN\n"; in_cay(t,1);
else cout<<"\n";
if(x<t->data) }
return searchtree(t->left,x); ***NANG CAO****
else #include <iostream.h>
return searchtree(t- #include <string.h>
>right,x); #include <ctype.h>
} #include<stdlib.h>
typedef char elem[4];
#include <iostream.h> #include "TREE.cpp"
typedef int elem;
#include "TREE.cpp" void nhap_toan(tree &t)
{
void main() elem x;
{ cout<<"Nhap vao gia tri: ";
tree t; int k; cin.getline(x,4);
cout<<"\nNhap vao cay theo cach duyet if(x[0]!='0')
tien tu\n"; nhap(t); {
cout<<"IN RA CAY NHI PHAN\n"; t=new node;
in_cay(t,1); strcpy(t->data,x);
cout<<"\nXuat cay theo cach duyet tien nhap_toan(t->left);
tu\n"; NLR(t); nhap_toan(t->right);
cout<<"\nXuat cay theo cach duyet trung }
tu\n"; LNR(t); else
cout<<"\nXuat cay theo cach duyet hau t=NULL;
tu\n"; LRN(t); }
cout<<"\nSo nut cua cay la :"<<sonut(t); //HAM IN BIEU THUC TOAN CAY NHI PHAN (cho
cout<<"\nSo la cua cay la :"<<sola(t); m=1)
cout<<"\nSo nut 1 con cua cay void in_cay(tree t,int m)
la :"<<dem_1con(t); {
cout<<"\nSo nut 2 con cua cay if(t!=NULL)
la :"<<dem_2con(t); {
cout<<"\nChieu cao cua cay la "<<h(t); in_cay(t->left,m+1);
cout<<"\nDem so nut tren muc k "; cout<<"\n"<<setw(4*m)<<t->data;
cin>>k; in_cay(t->right,m+1);
}

Trang 20
} l->data=x;
//HAM TINH GIA TRI CUA BIEU THUC }
int tinh(tree t) }while(x>0);
{ l->next=NULL;
if(t!=NULL) l=p->next;
{ delete p;
if(t->data[0]<'0') }
{ //HAM XUAT MANG SO BANG LIST
if(t->data[0]=='+') void xuat(list l)
return tinh(t->left) {
+tinh(t->right); while(l!=NULL)
if(t->data[0]=='-') {
return tinh(t->left)- cout<<l->data<<" ";
tinh(t->right); l=l->next; //tro toi phan tu
if(t->data[0]=='*') tiep theo cua list
return tinh(t- }
>left)*tinh(t->right); }
if(t->data[0]=='/') //HAM CHEN PHAN TU VAO CUOI DANH SACH
return tinh(t- void chen_cuoi(list &l,elem &x)
>left)/tinh(t->right); {
} list p=l;
else while(p!=NULL && p->next!=NULL)
return atoi(t->data); p=p->next;
} insertlist(l,x,p);
return 0; }
} //HAM SO SANH
int ss(elem a,elem b)
void main() {
{ return a-b;
tree t; }
cout<<"\nNhap vao cay theo cach duyet //HAM DEM SO PHAN TU BANG LIST
tien tu\n"; nhap_toan(t); int dem_x(list l,int x)
cout<<"IN RA CAY NHI PHAN\n"; {
in_cay(t,1); if(l==NULL) return 0;
cout<<"\nGIA TRI CUA BIEU THUC LA int dem=0;
"<<tinh(t); do
cout<<"\n"; {
} if(l->data==x)
++dem;
****Ôn tập*** l=l->next;
DANH SACH LIEN KET DON }while(l!=NULL);
#include <iostream.h> return dem;
typedef int elem; }
#include "LIST.cpp" //HAM SAO CHEP DANH SACH
void copy_list(list l1,list &l2)
//HAM NHAP MANG SO DUONG BANG LIST {
THEO THU TU BAN PHIM creatlist(l2);
void nhapdem(list &l) list p;
{ p=l2=new node;
creatlist(l); while(l1!=NULL)
list p; int x; {
p=l=new node; //pt dem l2->next=new node;
do l2=l2->next;
{ l2->data=l1->data;
cout<<"Nhap vao phan tu "; l1=l1->next;
cin>>x; }
if(x>0) l2->next=NULL;
{ l2=p->next;
l->next=new node; delete p;
l=l->next; }

Trang 21
//HAM HUY TOAN BO DANH SACH void doi_cuoi(list &l,int m)
void huy(list l) {
{ list p,q;p=q=l;
while(l!=NULL) //Cho q chay len cach p m node
deletelist(l,NULL); for(;q && m>0;m--)
} q=q->next;
//HAM CHEN SO 0 VAO SAU CAC PHAN TU if(q) //q!=NULL
CHAN {
void chen0(list &l) //Cho q chay den cuoi
{ for(;q->next;q=q->next,p=p-
list p=l; int khong=0; >next);
while(p!=NULL) //Luc nay q=5 p=3
{ q->next=l;
if(p->data%2==0) l=p->next;
{ p->next=NULL;
insertlist(l,khong,p); }
p=p->next; }
}
p=p->next; void main()
} {
} list l,p,l1; int x;
//HAM CHEN X VAO LIST TANG creatlist(l);
void chen_tang(list &l,elem x) nhapdem(l); xuat(l);
{ /*cout<<"\nNhap vao so x "; cin>>x;
list p,q;int chen=0; cout<<"Kiem tra x co trong danh sach
//p sau q 1 buoc khong";
p=NULL; q=l; if(searchlist(l,x,p,ss)==NULL)
while(chen==0 && q!=NULL) cout<<"\nKhong co";
{ else
if(x<=q->data) cout<<"\nx co trong danh sach";
{ cout<<"\nSo phan tu bang x trong danh
insertlist(l,x,p); sach "<<dem_x(l,x);
chen=1; cout<<"\nChen phan tu x vao cuoi danh
} sach\n";
p=q; q=q->next; chen_cuoi(l,x); xuat(l);
} cout<<"\nXoa tat ca cac phan tu bang
if(chen==0) x\n";
insertlist(l,x,p); xoa_x(l,x); xuat(l);
} cout<<"\nSao chep list l1 vao list l2\n";
//XOA TAT CA CAC PHAN TU BANG X copy_list(l,l1); xuat(l);
void xoa_x(list &l,elem x) cout<<"\nChen so 0 vao sau cac ptu
{ chan\n";
list p,q; chen0(l); xuat(l);
//p sau q 1 buoc cout<<"\nChen so x list tang sao cho
p=NULL; q=l; bao dam tinh tang dan\n";
while(q!=NULL) cout<<"Nhap vao list tang\n";
if(x==q->data) nhapdem(l); xuat(l);
{ cout<<"\nNhap vao so x "; cin>>x;
q=q->next; chen_tang(l,x); xuat(l);*/
deletelist(l,p); cout<<"\nNhap vao m de doi m phan tu
} cuoi ra dau\n"; cin>>x;
else doi_cuoi(l,x); xuat(l);
{
p=q; cout<<"\n";
q=q->next; }
} ******DANH SACH LIEN KET DOI****
} #include <iostream.h>
//DOI M PHAN TU CUOI CUA DANH SACH LEN typedef int elem;
DAU #include "DLIST.cpp"
// Nhap 1 2 3 4 5 nhap m=2 Kq 4 5 1 2 3

Trang 22
//HAM SO VAO DLIST TU LAM {
void nhap_dlist2(dlist &l) dem++;
{ l.first=l.first->next;
nodeptr p,newp; int x; }
p=l.first=new node; //Tao phan tu dem return dem;
do }
{ //HAM TIM MAX CUA DLIST (tra ve con tro den ptu
cout<<"Nhap vao "; cin>>x; lon nhat)
if(x>0) nodeptr max_dlist(dlist l)
{ {
newp=new node; nodeptr m=l.first;
p->next=newp; while(l.first!=NULL)
newp->prev=p; {
p=p->next; if(l.first->data>m->data)
p->data=x; m=l.first;
} l.first=l.first->next;
}while(x>0); }
p->next=NULL; //pt cuoi tro toi NULL return m;
l.last=p; //con tro last =pt cuoi }
p=l.first; //cho p tro toi ptu dem //HAM SAP XEP TANG CAC PHAN TU CUA
l.first=l.first->next; //Bo ptu dem DANH SACH L1 TRA VE L2
l.first->prev=NULL; //ptu dau //Neu muon SXDS l1 ta goi sapxep_dlist(l1,l1)
tro ve NULL void sapxep_dlist(dlist l1,dlist &l2)
delete p; //Xoa ptu dem {
} createdlist(l2); nodeptr m;
//HAM NHAP BANG INSERTDLIST while(!emptydlist(l1))
void nhap_dlist(dlist &l) {
{ m=max_dlist(l1);
int x; cout<<"Nhap vao "; cin>>x; insertdlist(l2,m->data,NULL);
while(x>0) deletedlist(l1,m->prev);
{ }
insertdlist(l,x,l.last); }
cout<<"Nhap vao "; cin>>x; /*Mot so luu y ve han chen va delete
} insert(NULL) chen dau
} insert(l.last) chen cuoi
//XUAT DLIST insert(l.frist) chen sau pt thu 1
void xuat_dlist(dlist l) insert(p) chen sau pt p
{ De doi bai tren thanh sx giam ta thay
while(!emptydlist(l)) insertdlist(l2,m->data,l.last);*/
{
cout<<l.first->data<<" "; //HAM TACH CHAN LE DANH SACH DOI
l.first=l.first->next; void tach_chanle(dlist l,dlist &l1,dlist &l2)
} {
} createdlist(l1); createdlist(l2);
//HAM DAO NGUOC DLIST L1 VAO L2 //Tao phan tu dem
//muon dao list l1 dao_dlist(l1,l1) insertdlist(l2,l.first->data,NULL);
void dao_dlist(dlist l1,dlist &l2) insertdlist(l1,l.first->data,NULL);
{ //Chep cac so chan le vao cuoi l2,l1
createdlist(l2); while(l.first!=NULL)
while(l1.last!=NULL) {
{ if(l.first->data%2==0)
insertdlist(l2,l1.last->data,l2.last); insertdlist(l2,l.first-
l1.last=l1.last->prev; >data,l2.last);
} else
} insertdlist(l1,l.first-
//HAM DEM SO PHAN TU CUA DANH SACH >data,l1.last);
int dem_dlist(dlist l) l.first=l.first->next;
{ }
int dem=0; //Xoa phan tu dem o dau
while(!emptydlist(l)) deletedlist(l1,NULL);

Trang 23
deletedlist(l2,NULL); dlist l,l1,l2; nodeptr tam;
} createdlist(l); createdlist(l1);
createdlist(l2);
//HAM LAY RA MOT DANH SACH CON TANG /*nhap_dlist(l); xuat_dlist(l);
void con_dlist(dlist &l,dlist &l1) cout<<"\nCopy vao DSLK khac\n";
{ copy_dlist(l,l1); xuat_dlist(l1);
createdlist(l1); elem x; cout<<"\nDao nguoc danh sach\n";
if(l.first!=NULL) dao_dlist(l,l); xuat_dlist(l);
x=l.first->data; cout<<"\nSo phan tu cua danh sach la
while(l.first!=NULL && x<=l.first->data) "<<dem_dlist(l);
{ cout<<"\nPhan tu lon nhat cua danh sach
x=l.first->data; la :"<<max_dlist(l)->data;
insertdlist(l1,x,l1.last); sapxep_dlist(l,l);
deletedlist(l,NULL); cout<<"\nSap xep lai danh sach theo thu tu
} tang\n"; xuat_dlist(l);
} cout<<"\nTach thanh 2 danh sach chan
//HAM TRON 2 MANG TANG THANH 1 MANG le\n";
TANG TRA CON TRO VE CUOI tach_chanle(l,l1,l2);
void merge(dlist &l,dlist l1, dlist l2) cout<<"\nDANH SACH LE\n";
{ xuat_dlist(l1);
createdlist(l); cout<<"\nDANH SACH CHAN\n";
nodeptr p=l.first=new node; xuat_dlist(l2);
while( l1.first!= NULL && l2.first!=NULL) cout<<"\nLAY RA DANH SACH CON
{ TANG\n"; con_dlist(l,l1);
if(l1.first->data < l2.first->data) cout<<"\nDanh sach me\n";
{ xuat_dlist(l);
p->next=l1.first; cout<<"\nDanh sach con\n";
l1.first->prev=p; xuat_dlist(l1);*/
l1.first=l1.first->next; cout<<"\nNHAP VAO HAI DANH SACH
} TANG DE TRON LAI\n";
else cout<<"\nNhap vao DS thu 1\n";
{ nhap_dlist(l1); xuat_dlist(l1);
p->next=l2.first; cout<<"\nNhap vao DS thu 2\n";
l2.first->prev=p; nhap_dlist(l2); xuat_dlist(l2);
l2.first=l2.first->next; cout<<"\nTron 2 danh sach thanh 1\n";
} merge(l,l1,l2); xuat_dlist(l);
p=p->next; cout<<"\n";
} }
//Gan phan con lai vao l *******GIAI DE THI NAM 2007 - 2008**********
if(l1.first!=NULL) Câu 1
{ p->next=l1.first; #include <iostream.h>
l1.first->prev=p; typedef struct {
} else { int msmh;
p->next=l2.first; int sl;
l2.first->prev=p; double tt;
} }elem;
//Tro toi phan tu cuoi #include "LIST.cpp"
while(p->next!=NULL)
p=p->next; //HAM NHAP THONG TIN
//Xu ly phan cuoi void nhapdem(list &l)
l.last=p; //con tro last =pt cuoi {
p=l.first; //cho p tro toi ptu dem list p; int x;
l.first=l.first->next; //Bo ptu dem p=l=new node; //pt dem
l.first->prev=NULL; //ptu dau do
tro ve NULL {
delete p; //Xoa ptu dem cout<<"\nNhap vao ma so mat
} hang "; cin>>x;
if(x>0)
void main() {
{ l->next=new node;

Trang 24
l=l->next; while(l->next!=NULL)
l->data.msmh=x; l=l->next;
cout<<"Nhap vao so luong l=p->next;
"; cin>>l->data.sl; delete p;
cout<<"Nhap vao thanh }
tien "; cin>>l->data.tt;
} //HAM XUAT DU LIEU
}while(x>0); void xuat(list l)
l->next=NULL; {
l=p->next; while(l!=NULL)
delete p; {
} cout<<"\n\nMa so mat hang "<<l-
//HAM IN RA GIA TRI TRUNG BINH CONG >data.msmh;
double tb_gia(list l) cout<<"\nSo luong "<<l->data.sl;
{ cout<<"\nThanh tien "<<l->data.tt;
double T=0; int dem=0; l=l->next; //tro toi phan tu
while(l!=NULL) tiep theo cua list
{ }
dem++; }
T=T + l->data.tt / l->data.sl; void main()
l=l->next; {
} list l1,l2,l;
return T/dem; creatlist(l1); creatlist(l2); creatlist(l);
} cout<<"\nNhap vao l1\n";
//HAM TAO RA MOT DANH SACH LIEN KET nhapdem(l1); xuat(l1);
TANG NGHIEM NGAT cout<<"\nNhap vao l2\n";
void ghep(list &l,list l1, list l2) nhapdem(l2); xuat(l2);
{ cout<<"\nTinh Trung Binh gia cac mat hang
list p; l1 "<<tb_gia(l1);
l=p=new node; cout<<"\nGhep 2 list thanh 1 list";
while( l1!= NULL && l2!=NULL) ghep(l,l1,l2); xuat(l);
if(l1->data.msmh == l2- cout<<"\n";
>data.msmh) }
{ #include <iostream.h>
l->next=new node; #include <string.h>
l=l->next; #include <ctype.h>
l->data.msmh=l1- #include<stdlib.h>
>data.msmh; typedef char elem[4];
l->data.sl=l1->data.sl + l2- #include "TREE.cpp"
>data.sl;
l->data.tt=l1->data.tt + l2- void nhap_toan(tree &t)
>data.tt; {
l1=l1->next; elem x;
l2=l2->next; cout<<"Nhap vao gia tri: ";
} cin.getline(x,4);
else if(x[0]!='0')
{ {
if(l1->data.msmh < l2- t=new node;
>data.msmh) strcpy(t->data,x);
{ l->next=l1; nhap_toan(t->left);
l1=l1->next; nhap_toan(t->right);
} else { }
l->next=l2; else
l2=l2->next; t=NULL;
} l = l->next; }
} //HAM IN BIEU THUC TOAN CAY NHI PHAN (cho
if(l1!=NULL) m=1)
l->next=l1; void in_cay(tree t,int m)
else {
l->next=l2; if(t!=NULL)

Trang 25
{ cout<<"\nNhap vao cay theo cach duyet
in_cay(t->left,m+1); tien tu\n"; nhap_toan(t);
cout<<"\n"<<setw(4*m)<<t->data; cout<<"IN RA CAY NHI PHAN\n";
in_cay(t->right,m+1); in_cay(t,1);
} cout<<"\nSo Toan Hang cua cay la
} "<<sola(t);
//HAM TINH GIA TRI CUA BIEU THUC cout<<"\nSo Bieu Thuc cua cay la
int tinh(tree t) "<<dem_2con(t);
{ cout<<"\nGIA TRI CUA BIEU THUC LA
if(t!=NULL) "<<tinh(t);
{ cout<<"\n";
if(t->data[0]<'0') }
{ --CAY NHI PHAN BST---
if(t->data[0]=='+') #include <iostream.h>
return tinh(t->left) #include <string.h>
+tinh(t->right); #include <ctype.h>
if(t->data[0]=='-') #include<stdlib.h>
return tinh(t->left)- typedef int elem;
tinh(t->right); #include "TREE.cpp"
if(t->data[0]=='*')
return tinh(t- //20 10 30 5 14 25 40 1 12 18 28 16 0
>left)*tinh(t->right); //HAM NHAP CAY BST
if(t->data[0]=='/') void nhap_BST(tree &t)
return tinh(t- {
>left)/tinh(t->right); int x;
} do
else {
return atoi(t->data); cin>>x;
} if(x>0)
return 0; inserttree(t,x);
} }while(x>0);
//DEM SO LA (DEM SO TOAN HANG }
int sola(tree t) //HAM DUYET BFS (DUYET THEO TUNG MUC)
{ //ham in cac nut o muc k
if(t==NULL) return 0; void in_muck(tree t,int k,int i=1)
if(t->left==NULL && t->right==NULL) {
return 1; if(t)
return sola(t->left)+sola(t->right); if(i==k)
} cout<<setw(4)<<t->data;
//KIEM TRA NUT HAI CON else
int nut_2con(tree t) {
{ in_muck(t->left,k,i+1);
if(t!=NULL) in_muck(t->right,k,i+1);
return(t->left!=NULL)&&(t->right! }
=NULL); }
return NULL; void duyet_BFS(tree t)
} {
//DEM SO NUT HAI CON (DEM SO PHEP TOAN) int i,cao=h(t);
int dem_2con(tree t) for(i=1;i<=cao;i++)
{ {
if(t==NULL) in_muck(t,i);
return 0; cout<<"\n";
return nut_2con(t)+dem_2con(t->left) }
+dem_2con(t->right); }
} //TIM MUC CUA NODE CHUA SO X CHO TRUOC
int tim_mucx(tree t,int x,int m) //m=1
{
void main() if(t==NULL) return 0;
{ if(t->data==x) return m;
tree t; 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;
else
cout<<"\nKhong tim thay "<<x;

cout<<"\n";
}

Trang 27

Вам также может понравиться