Friday, June 17, 2016

Hàng đợi cài đặt bằng danh sách liên kết đơn tính tổng các chữ số của một số nguyên dương Lập trình hướng đối tượng Với C++

//Họ và tên: Trần Văn Linh
//Msv:581597
//Lớp :K58QLTT

#include<iostream>

using namespace std;
//Khai bao lop
class DSLKD
{
          private:
                   struct node
                   {
                             int infor;
                             node *link;
                   }*F,*R;
          public:
                   ~DSLKD();
                   DSLKD();
                   void Insert(int x);//Chèn vào lối sau
                   int Delete();//Xóa ở lối sau
                   int empty();//Kiểm tra hằng đợi 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);
         
          int tg;
          while(n)
          {
                   ds.Insert(n%10);
                   n/=10;
          }
          int tong=0;
          while(!ds.empty())
                   tong+=ds.Delete();
          cout<<"Tong cua so nguyen da nhap la: "<<tong;
          return 0;
}
DSLKD::~DSLKD()
{
          node *P;
          while(F)
          if(F==R)
                   delete F;
          else
          {
                   P=F;
                   delete P;
                   F=F->link;
          }
}
//----------------
DSLKD::DSLKD():F(NULL),R(NULL)
{
         
}
//----------------
void DSLKD::Insert(int x)
{
          node *N;
          N=new node();
          N->infor=x;
          N->link=NULL;
          if(F==NULL)
                   F=R=N;
          else
          {
                   R->link=N;
                   R=N;
          }
}
//----------------
int DSLKD::Delete()
{
          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