Bài giải stack C++ [Lê Văn Vuơng_08TH1D]
Diễn đàn cntt ĐH-TÔN ĐỨC THẮNG.Thân mời các anh em tham gia để diễn đàn phong phú hơn :: Tài liệu Môn học :: cấu trúc dữ liệu và giải thuật
Trang 1 trong tổng số 1 trang
Bài giải stack C++ [Lê Văn Vuơng_08TH1D]
#pragma once
class Node//lớp này là biểu diễn cho 1 nút trên stack
{
public :
int gia_tri;
Node *next;
Node(int giatri)
{
gia_tri=giatri;
this->next=NULL;
}
};
class Chong_Stack
{
Node *pt_dau,*pt_duoi;
int SoLuongPhanTu;
public:
Chong_Stack(void);
~Chong_Stack(void);
void ThemVao(int giatri);
bool LayRa(int &giatri_ra);
void In_Stack();
};
////////////////////// tuơng tự như bài LIST ,bài này chia làm mấy file rời,file trên là Chong_Stack.h///////////
#include "StdAfx.h"
#include "Chong_Stack.h"
#include <iostream>
using namespace std;
Chong_Stack::Chong_Stack(void)
{
pt_dau=pt_duoi=NULL;
SoLuongPhanTu=0;
}
Chong_Stack::~Chong_Stack(void)//đây là phương thức hủy bỏ theo trình tự từ phần tử đầu đến phần tử cuối
//còn trong bài List là phương pháp hủy theo trình tự từ cuối lên đầu
{
while(pt_dau)
{
Node *p=pt_dau;
pt_dau=pt_dau->next;
delete p;
}
pt_duoi=pt_dau;
}
void Chong_Stack::ThemVao(int giatri)
{
Node *p=new Node(giatri);
if(pt_dau==NULL)
{
pt_dau=pt_duoi=p;
}
else
{
p->next=pt_dau;
pt_dau=p;
}
this->SoLuongPhanTu++;
}
bool Chong_Stack::LayRa(int &giatri_ra)
{
if(pt_dau==NULL)
{
return false;
}
else
{
giatri_ra=pt_dau->gia_tri;
pt_dau=pt_dau->next;
if(pt_dau==NULL)
pt_duoi=NULL;
this->SoLuongPhanTu--;
return true;
}
}
void Chong_Stack::In_Stack()
{
Node *p=pt_dau;
cout<<"\nStack \n";
while(p)
{
cout<<p->gia_tri;
p=p->next;
cout<<"->";
}
cout<<"NULL\n";
}
////////////////file trên này là Chong_Stack.cpp////////////
// STACK.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include"Chong_Stack.h"
#include<iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
Chong_Stack s=Chong_Stack();
cout<<"hay nhap nhung so muon them vao stack\nnhap so '0' de ket thuc\n";
int giatri;
do
{
cin>>giatri;
s.ThemVao(giatri);
}while(giatri!=0);
cout<<"\nchuoi dao nguoc cua day vua nhap vao la\n";
s.In_Stack();
return 0;
}
FILE cuối này luư dứoi tên STACK.cpp
Tác giả bài viết: Lê Văn Vương_08Th1D
class Node//lớp này là biểu diễn cho 1 nút trên stack
{
public :
int gia_tri;
Node *next;
Node(int giatri)
{
gia_tri=giatri;
this->next=NULL;
}
};
class Chong_Stack
{
Node *pt_dau,*pt_duoi;
int SoLuongPhanTu;
public:
Chong_Stack(void);
~Chong_Stack(void);
void ThemVao(int giatri);
bool LayRa(int &giatri_ra);
void In_Stack();
};
////////////////////// tuơng tự như bài LIST ,bài này chia làm mấy file rời,file trên là Chong_Stack.h///////////
#include "StdAfx.h"
#include "Chong_Stack.h"
#include <iostream>
using namespace std;
Chong_Stack::Chong_Stack(void)
{
pt_dau=pt_duoi=NULL;
SoLuongPhanTu=0;
}
Chong_Stack::~Chong_Stack(void)//đây là phương thức hủy bỏ theo trình tự từ phần tử đầu đến phần tử cuối
//còn trong bài List là phương pháp hủy theo trình tự từ cuối lên đầu
{
while(pt_dau)
{
Node *p=pt_dau;
pt_dau=pt_dau->next;
delete p;
}
pt_duoi=pt_dau;
}
void Chong_Stack::ThemVao(int giatri)
{
Node *p=new Node(giatri);
if(pt_dau==NULL)
{
pt_dau=pt_duoi=p;
}
else
{
p->next=pt_dau;
pt_dau=p;
}
this->SoLuongPhanTu++;
}
bool Chong_Stack::LayRa(int &giatri_ra)
{
if(pt_dau==NULL)
{
return false;
}
else
{
giatri_ra=pt_dau->gia_tri;
pt_dau=pt_dau->next;
if(pt_dau==NULL)
pt_duoi=NULL;
this->SoLuongPhanTu--;
return true;
}
}
void Chong_Stack::In_Stack()
{
Node *p=pt_dau;
cout<<"\nStack \n";
while(p)
{
cout<<p->gia_tri;
p=p->next;
cout<<"->";
}
cout<<"NULL\n";
}
////////////////file trên này là Chong_Stack.cpp////////////
// STACK.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include"Chong_Stack.h"
#include<iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
Chong_Stack s=Chong_Stack();
cout<<"hay nhap nhung so muon them vao stack\nnhap so '0' de ket thuc\n";
int giatri;
do
{
cin>>giatri;
s.ThemVao(giatri);
}while(giatri!=0);
cout<<"\nchuoi dao nguoc cua day vua nhap vao la\n";
s.In_Stack();
return 0;
}
FILE cuối này luư dứoi tên STACK.cpp
Tác giả bài viết: Lê Văn Vương_08Th1D
Similar topics
» Bài giải cấu trúc dữ liệu và giải thuật [LIST ]
» HOt!!! bài giải gần hết các chương
» một số bài giải hệ điều hành
» tài liệu cấu trúc dữ liệu và giải thuật
» full các bài giải sort java
» HOt!!! bài giải gần hết các chương
» một số bài giải hệ điều hành
» tài liệu cấu trúc dữ liệu và giải thuật
» full các bài giải sort java
Diễn đàn cntt ĐH-TÔN ĐỨC THẮNG.Thân mời các anh em tham gia để diễn đàn phong phú hơn :: Tài liệu Môn học :: cấu trúc dữ liệu và giải thuật
Trang 1 trong tổng số 1 trang
Permissions in this forum:
Bạn không có quyền trả lời bài viết
|
|