Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //by FamiHug@FAMILUG
- #include <stdio.h>
- #include <stdlib.h>
- int init();
- void insert_head(int);
- void insert_tail(int);
- void delete_head();
- void delete_tail();
- typedef struct pointer
- {
- int info;
- struct pointer *link;
- }dslk;
- dslk *l,*p;
- int n,d,i;
- float s;
- int main()
- {
- int x;
- int init();
- printf("CHUONG TRINH MINH HOA DANH SACH LIEN KET DON\n");
- printf("Ung dung de tinh trung binh cong cua cac phan tu chan trong day so nhap vao\n");
- printf("======================================================\n");
- printf("Nhap vao so phan tu cua danh sach: ");
- scanf("%d",&n);
- if(n>=0)
- {
- for(i=1;i<=n;i++)
- {
- printf("Phan tu thu %d:",i);
- scanf("%d",&x);
- insert_head(x);
- }
- s=0;
- d=0;
- p=l;
- for(i=1;i<=n;i++)
- {
- if(p->info %2 ==0)
- {
- d++;
- s=s+p->info;
- }
- p=p->link;
- }
- if(d!=0) printf("TBC=%f",s/d);
- else printf(" Khong co phan tu nao chia het cho 2 ");
- }else printf("So nhap vao khong hop le. Chuong trinh ket thuc");
- return 0;
- }
- int init()
- {
- l=NULL;
- return 0;
- }
- void insert_head(int x)
- {
- p=(dslk *)malloc(sizeof(dslk));
- p->info=x;
- p->link=l;
- l=p;
- }
- void insert_tail(int x)
- {
- dslk *q;
- q=(dslk *)malloc(sizeof(dslk));
- p->info=x;
- p->link=NULL;
- if(l==NULL) l=p;
- else
- {
- q=l;
- while(q->link!=NULL)
- {
- q=q->link;
- }
- q->link=p;
- }
- }
- void delete_head()
- {
- if(l==NULL) printf("\n Danh sach rong");
- else
- {
- p=l;
- l=l->link;
- free(p);
- }
- }
- void delete_tail()
- {
- dslk *q;
- if(l==NULL) printf("\n Danh sach rong");
- else
- {
- if (l==NULL)
- {
- p=l;
- l=NULL;
- free(p);
- }
- else
- {
- q=l;
- while (q->link->link !=NULL)
- {
- q=q->link;
- p=q->link;
- q->link=NULL;
- free(p);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement