Danhsachlienket

Màu nền
Font chữ
Font size
Chiều cao dòng

#include<stdio.h>

#include<conio.h>

#include<iostream.h>

#include<stdlib.h>

#include<math.h>

typedef struct Node

{

int key;

struct Node * next;

}node;

typedef struct taglist

{

node * pheah;

node * ptail;

}list;

node * getnode(int x)

{

node * p;

p=new node;

if(p==NULL)

{

cout<<"ko du bo nho";

exit(1);

}

p->key=x;

p->next=NULL;

return p;

}

void addfirt(list &l,node *newp)

{

if(l.pheah==NULL)

{

l.pheah=l.ptail=newp;

}

else

{

newp->next=l.pheah;

l.pheah=newp;

}

}

void chenx(list &l)

{

int x;

node * newp;

do

{

cout<<"nhap vao so x = ";

cin>>x;

newp=getnode(x);

addfirt(l,newp);

}while(x!=0);

}

void xuat(list &l)

{

node * p;

for(p=l.pheah;p!=NULL;p=p->next)

cout<<p->key<<"\t";

}

int demphantu(list &l)

{

int dem=0;

node * p;

for(p=l.pheah;p!=NULL;p=p->next)

{

if(p->key%5==0&&p->key!=0)

{

dem=dem+1;

}

}

return dem;

}

int tongcacsoduong(list &l)

{

int s=0;

node * p;

for(p=l.pheah;p!=NULL;p=p->next)

{

if(p->key>0)

{

s=s+p->key;

}

}

return s;

}

void themsaup(list &l,node * p,node * pnew)

{

if(p!=NULL)

{

pnew->next=p->next;

p->next=pnew;

if(p==l.ptail)

{

l.ptail=pnew;

}

}

else

addfirt(l,pnew);

}

node * timduongdau(list &l)

{

for(node * p=l.pheah;p!=NULL;p=p->next)

if(p->key>0)

return p;

}

void themsauduongdau(list &l,int x)

{

node *k=getnode(x);

node *p=timduongdau(l);

if(p==NULL)

addfirt(l,k);

else

themsaup(l,p,k);

}

int ktscp(int &n)

{

for(int i=0;i<n;i++)

if(i*i==n)

return i;

}

int tichcacsochinhphuong(list &l)

{

int s=1;

for(node * p=l.pheah;p!=NULL;p=p->next)

{

if(sqrt(p->key)==ktscp(p->key))

s=s*p->key;

}

return s;

}

void main()

{

list l;

l.pheah=NULL;

l.ptail=NULL;

chenx(l);

xuat(l);

cout<<endl;

//dem cac phan tu chia het cho 5

int dem=demphantu(l);

cout<<"cac phan tu chua gia tri chia het cho 5: ";

cout<<dem<<endl;

//tinh tong cac so duong

int tong=tongcacsoduong(l);

cout<<"tong cac gia tri duong: ";

cout<<tong<<endl;

//tinh tich cac so chinh phuong

int kt=tichcacsochinhphuong(l);

if(kt==1)

cout<<"danh sach ko co so chinh phuong";

else

cout<<" tich cac so chinh phuong la: "<<endl<<kt;

//them x vao sau phan tu duong dau tien

int x;

cout<<endl<<"nhap vao so x : "<<endl;

cin>>x;

themsauduongdau(l,x);

cout<<endl;

xuat(l);

}

Bạn đang đọc truyện trên: Truyen2U.Pro