RSS

Thứ Hai, 20 tháng 6, 2011

BÀI TẬP NHÓM 5

Cài đặt chương trình hoàn chỉnh minh họa việc cài đặt danh sách đặc
Yêu cầu 1:
·        Viết các khai báo và các phép toán cơ bản cần thiết càiđặt danh sách các số nguyên.
Yêu cầu 2:
·        viết hàm cho phép nhập danh sách các số nguyên theo thứ tự tăng (Các phần tử khi nhập vào sẽ tự tìm vị trí sao cho danh sách có thứ tự tăng).
·        viết hàm cho phép tìm và xóa tất cả các phần tử có nội dung x.
·        viết hàm tìm phần tử lẻ dầu tiên sau phần tử có nội dung x
ví dụ: Dãy 3 4 6 8 9 5
nhập x=4
phần tử lẻ đầu tiên sau 4 là 9

·        viết hàm cho phép chèn x vào danh sách tăng đểđược danh sách tăng

#include<conio.h>
#include<stdio.h>
#define maxlength 100
typedef int elementtype;
typedef int position;
typedef struct{
elementtype element[maxlength];
position last;
}list;

void makenull_list(list *l)
{
l->last=0;
}
int empty_list(list l)
{
return l.last==0;
}
int full_list(list l)
{
return l.last==maxlength;
}
void insert_list(elementtype x, position p, list *l)
{
if(full_list(*l))
printf("Danh sach day");
else if(p<0||p>l->last+1)
printf("Vi tri khong hop le");
else
{
for (int i=l->last;i>p;i--)
l->element[i]=l->element[i-1];

l->element[p]=x;
l->last++;
}
}
void delete_list(position p,list *l)
{
int i;
if(empty_list(*l))
printf("Danh sach rong");
else if(p<0||p>l->last)
printf("vi tri khong hop le");
else
{
for(i=p;i<l->last-1;i++)
{
l->element[i]=l->element[i+1];

}
l->last--;
}
}
void read_list(list *l)
{
elementtype x;
int i=0;
makenull_list(l);
do{
printf("\n Nhap phan tu thu %d: ",i+1);
scanf("%d",&x);
if(x!=0)
insert_list(x,i,l);
i++;
}while(x!=0);
}
void print_list(list l)
{
int i=0;
while(i<l.last)
{
printf("%4d",l.element[i]);
i++;
}
}
elementtype retrieve(position p, list l)
{
if(p<0||p>l.last)
return 0;
else return l.element[p];
}
position locate(elementtype x, list l)
{
position p=0;
while(p<l.last && l.element[p]!=x)
{
p++;
}
if (p==l.last) return -1;
else return p;
}
//ham bai tap
void Chen( list *l, position vt,elementtype X)
{
    
    if (vt<1||vt>l->last)
        printf("Vi Tri khong hop le!\n");
    else if(l->last==maxlength)
printf("Danh Sach day!\n");

    else
    {
        for(int i=l->last;i>vt-1;i--)
            l->element[i]=l->element[i-1];
        l->element[vt-1]=X;
        l->last++;
    }
}
int ChenTang(elementtype x,list *l )
{
    
    if(l->last==maxlength)
    {
    printf("Danh Sach day");
    return -1; //Danh Sach day
    }
else if(l->last==0)//Danh Sach dang rong 
{
l->element[0]=x;
l->last++;
return 1; // tra ve vi tri chen la 1
}

    else
    {
        for(int i=1;i<=l->last;i++)
        {
        if(l->element[i-1]>=x)
        {
        Chen(l,i,x);
        return i;//tra ve vi tri chen
        }        
} 
/// Duyet toi het danh sach ma ko gap phan tu lon hon X
l->element[l->last] = x;
l->last++;
return l->last-1; 
    }
    
}


void NhapTang(list *l)
{
    int x;
int N;
makenull_list(l);
    printf("Nhap so phan tu cua sanh sach: ");
scanf("%d",&N);
l->last=0;
    for(int i=1;i<=N;i++)
    {
        printf("Nhap phan tu thu %d: ",i);
        scanf("%d",&x);
        ChenTang(x,l);
    }
}
void xoaall(elementtype x, list *l)//xoa tat ca cac vi tri
{
position p=0,i;
int kt=0;
for(i=p;i<l->last;i++)
if(l->element[i]==x)
{
delete_list(i,l);
kt++;
i--;
}
if(kt==0)
printf("khong co %d trong mang",x);
// else
// {
//printf("khong co %d trong mang",x);
// break;
// }

}
void timledautien(elementtype x, list *l)
{
int vt=locate(x,*l);
while(l->element[vt]%2==0 && vt<=l->last)
{
vt++;
}
if(vt<=l->last)
printf("\n vi tri le dau tien sau x la: %d ",l->element[vt]);
else
printf("khong tim thay:");
}


void InDanhSach(list *l)
{
    if (l->last ==0)
printf("Danh sach rong!\n");
    else
        for(int i=1;i<=l->last;i++)
            printf("%d ",l->element[i-1]);
}


main()
{
list l;
position p;
elementtype x,x1,x2;
//clrscr();
NhapTang(&l);
printf("\n DS da nhap: ");
InDanhSach(&l);
printf("\n Nhap phan tu can xoa: ");scanf("%d",&x);
xoaall(x,&l);
printf("\n Danh sach sau khi xoa: ");
InDanhSach(&l);
printf("\n Nhap phan tu can tim : ");scanf("%d",&x1);
timledautien(x1,&l);
printf("\n Nhap phan tu can chen de mang tang: ");scanf("%d",&x2);
ChenTang(x2,&l);
printf("\n Danh sach sau khi chen: ");
print_list(l);
getch();
}

0 nhận xét:

Đăng nhận xét