BON CHEN VÀI BÀI (CÓ THỂ CHƯA RA KẾT QUẢ)
DANH SÁCH ĐẶC
+HÀNG ĐỢI:
bài này ra rồi
Mã:
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
int Queue[50];
int front,rear;
void chen(int &n,int Queue[],int &front , int &rear)
{
front=0;
cout<<"nhap hang doi:\n";
for(int i=1; i<=n; i++)
{
cout<<"so thu "<<i<<" la: ";
cin>>Queue[i];
rear++;
Queue[rear]=Queue[i];
}
}
void xuat(int Queue[])
{
for(int i=front; i<=rear; i++)
{
cout<<"\t"<<Queue[i];
}
}
void popQ(int &x,int Queue[],int &front,int &rear)
{
if(front==-1)
printf("\nhang rong");
else
{
x=Queue[front];
front=front+1;
if(front>rear)
{
front=-1;
rear=-1;
}
}
}
void pushQ(int x,int Queue[],int &front,int &rear)
{
if(rear-front+1==50) //hang rong
printf("hang day");
else
{
if(front==-1)
front=0;
if(rear==49) //rear o cuoi
{
for(int i=front; i<=rear; i++)
Queue[i-front]=Queue[i];
rear=50-front-1;
front=0;
}
rear=rear+1;
if(front==0)
Queue[rear]=x; //them hang tren Queue[rear]=x;
}
}
void main()
{
int x;
int i,n;
clrscr();
front=-1;
rear=-1;
cout<<"nhap so phan tu cua hang doi:";
cin>>n;
chen(n,Queue,front,rear);
cout<<"hang doi vua nhap la:\n";
xuat(Queue);
cout<<"\nnhap pt can them: ";
cin>>x;
cout<<"hang doi sau khi them: ";
pushQ(x,Queue,front,rear);
xuat(Queue);
cout<<endl;
cout<<"hang doi sau khi xoa phan tu dau hang : ";
popQ(x,Queue,front,rear);
xuat(Queue);
getch();
}
+DANH SÁCH ĐẶC BÌNH THƯỜNG
Mã:
#include<iostream.h>
#include<conio.h>
#define max 30
int dt[max];
int last;
int tim(int x,int dt[])//tim phan tu x trong danh sach
{
int i=1,d=0;
if(last==0)
cout<<"\n danh sach rong";
else
{
while(i<last && d==0)
{
if(dt[i]==x)
d=1;
else i++;
}
}
if(d==0) return 0;
else return i;
}
void chen(int x,int n,int dt[])//chen phan tu x vao vi tri n trong danh sach
{
if(last==max)
cout<<"\ndanh sach day";
else if(n<1||n>last+1)
cout<<"\n Loi vi tri";
else
{
for(int i=last; i>=n; i--)
dt[i+1]=dt[i];
dt[n]=x;
last++;
}
}
void xoa(int m,int dt[])//xoa phan tu o vi tri m trong danh sach
{
if(last==0)
cout<<"\ndanh sach rong";
else if(m<1||m>last+1)
cout<<"\nLoi vi tri ";
else
{
for(int i=m; i<=last; i++)
dt[i]=dt[i+1];
last--;
}
}
void xem(int dt[])//xuat danh sach ra man hinh
{
for(int i=1; i<=last; i++)
{
cout<<" \t "<<dt[i];
}
}
void nhap(int dt[],int n,int last)//nhap gia tri cho danh sach
{
cout<<"nhap vao so phan tu cua danh sach:";
cin>>n;
for(int i=1; i<=n; i++)
{
cout<<"nhap phan tu thu "<<i<<" ";
cin>>dt[i];
last++;
dt[last]=dt[i];
cout<<endl;
}
}
//chuong trinh cu the de xoa tat ca cac phan tu nho hon 10 co trong danh sach & chen phan tu 20 vao vi tri thu 10.
void main()
{
int n;
int dk=1;
char c;
while(dk)
{
clrscr();
nhap(dt,n,last);
xem(dt);
cout<<"\nDanh sach khi da xoa cac phan tu nho hon 10 la\n ";
for(int i=1; i<=last; i++)
{
if(dt[i]<10)
{
xoa(i,dt);
i--;
}
}
xem(dt);
cout<<"\nDanh sach khi da them 20 vao vi tri 10 la\n ";
chen(20,10,dt);
xem(dt);
cout<<"\nCo tiep tuc ko y/n ";
cin>>c;
if(c=='y') dk=1;
else dk=0;
}
getch();
}
DANH SÁCH LIÊN KẾT
Mã:
#include<iostream.h>
#include<conio.h>
typedef struct Node
{
char ht[30];
int ns;
float dtb;
struct Node *link;
} DS;
DS *L;
void taods(DS **L,char x[30], int y, float z)
{
DS *p;
p= new DS;
p->ht[30]=x[30];
p->ns=y;
p->dtb=z;
p->link =*L;
*L=p;
}
void xuatds(DS *L)
{
DS *p;
p=L;
while (p!=NULL)
{
cout<<p->ht[30]<<"\t"<<cout<<p->ns<<"\t"<<cout<<p->dtb<<"";
p= p->link ;
}
cout<<"\n";
}
void main()
{
DS *L;
char x[30];
int y;
float z;
int dk=1;
char c;
clrscr();
while (dk)
{
cout<<"nhap ho ten :\n";
cin>>x;
cout<<"nhap nam sinh:\n";
cin>>y;
cout<<"nhap diem trung binh:\n";
cin>>z;
taods(&L,x,y,z);
cout<<"ban co muon them vao danh sach ko?y/n ";
cin>>c;
if(c=='y')
dk=1;
else dk=0;
}
xuatds(L);
getch();
}
bài này chưa xuất họ tên được
BÀI CÂY
Mã:
#include<stdio.h>
#include<conio.h>
#include<math.h>
typedef int tdata;
typedef struct tnode
{
tdata data;
tnode *left;
tnode *right;
} ;
typedef tnode *Ttree;
Ttree insert(tdata v,Ttree l,Ttree r)
{
Ttree n;
n=new tnode;
n->data=v;
n->left=l;
n->right=r;
return n;
}
void preorder(Ttree T)
{
Ttree p;
p=T;
if(p!=NULL)
{
printf("%d\t",p->data);
preorder(p->left);
preorder(p->right);
}
}
void inorder(Ttree T)
{
Ttree p;
p=T;
if(p!=NULL)
{
inorder(p->left);
printf("%d\t",p->data);
inorder(p->right);
}
}
void posorder(Ttree T)
{
Ttree p;
p=T;
if(p!=NULL)
{
posorder(p->left);
posorder(p->right);
printf("%d\t",p->data);
}
}
int dem(Ttree T)
{
Ttree p;
int demnut=0;
p=T;
if(p!=NULL)
demnut=dem(p->left)+dem(p->right)+1;
return demnut;
}
void main()
{
clrscr();
int demnut;
float h=0;
Ttree T;
T=insert(50,insert(25,NULL,insert(10,insert(38,NULL,insert(9,insert(7,NULL,insert(17,insert(8,NULL,NULL),insert(18,NULL,NULL))),NULL)),NULL)),NULL);
printf("tien to\n");
preorder(T);
printf("\n");
printf("trung to\n");
inorder(T);
printf("\n");
printf("hau to\n");
posorder(T);
printf("\n");
printf("so nut trong cay la %d",dem(T));
printf("\n");
printf("chieu cao cua cay la %f",h);
h=log(dem(T)+1)/log(2);
getch();
}