//Họ Tên: Trần Văn Linh
//MaSV:581597
//Lớp:K58QLTT
DSLK.cpp
#include<iostream>
using namespace std;
//Khai bao lop
class dslkd
{
 private:
  struct node{
   int info;
   node *next;
  }*F;
 public:
  dslkd();
  ~dslkd();
  void chencuoi(int x);
  void xoanode(int x);
  void hien();
  
};
//==Chuong trinh chinh====
int main()
{
 
 dslkd ds;
 int x;
 char kt;
 do
 {
  cout<<"Nhap phan tu muon them vao danh sach:";
  cin>>x;
  ds.chencuoi(x);
  cout<<"\nCo muon nhap them nua khong (C-K):";
  cin>>kt;
 }
 while(kt=='c'||kt=='C');
 cout<<"\nDanh sach da nhap la:";
 ds.hien();
 cout<<"\nNhap phan tu muon xoa:";
 cin>>x;
 ds.xoanode(x);
 cout<<"Danh sach sau khi xoa la:";
 ds.hien();
 cout<<endl;
 return 0;
 return 0;
}
//Dinh nghia ham
dslkd::dslkd():F(NULL)
{
 
}
//------------
dslkd::~dslkd()
{
 node *P;
 while(F)
 {
  P=F;
  F=F->next;
  delete P;
 }
}
//------------
void dslkd::chencuoi(int x)
{
 node* N,*P;
 N=new node;
 N->info=x;
 N->next=NULL;
 
 P=F;
 if(F==NULL)
  F=N;
 else
 {
  while(P->next)
   P=P->next;
  P->next=N;
 }
}
//------------
void dslkd::xoanode(int x)
{
 node *P;
 node *Q;
 P=F;
 while(P)
 {
  if(P->info==x)
   break;
   Q=P;
   P=P->next;
 }
 if(P)
 {
  if(P==F)
   F=F->next;
  else
  {
   Q->next=P->next;
  }
  delete P;
 }
 
}
//------------
void dslkd::hien()
{
 node*P;
 P=F;
 while(P)
 {
  cout<<P->info;
  P=P->next;
 }
}
//------------
Friday, May 13, 2016
Home »
C++
 » Chương trình cài đặt ngăn xếp bằng danh sách liên kết đơn cho phép chèn , xóa  một số phần tử bằng C++






0 comments:
Post a Comment