không đúng đừng chém nha
bài 1 đây bạn ah
Mã:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
//Khai bao cau truc
struct node
{
int pt;
struct node *next;
};
typedef node list;
list *first,*ds2,*last;
//Xoa sach cac node trong danh sach va khoi tao danh sach
void clear_list(list **first)
{
list *p=*first;
while(*first!=NULL)
{
*first=p->next;
delete(p);
p=*first;
}
}
//In danh sach
void print_list(list *first)
{
if(first==NULL)
printf("\nDanh sach rong");
else
{
list *p=first;
printf("\nDanh sach:\n ");
while(p!=NULL)
{
printf("%d\t", p->pt);
p=p->next;
}
}
}
//Tao danh sach moi
void create_list(list **first)
{
int n;
list *p;
printf("\nNhap gia tri: ");
scanf("%d",&n);
while(n!=0)
{
p=new node;
p->pt=n;
if(*first==NULL)
*first=p;
else
last->next=p;
last=p;
p->next=NULL;
printf( "\nNhap gia tri: ");
scanf("%d",&n);
}
}
void thaythe(list **first)
{
list *p=*first;
while(p!=NULL)
{
if((p->pt)%2!=0)
p->pt=((p->pt)*(p->pt));
p=p->next;
}
}
void main()
{
clear_list(&first);
create_list(&first);
print_list(first);
thaythe(&first);
print_list(first);
getch();
}
Bài 2: chỉ có thủ tục thôi bạn bổ sung thêm hàm nhập nhé
Mã:
void daomang(int a[],int n)
{
int tmp,j=0;
for(int i=n-1;i>=(n/2);i--)
{
tmp=a[j];
a[j]=a[i];
a[i]=tmp;
j++;
}
}