Bài tập danh sách liên kết

Thảo luận trong 'Lập trình máy tính' bắt đầu bởi white.smut, 12 Tháng chín 2013.

  1. Offline

    white.smut

    • Administrator

    • Loading: |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||] 99% Completed - Error: Disconnected. Please try again!
    Số bài viết:
    842
    Đã được thích:
    425
    Điểm thành tích:
    450
    Đề bài: Viết chương trình khởi tạo danh sách các số nguyên, in ra màn hình các số chẵn và đếm có bao nhiêu nút.
    Mã:
    #include <stdio.h>
    #include <conio.h>
    typedef int Elementype;
    typedef struct SN{
        Elementype a;
        SN *next;
        };
    SN *L;
    void Nhap(SN *&L)
    {
    SN *C,*P;
    int x;
    char ch;
        C=NULL;
        do    {
            P=new SN;
            printf("\nNhap gia tri: ");
            scanf("%d",&x);
            P->a=x;
            P->next=NULL;
            if (L==NULL)    {
                    L=P;
                    C=P;
                    }
            else        {
                    C->next=P;
                    C=P;
                    }
            printf("\nDung lai  = y? ");
            scanf("%s",&ch);
            }
        while    (ch!='y');
    }
    void Xuat(SN *L)
    {
    SN *P;
        P=L;
        while (P!=NULL)
            {
            printf("\t%d",P->a);
            P=P->next;
            }
    }
     
    void Sochan(SN *L)
    {
    SN *P;
        P=L;
        printf("\nCac so chan: ");
        while (P!=NULL)
            {
            if (P->a%2==0)
                printf("\t%d",P->a);
            P=P->next;
            }
    }
     
    int Dem(SN *L)
    {
    int dem=0;
    SN *P;
        P=L;
        while(P!=NULL)
            {
            dem=dem+1;
            P=P->next;
            }
        printf("\nSo nut la: %d",dem);
    }
     
    main()
    {
        clrscr();
        int k,x,a;
        L=NULL;
        Nhap(L);
        printf("\nDVSVT: ");
        Xuat(L);
        Sochan(L);
        Dem(L);
        getch();
        return 1;
    }
    
    File đính kèm (đổi đuôi .TXT thành .CPP nhé):

    Các file đính kèm:

Chia sẻ trang này

Advertising: Linux system admin | nukeviet | nukeviet 4 | Upload ảnh miễn phí