//Msv:581597
//Lớp: K58QLTT
#include<iostream>
using namespace std;
//Khai bao lop
class DSLKD
{
          private:
                   struct
node 
                   {
                             int
infor;
                             node
*link;
                   }*F;
          public:
                   ~DSLKD();
                   DSLKD();
                   void
insertFirst(int x);//chen vào vị trí đầu của ds
                   int
deleteFirst();//xóa nút đầu của ds
                   int
empty();//kiểm tra ds rỗng
};
//==Chuong trinh chinh==
int main()
{
          DSLKD
ds;
          int
n;
          do
          {
                   cout<<"Nhap
so nguyen :";
                   cin>>n;
                   if(n<0)
                             cout<<"So
nguyen nhap <0 vui long nhap lai: "<<endl;
          }while(n<0);
          while(n)
          {
                   ds.insertFirst(n%10);
                   n/=10;
          }
          int
tong=0;
          while(!ds.empty())
                   tong+=ds.deleteFirst();
          cout<<"Tong
cua so nguyen da nhap la: "<<tong;
          return
0;
}
//===Dinh nghia ham==========
DSLKD::~DSLKD()
{
          node
*P;
          while(F)
          {
                   P=F;
                   F=F->link;
delete P;
delete P;
          }
}
//----------------
DSLKD::DSLKD():F(NULL)
{
}
//----------------
void DSLKD::insertFirst(int x)
{
          node
*N;
          N=new
node();
          N->infor=x;
          N->link=NULL;
          N->link=F;
          F=N;
}
//----------------
int DSLKD::deleteFirst()
{
          if(F==NULL)
                   return
0;
          node
*P=F;
          int
y=F->infor;
          F=F->link;
          delete
P;
          return
y;
}
//----------------
int DSLKD::empty()
{
          if(F==NULL)
                   return
1;
          return
0;
}






0 comments:
Post a Comment