Lớp container thùng chứa gồm 2 thuộc tính: unsigned long count; //Số phần tử trong thùng chứa void *errhandler; //Con trỏ tới hàm xử lý lỗi 2.. Lớp s_list thừa kế từ lớp container, có th
Trang 1Các lớp ngăn xếp và hàng đợi
Chương trình tổ chức thành 4 lớp chính:
1 Lớp container (thùng chứa) gồm 2 thuộc tính:
unsigned long count; //Số phần tử trong thùng chứa
void (*errhandler)(); //Con trỏ tới hàm xử lý lỗi
2 Lớp s_list thừa kế từ lớp container, có thêm 2 thuộc tính các con trỏ kiểu
cấu trúc listnode:
struct listnode
{
void *dataptr;
listnode *next;
};
listnode *head; // Trỏ tới đầu danh sách
listnode *tail; // Trỏ tới cuối danh sách
Các phần tử được chứa trong lớp s_list dưới dạng một danh sách móc nối đơn Mỗi nút chứa địa chỉ của một phần tử Do ở đây dùng kiểu con trỏ void nên có thể đưa vào lớp s_list các phần tử có kiểu bất kỳ
3 Lớp stack thừa kế từ lớp s_list
4 Lớp queue thừa kế từ lớp stack
Các lớp stack và queue không có các thuộc tính riêng Hai phương thức quan trọng của các lớp này là:
virtual int store(void *item) ; // Cất vào một phần tử
virtual void *retrieve () ; // Lấy ra một phần tử
Chú ý là: Lớp stack hoạt động theo nguyên tắc LIFO (vào sau ra trước) còn lớp
queue hoạt động theo nguyên tắc FIFO (vào trước ra trước)
Chương trình sau minh hoạ cách dùng liên kết bội, phương thức ảo và con trỏ kiểu void để quản lý các kiểu dữ liệu khác nhau
Hoạt động của chương trình như sau:
+ Trước tiên lần lượt đưa địa chỉ của biến đối tượng ts1, chuỗi “HA NOI”, biến nguyên a, biến đối tượng ts2 và biến thực x vào ngăn xếp s1 và hàng đợi q1
+ Thực hiện phép gán các biến đối tượng:
s2 = s1 ;
q2 = q1 ;
+ Lấy các phần tử trong ngăn xếp s2 theo trình tự ngược với lúc đưa vào
+ Lấy các phần tử trong hàng đợi q2 theo trình tự như lúc đưa vào
/*
CT10_05.CPP
Lop vat chua (container)
Lop danh sach moc noi
Trang 2Lop ngan xep Lop hang doi Chu y:
1 constructor sao chep cua lop dan suat
2 toan tu gan cua lop dan suat
3 co the dung cac phuong thuc khac
de viet constructor va destructor
4 Dung con tro this
*/
#include <stdio.h>
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <alloc.h>
#include <dos.h>
//Lop container class container {
protected:
unsigned long count; //so pt trong thung chua void (*errhandler)();
public:
container();
container(const container &c); // Ham tao sao chep void operator=(const container &c); // Gan
unsigned long getcount(); // Cho biet so phan tu // Dinh ham xl loi
void seterrorhandler(void (*userhandler)());
// 4 phuong thuc thuan ao virtual int store(void *item)=0;//Cat mot phan tu vao thung virtual void *examine()=0; // Xem gia tri mot phan tu virtual void *retrieve ()=0; // Lay mot pt ra
virtual void empty()=0; // Lam cho thung tro nen rong };
// Cai dat // Ham xl loi mac dinh void defaulthandler();
void defaulthandler() {
526
Trang 3puts("\nContainer error: memory allocation failure"); }
container::container () {
count=0; errhandler= defaulthandler;
} container::container(const container &c) {
count=c.count; errhandler=c.errhandler;
} // Gan void container::operator=(const container &c) {
count=c.count; errhandler=c.errhandler;
} // Cho biet so pt unsigned long container::getcount() {
return count;
} // Dinh ham xl loi void container::seterrorhandler(void (*userhandler)()) {
errhandler=userhandler;
} // Lop danh sach moc noi don class s_list:public container {
protected:
//Cau truc mot nut trong ds struct listnode
{ void *dataptr;
listnode *next;
};
listnode *head;
listnode *tail;
private:
// phuong thuc sao chep 528
Trang 4void copy(const s_list &s1);
public:
s_list();
s_list(const s_list &s1);
~s_list();
void operator=(const s_list &s1);
// 4 phuong thuc ao
virtual int store(void *item)=0; // Cat mot phan tu vao
// thung virtual void *examine()=0; // Xem gia tri mot phan tu virtual void *retrieve ()=0; // Lay mot pt ra
virtual void empty(); // Lam cho thung tro nen rong };
//Cai dat
void s_list::copy(const s_list &s1)
{
head=NULL; tail=NULL;
listnode *temp = s1.head;
while(temp!=NULL)
{
if(head==NULL)
{
head= new listnode;
if(head==NULL) errhandler();
tail=head;
}
else
{
tail->next = new listnode;
if(tail->next == NULL) errhandler();
tail = tail->next;
}
tail->dataptr= temp->dataptr;
tail->next=NULL;
temp = temp->next;
}
}
// constructor
Trang 5s_list::s_list() : container() {
head=NULL; tail=NULL;
} s_list::s_list(const s_list &s1):container(s1) {
copy(s1);
} s_list::~s_list() {
this->empty();
} void s_list::operator=(const s_list &s1) {
this->empty();
count=s1.count;
copy(s1);
} void s_list::empty() {
listnode *q,*p;
p = head; head=NULL; tail=NULL;
while (p!=NULL) {
q=p; p=p->next;
delete q;
} } // Lop stack class stack:public s_list {
public:
stack();
stack(const stack &st);
void operator=(const stack &st);
virtual int store(void *item); // Cat mot phan tu vao thung virtual void *examine(); // Xem gia tri mot phan tu
virtual void *retrieve(); // Lay mot pt ra };
stack::stack():s_list() 530
Trang 6{ } stack::stack(const stack &st):s_list(st) {
} void stack::operator=(const stack &st) {
this->s_list::operator=(st); //Dung toan tu gan cua s_list }
int stack::store(void *item) // Cat mot phan tu vao thung {
//Dua vao dau danh sach listnode *p;
p= new listnode ; if(p==NULL) return 1;
count++;
p->dataptr=item; p->next=head;
head=p; return 0;
} void *stack::examine() // Xem gia tri mot phan tu {
if(count==0) return NULL;
else return head->dataptr;
} void *stack::retrieve() // Lay mot pt ra {
if(count==NULL) return NULL;
else { listnode *p; void *value;
value = head->dataptr;
p=head;
head = p->next;
delete p;
count ;
return value;
} } // Lop queue class queue:public stack 532
Trang 7public:
queue();
queue(const queue &q);
void operator=(const queue &q);
virtual int store(void *item); // Cat mot phan tu vao thung };
queue::queue(): stack()
{
}
queue::queue(const queue &q):stack(q)
{
}
void queue::operator=(const queue &q)
{
this->stack::operator=(q); //Dung toan tu gan cua stack }
int queue::store(void *item)
{
// Dat vao cuoi
listnode *q;
q=new listnode;
if(q==NULL)return 1;
// Bo sung
q->next=NULL; q->dataptr=item;
if(count==0)
{
head=q; tail=q;
}
else
{
tail->next=q;
tail=q;
}
count++; return 0;
}
class TS
{
private:
Trang 8char ht[25];
int sobd;
float td;
public:
void nhap() {
cout << "\nHo ten: " ; fflush(stdin);
gets(ht);
cout << "So bao danh: " ; cin >> sobd;
cout << "Tong diem: " ; cin >> td;
} void xuat() {
cout << "\nHo ten: " << ht;
cout << "\nSo bao danh: " << sobd;
cout << "\nTong diem: " << setiosflags(ios::showpoint)
<< setprecision(1)<<setw(5)<< td;
} };
// Ham main void main() {
stack s1,s2; queue q1,q2;
TS ts1,ts2,ts;
int a=123,b;
float x=3.14,y;
char *str;
clrscr();
ts1.nhap();
ts2.nhap();
//Gui vao s1.store(&ts1); q1.store(&ts1);
s1.store("HA NOI"); q1.store("HA NOI");
s1.store(&a); q1.store(&a);
s1.store(&ts2); q1.store(&ts2);
s1.store(&x); q1.store(&x);
534
Trang 9//Lay ra tu ngan xep theo nguyen tac LIFO cout <<"\n\nLay ra tu ngan xep:" ;
s2=s1;
y = *((float*)s2.retrieve());
cout << "\nSo thuc = " <<setiosflags(ios::showpoint)
<< setprecision(2)<< y;
ts = *((TS*)s2.retrieve());
ts.xuat();
b = *((int*)s2.retrieve());
cout << "\nSo nguyen = " << b;
str = (char*)s2.retrieve();
cout << "\nChuoi ky tu: " << str;
ts = *((TS*)s2.retrieve());
ts.xuat();
//Lay ra tu hang doi theo nguyen tac FIFO cout <<"\n\nLay ra tu hang doi:" ;
q2=q1;
ts = *((TS*)q2.retrieve());
ts.xuat();
str = (char*)q2.retrieve();
cout << "\nChuoi ky tu: " << str;
b = *((int*)q2.retrieve());
cout << "\nSo nguyen = " << b;
ts = *((TS*)q2.retrieve());
ts.xuat();
y = *((float*)q2.retrieve());
cout << "\nSo thuc = " << setiosflags(ios::showpoint)
<< setprecision(2)<< y;
getch();
} 536